user.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. video "go-common/app/service/bbq/video/api/grpc/v1"
  6. "go-common/library/log"
  7. "os"
  8. "time"
  9. )
  10. //taskSyncUserDmg 同步用户画像
  11. func (s *Service) taskSyncUserDmg() {
  12. jobURL, err := s.dao.QueryUserDmg(context.Background())
  13. if err != nil {
  14. log.Error("get user dmg err(%v)", err)
  15. return
  16. }
  17. urls, err := s.dao.QueryJobStatus(context.Background(), jobURL)
  18. if err != nil {
  19. log.Error("get user dmg job result err(%v)", err)
  20. return
  21. }
  22. for _, url := range urls {
  23. go func(url string) {
  24. fpath, err := s.dao.Download(url, "")
  25. if err != nil {
  26. return
  27. }
  28. s.dao.ReadLine(fpath, s.dao.HandlerUserDmg)
  29. os.RemoveAll(fpath)
  30. }(url)
  31. }
  32. }
  33. //UserProfileUpdate bbq_user_profile
  34. func (s *Service) UserProfileUpdate() {
  35. _, err := s.dao.UserProfileGet(context.Background())
  36. if err != nil {
  37. log.Error("get user dmg err(%v)", err)
  38. }
  39. }
  40. //taskSyncUserDmg 同步天马推荐用户画像
  41. func (s *Service) taskSyncPegasusUserBasic() {
  42. _, err := s.dao.QueryUserBasic(context.Background())
  43. if err != nil {
  44. log.Error("get user dmg err(%v)", err)
  45. }
  46. }
  47. //taskSyncUpUserDmg 同步up主画像
  48. func (s *Service) taskSyncUpUserDmg() {
  49. log.Infov(context.Background(), log.KV("event", "sync_up_user_dmg"))
  50. var mid = int64(0)
  51. for {
  52. log.Infov(context.Background(), log.KV("event", "one_sync_up_user_dmg"), log.KV("mid", mid))
  53. upUserDmgs, err := s.dao.QueryUpUserDmg(context.Background(), mid)
  54. if err != nil {
  55. log.Error("get up user dmg err(%v)", err)
  56. return
  57. }
  58. if len(upUserDmgs) == 0 {
  59. break
  60. }
  61. for _, upUserDmg := range upUserDmgs {
  62. mid = upUserDmg.MID
  63. fmt.Println(upUserDmg.MID)
  64. if err = s.dao.InsertOnDup(context.Background(), upUserDmg); err != nil {
  65. log.Error("user dmg insert on dup failed,mid :%v, err:%v", upUserDmg.MID, err)
  66. }
  67. }
  68. }
  69. // TODO: 考虑个好方法
  70. // 万一对方接口有问题,那就都完了
  71. //s.dao.DelUpUserDmg(context.Background())
  72. }
  73. // 同步up主画像 从hive更新到user_statistics_hive
  74. func (s *Service) taskSyncUsrStaFromHive() {
  75. log.Infov(context.Background(), log.KV("event", "taskSyncUsrStaFromHive"))
  76. var (
  77. err error
  78. jobURL string
  79. urls []string
  80. url string
  81. fpath string
  82. try int
  83. date = time.Now().AddDate(0, 0, -1).Format("20060102")
  84. )
  85. for try = 1; try <= 3; try++ {
  86. //发起hive查询,拿到url
  87. if jobURL, err = s.dao.QueryUpMid(context.Background(), date); err != nil {
  88. log.Warn("taskSyncUsrStaFromHive try and init query hive failed, err(%v)", err)
  89. continue
  90. }
  91. log.Info("taskSyncUsrStaFromHive init query hive success")
  92. //查询job状态
  93. if urls, err = s.dao.QueryJobStatus(context.Background(), jobURL); err != nil {
  94. log.Warn("taskSyncUsrStaFromHive try and get hive query status failed, err(%v)", err)
  95. continue
  96. }
  97. break
  98. }
  99. if err != nil {
  100. log.Error("taskSyncUsrStaFromHive init and get hive query status failed, err(%v)", err)
  101. return
  102. }
  103. for _, url = range urls {
  104. for try = 0; try <= 3; try++ {
  105. if fpath, err = s.dao.Download(url, ""); err != nil {
  106. log.Warn("taskSyncUsrInfoFromHive try and download file (%v) failed, err(%v)", url, err)
  107. time.Sleep(time.Duration(try*10) * time.Second)
  108. continue
  109. }
  110. s.dao.ReadLines(fpath, s.dao.HandlerMids)
  111. os.RemoveAll(fpath)
  112. return
  113. }
  114. if err != nil {
  115. log.Error("taskSyncUsrInfoFromHive download file (%v) failed, err(%v)", url, err)
  116. }
  117. }
  118. }
  119. //从video表同步up画像
  120. //func (s *Service) taskSyncUsrBaseFromVideo(c context.Context) {
  121. // log.Infov(context.Background(), log.KV("event", "taskSyncUsrInfoFromVideo"))
  122. // //get mids
  123. // var (
  124. // mids *[]int64
  125. // err error
  126. // i int8
  127. // mid int64
  128. // req *video.SyncUserBaseResponse
  129. // )
  130. // for i := 0; i <= 3; i++ {
  131. // mids, err = s.dao.SelMidFromVideo()
  132. // if err != nil {
  133. // log.Info("taskSyncUsrInfoFromVideo try and get up mid failed , err(%v)", err)
  134. // } else {
  135. // break
  136. // }
  137. // }
  138. // if err != nil {
  139. // log.Error("taskSyncUsrInfoFromVideo get up mid failed, err(%v)", err)
  140. // }
  141. // //get userinfo and update
  142. // for _, mid = range *mids {
  143. // //重试
  144. // for i = 0; i <= 3; i++ {
  145. // if req, err = s.dao.VideoClient.SyncUserBase(c, &video.SyncMidRequset{MID: mid}); err != nil || req.Affc == -1 {
  146. // log.Info("taskSyncUsrInfoFromVideo try and failed, mid(%v), err(%v)", mid, err)
  147. // } else {
  148. // break
  149. // }
  150. // }
  151. // if err != nil {
  152. // log.Error("taskSyncUsrInfoFromVideo failed, mid(%v), err(%v)", mid, err)
  153. // }
  154. // }
  155. //}
  156. //从video表同步up画像
  157. func (s *Service) taskSyncUsrBaseFromVideo(c context.Context) {
  158. fmt.Println("taskSyncUsrBaseFromVideo start")
  159. log.Infov(context.Background(), log.KV("event", "taskSyncUsrInfoFromVideo"))
  160. //get mids
  161. var (
  162. mids []int64
  163. err error
  164. req *video.SyncUserBaseResponse
  165. )
  166. for try := 0; try <= 3; try++ {
  167. mids, err = s.dao.SelMidFromVideo()
  168. if err != nil {
  169. log.Info("taskSyncUsrInfoFromVideo try and get up mid failed , err(%v)", err)
  170. } else {
  171. break
  172. }
  173. }
  174. if err != nil {
  175. log.Error("taskSyncUsrInfoFromVideo get up mid failed, err(%v)", err)
  176. }
  177. i := len(mids) / 50
  178. //get userinfo and update
  179. for j := 1; j <= i; j++ {
  180. for try := 0; try <= 3; try++ {
  181. if req, err = s.dao.VideoClient.SyncUserBases(c, &video.SyncMidsRequset{MIDS: mids[(j-1)*50 : j*50]}); err != nil {
  182. log.Info("taskSyncUsrInfoFromVideo try and failed, err(%v)", err)
  183. } else {
  184. break
  185. }
  186. }
  187. if err != nil {
  188. log.Error("taskSyncUsrInfoFromVideo failed, err(%v)", err)
  189. } else {
  190. log.Info("taskSyncUsrInfoFromVideo success ,affected %v rows", req.Affc)
  191. }
  192. }
  193. if i*50 < len(mids) {
  194. for try := 0; try <= 3; try++ {
  195. if req, err = s.dao.VideoClient.SyncUserBases(c, &video.SyncMidsRequset{MIDS: mids[i*50:]}); err != nil {
  196. log.Info("taskSyncUsrInfoFromVideo try and failed, err(%v)", err)
  197. } else {
  198. break
  199. }
  200. }
  201. if err != nil {
  202. log.Error("taskSyncUsrInfoFromVideo failed, err(%v)", err)
  203. } else {
  204. log.Info("taskSyncUsrInfoFromVideo success ,affected %v rows", req.Affc)
  205. }
  206. }
  207. }
  208. // UpdateUsrBaseFace 更新user_base里的face字段
  209. func (s *Service) UpdateUsrBaseFace() (err error) {
  210. var (
  211. mids []int64
  212. )
  213. log.Infov(context.Background(), log.KV("event", "UpdateUsrBaseFace"))
  214. for i := 0; ; i++ {
  215. mids, err = s.dao.SelMidFromUserBase(i * 1000)
  216. if err != nil {
  217. log.Error("UpdateUsrBaseFace select mid failed")
  218. return
  219. }
  220. if len(mids) == 0 {
  221. break
  222. }
  223. s.dao.UpUserBases(context.Background(), mids)
  224. }
  225. return
  226. }