search_sug.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package pgc
  2. import (
  3. "encoding/json"
  4. "os"
  5. "time"
  6. model "go-common/app/job/main/tv/model/pgc"
  7. "go-common/library/log"
  8. )
  9. const (
  10. errFormat = "Func:[%s] - Step:[%s] - Error:[%v]"
  11. )
  12. func (s *Service) pgcSeaSug(f *os.File) (err error) {
  13. var (
  14. str []byte // the json string to write in file
  15. sug []*model.SearchSug
  16. )
  17. if sug, err = s.dao.PgcSeaSug(ctx); err != nil {
  18. log.Error(errFormat, "searchSug", "PgcSeaSug", err)
  19. return
  20. }
  21. for _, v := range sug {
  22. if str, err = json.Marshal(v); err != nil {
  23. log.Error(errFormat, "searchSug", "JsonMarshal", err)
  24. return
  25. }
  26. f.WriteString(string(str) + "\n")
  27. }
  28. return
  29. }
  30. func (s *Service) ugcSeaSug(f *os.File) (err error) {
  31. var (
  32. str []byte // the json string to write in file
  33. sug []*model.SearchSug
  34. )
  35. if sug, err = s.dao.UgcSeaSug(ctx); err != nil {
  36. log.Error(errFormat, "ugcSeaSug", "UgcSeaSug", err)
  37. return
  38. }
  39. for _, v := range sug {
  40. if str, err = json.Marshal(v); err != nil {
  41. log.Error(errFormat, "ugcSeaSug", "JsonMarshal", err)
  42. return
  43. }
  44. f.WriteString(string(str) + "\n")
  45. }
  46. return
  47. }
  48. func (s *Service) searchSugproc() {
  49. for {
  50. if s.daoClosed {
  51. log.Info("searchSugproc DB closed!")
  52. return
  53. }
  54. s.searchSug()
  55. time.Sleep(time.Duration(s.c.Search.Cfg.UploadFre))
  56. }
  57. }
  58. // generate the valid seasons file for search suggestion
  59. func (s *Service) searchSug() {
  60. // write into the file
  61. file, err := os.OpenFile(s.c.Search.SugPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0766)
  62. if err != nil {
  63. log.Error(errFormat, "searchSug", "OpenFile", err)
  64. return
  65. }
  66. if err := s.pgcSeaSug(file); err != nil {
  67. log.Error(errFormat, "searchSug", "OpenFile", err)
  68. return
  69. }
  70. //if switch is on then onpen ugc search suggest
  71. if s.c.Search.UgcSwitch == "on" {
  72. if err := s.ugcSeaSug(file); err != nil {
  73. log.Error(errFormat, "searchSug", "OpenFile", err)
  74. return
  75. }
  76. }
  77. file.Close()
  78. // calculate file's md5
  79. if err := s.ftpDao.FileMd5(s.c.Search.SugPath, s.c.Search.Md5Path); err != nil {
  80. log.Error(errFormat, "searchSug", "fileMd5", err)
  81. return
  82. }
  83. // upload original file
  84. if err := s.ftpDao.UploadFile(s.c.Search.SugPath, s.c.Search.FTP.RemoteFName, s.c.Search.FTP.URL); err != nil {
  85. log.Error(errFormat, "searchSug-File", "uploadFile", err)
  86. return
  87. }
  88. //upload md5 file
  89. if err := s.ftpDao.UploadFile(s.c.Search.Md5Path, s.c.Search.FTP.RemoteMd5, s.c.Search.FTP.URL); err != nil {
  90. log.Error(errFormat, "searchSug-Md5", "uploadFile", err)
  91. return
  92. }
  93. log.Error("FTP Upload Success")
  94. }