infoc.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package service
  2. import (
  3. "strconv"
  4. "time"
  5. "encoding/json"
  6. "go-common/app/interface/openplatform/article/model"
  7. "go-common/library/log"
  8. binfoc "go-common/library/log/infoc"
  9. "go-common/library/stat/prom"
  10. )
  11. type displayInfo struct {
  12. ip string
  13. mid string
  14. now string
  15. client string
  16. build string
  17. buvid string
  18. pagetype string
  19. pageNo string
  20. isRec string
  21. showlist json.RawMessage
  22. }
  23. type clickInfo struct {
  24. mid string
  25. client string
  26. build string
  27. buvid string
  28. now string
  29. from string
  30. itemID string
  31. itemType string
  32. extra json.RawMessage
  33. }
  34. type aiClickInfo struct {
  35. mid string
  36. client string
  37. build string
  38. buvid string
  39. time string
  40. from string
  41. itemID string
  42. itemType string
  43. actionID string
  44. action string
  45. extra json.RawMessage
  46. }
  47. // 用户在列表页停留上报
  48. type showInfo struct {
  49. ip string
  50. time string
  51. buvid string
  52. mid string
  53. client string
  54. pageType string
  55. from string
  56. build string
  57. extra string
  58. }
  59. type recItem struct {
  60. ID int64 `json:"id"`
  61. Page int64 `json:"page"`
  62. Pos int64 `json:"pos"`
  63. View int64 `json:"view"`
  64. Fav int64 `json:"fav"`
  65. Like int64 `json:"like"`
  66. Reply int64 `json:"reply"`
  67. Share int64 `json:"share"`
  68. AvFeature string `json:"av_feature,omitempty"`
  69. UserFeature string `json:"user_feature,omitempty"`
  70. }
  71. // RecommendInfoc .
  72. func (s *Service) RecommendInfoc(mid int64, plat int8, pageType, cid, build int, buvid, ip string, metas []*model.Meta, isRcmd bool, now time.Time, pn int64, sky *model.SkyHorseResp) {
  73. var isRc = "0"
  74. if isRcmd {
  75. isRc = "1"
  76. }
  77. skyMap := make(map[int64]string)
  78. if sky != nil {
  79. for _, item := range sky.Data {
  80. skyMap[item.ID] = item.AvFeature
  81. }
  82. }
  83. var list []*recItem
  84. for i, m := range metas {
  85. x := &recItem{ID: m.ID, Page: pn, Pos: int64(i + 1), View: m.Stats.View, Fav: m.Stats.Favorite, Like: m.Stats.Like, Reply: m.Stats.Reply, Share: m.Stats.Share}
  86. if sky != nil && sky.UserFeature != "" {
  87. x.UserFeature = sky.UserFeature
  88. }
  89. x.AvFeature = skyMap[m.ID]
  90. list = append(list, x)
  91. }
  92. var sl = &struct {
  93. List []*recItem `json:"itemlist"`
  94. }{
  95. List: list,
  96. }
  97. msg, _ := json.Marshal(sl)
  98. s.infoc(displayInfo{ip, strconv.FormatInt(mid, 10), strconv.FormatInt(now.Unix(), 10), strconv.Itoa(int(plat)), strconv.Itoa(build), buvid, strconv.Itoa(pageType), strconv.Itoa(cid), isRc, msg})
  99. }
  100. // ViewInfoc .
  101. func (s *Service) ViewInfoc(mid int64, plat int8, build int, itemType, from, buvid string, itemID int64, now time.Time, ua string) {
  102. var extra = &struct {
  103. UA string `json:"ua"`
  104. }{
  105. UA: ua,
  106. }
  107. msg, _ := json.Marshal(extra)
  108. s.infoc(clickInfo{strconv.FormatInt(mid, 10), strconv.Itoa(int(plat)), strconv.Itoa(build), buvid, strconv.FormatInt(now.Unix(), 10), from, strconv.FormatInt(itemID, 10), itemType, msg})
  109. }
  110. // AIViewInfoc .
  111. func (s *Service) AIViewInfoc(mid int64, plat int8, build int, itemType, from, buvid string, itemID int64, now time.Time, ua string) {
  112. var extra = &struct {
  113. UA string `json:"ua"`
  114. }{
  115. UA: ua,
  116. }
  117. msg, _ := json.Marshal(extra)
  118. s.infoc(aiClickInfo{
  119. mid: strconv.FormatInt(mid, 10),
  120. client: model.Client(plat),
  121. build: strconv.Itoa(build),
  122. buvid: buvid,
  123. time: strconv.FormatInt(now.Unix(), 10),
  124. from: from,
  125. itemID: strconv.FormatInt(itemID, 10),
  126. itemType: itemType,
  127. action: "click",
  128. actionID: "",
  129. extra: msg,
  130. })
  131. }
  132. // ShowInfoc .
  133. func (s *Service) ShowInfoc(ip string, now time.Time, buvid string, mid int64, client int8, pageType string, from string, build string, ua string, referer string) {
  134. var extra = &struct {
  135. UA string `json:"ua"`
  136. Referer string `json:"referer"`
  137. }{
  138. UA: ua,
  139. Referer: referer,
  140. }
  141. msg, _ := json.Marshal(extra)
  142. s.infoc(showInfo{
  143. ip: ip,
  144. time: strconv.FormatInt(now.Unix(), 10),
  145. buvid: buvid,
  146. mid: strconv.FormatInt(mid, 10),
  147. client: strconv.Itoa(int(client)),
  148. pageType: pageType,
  149. from: from,
  150. build: build,
  151. extra: string(msg),
  152. })
  153. }
  154. func (s *Service) infoc(i interface{}) {
  155. select {
  156. case s.logCh <- i:
  157. default:
  158. log.Warn("infocproc chan full")
  159. }
  160. }
  161. // writeInfoc
  162. func (s *Service) infocproc() {
  163. var (
  164. displayInfoc = binfoc.New(s.c.DisplayInfoc)
  165. clickInfoc = binfoc.New(s.c.ClickInfoc)
  166. aiClickInfoc = binfoc.New(s.c.AIClickInfoc)
  167. showInfoc = binfoc.New(s.c.ShowInfoc)
  168. )
  169. for {
  170. i, ok := <-s.logCh
  171. if !ok {
  172. log.Warn("infoc proc exit")
  173. return
  174. }
  175. prom.BusinessInfoCount.State("infoc_channel", int64(len(s.logCh)))
  176. switch l := i.(type) {
  177. case displayInfo:
  178. displayInfoc.Info(l.ip, l.now, l.buvid, l.mid, l.client, l.pagetype, l.pageNo, string(l.showlist), l.isRec, l.build)
  179. log.Info("infocproc displayInfo param(ip:%s,now:%s,buvid:%s,mid:%s,client:%s,pagetype:%s,pageno:%s,showlist:%s,isRec:%s,build:%s)", l.ip, l.now, l.buvid, l.mid, l.client, l.pagetype, l.pageNo, l.showlist, l.isRec, l.build)
  180. case clickInfo:
  181. clickInfoc.Info(l.from, l.now, l.buvid, l.mid, l.client, l.itemType, l.itemID, "", l.build)
  182. log.Info("infocproc clickInfoc param(client:%s,buvid:%s,mid:%s,now:%s,from:%s,build:%s,itemID:%s,itemType:%s)", l.client, l.buvid, l.mid, l.now, l.from, l.build, l.itemID, l.itemType)
  183. case aiClickInfo:
  184. aiClickInfoc.Info(l.client, l.buvid, l.mid, l.time, l.from, l.build, l.itemID, l.itemType, l.action, l.actionID, string(l.extra))
  185. log.Info("infocproc aiclickInfoc param(client:%s,buvid:%s,mid:%s,time:%s,from:%s,build:%s,itemID:%s,itemType:%s,action: %s, actionID: %s, extra: %s)", l.client, l.buvid, l.mid, l.time, l.from, l.build, l.itemID, l.itemType, l.action, l.actionID, string(l.extra))
  186. case showInfo:
  187. showInfoc.Info(l.ip, l.time, l.buvid, l.mid, l.client, l.pageType, l.from, l.build, l.extra)
  188. log.Info("infocproc showInfoc param(%+v)", l)
  189. }
  190. }
  191. }