infoc.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package feed
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "strconv"
  7. "time"
  8. "go-common/app/interface/main/app-card/model/card/ai"
  9. "go-common/app/interface/main/app-intl/model"
  10. "go-common/library/log"
  11. binfoc "go-common/library/log/infoc"
  12. "go-common/library/net/metadata"
  13. )
  14. // infoc struct
  15. type infoc struct {
  16. typ string
  17. mid string
  18. client string
  19. build string
  20. buvid string
  21. disid string
  22. ip string
  23. style string
  24. api string
  25. now string
  26. isRcmd string
  27. pull string
  28. userFeature json.RawMessage
  29. code string
  30. items []*ai.Item
  31. lp bool
  32. zoneID string
  33. adResponse string
  34. deviceID string
  35. network string
  36. newUser string
  37. flush string
  38. autoPlay string
  39. deviceType string
  40. }
  41. // IndexInfoc is.
  42. func (s *Service) IndexInfoc(c context.Context, mid int64, plat int8, build int, buvid, disid, api string, userFeature json.RawMessage, style, code int, items []*ai.Item, isRcmd, pull, newUser bool, now time.Time, adResponse, deviceID, network string, flush int, autoPlay string, deviceType int) {
  43. if items == nil {
  44. return
  45. }
  46. var (
  47. isRc = "0"
  48. isPull = "0"
  49. isNewUser = "0"
  50. zoneID int64
  51. )
  52. if isRcmd {
  53. isRc = "1"
  54. }
  55. if pull {
  56. isPull = "1"
  57. }
  58. if newUser {
  59. isNewUser = "1"
  60. }
  61. ip := metadata.String(c, metadata.RemoteIP)
  62. info, err := s.loc.Info(c, ip)
  63. if err != nil {
  64. log.Warn(" s.loc.Info(%v) error(%v)", ip, err)
  65. err = nil
  66. }
  67. if info != nil {
  68. zoneID = info.ZoneID
  69. }
  70. s.infoc(infoc{"综合推荐", strconv.FormatInt(mid, 10), strconv.Itoa(int(plat)), strconv.Itoa(build), buvid, disid, ip, strconv.Itoa(style), api, strconv.FormatInt(now.Unix(), 10), isRc, isPull, userFeature, strconv.Itoa(code), items, false, strconv.FormatInt(zoneID, 10), adResponse, deviceID, network, isNewUser, strconv.Itoa(flush), autoPlay, strconv.Itoa(deviceType)})
  71. }
  72. // infoc is.
  73. func (s *Service) infoc(i interface{}) {
  74. select {
  75. case s.logCh <- i:
  76. default:
  77. log.Warn("infocproc chan full")
  78. }
  79. }
  80. // infocproc is.
  81. func (s *Service) infocproc() {
  82. const (
  83. // infoc format {"section":{"id":"%s推荐","pos":1,"style":%d,"items":[{"id":%s,"pos":%d,"type":1,"url":""}]}}
  84. noItem1 = `{"section":{"id":"`
  85. noItem2 = `{","pos":1,"style":`
  86. noItem3 = `,"items":[]}}`
  87. )
  88. var (
  89. msg1 = []byte(`{"section":{"id":"`)
  90. msg2 = []byte(`","pos":1,"style":`)
  91. msg3 = []byte(`,"items":[`)
  92. msg4 = []byte(`{"id":`)
  93. msg5 = []byte(`,"pos":`)
  94. msg6 = []byte(`,"type":`)
  95. msg7 = []byte(`,"source":"`)
  96. msg8 = []byte(`","tid":`)
  97. msg9 = []byte(`,"av_feature":`)
  98. msg10 = []byte(`,"url":"`)
  99. msg11 = []byte(`","rcmd_reason":"`)
  100. msg12 = []byte(`"},`)
  101. msg13 = []byte(`]}}`)
  102. showInf = binfoc.New(s.c.ShowInfoc)
  103. buf bytes.Buffer
  104. list string
  105. trackID string
  106. )
  107. for {
  108. i, ok := <-s.logCh
  109. if !ok {
  110. log.Warn("infoc proc exit")
  111. return
  112. }
  113. switch l := i.(type) {
  114. case infoc:
  115. if len(l.items) > 0 {
  116. buf.Write(msg1)
  117. buf.WriteString(l.typ)
  118. buf.Write(msg2)
  119. buf.WriteString(l.style)
  120. buf.Write(msg3)
  121. for i, v := range l.items {
  122. if v == nil {
  123. continue
  124. }
  125. if v.TrackID != "" {
  126. trackID = v.TrackID
  127. }
  128. buf.Write(msg4)
  129. buf.WriteString(strconv.FormatInt(v.ID, 10))
  130. buf.Write(msg5)
  131. buf.WriteString(strconv.Itoa(i + 1))
  132. buf.Write(msg6)
  133. buf.WriteString(gotoMapID(v.Goto))
  134. buf.Write(msg7)
  135. buf.WriteString(v.Source)
  136. buf.Write(msg8)
  137. buf.WriteString(strconv.FormatInt(v.Tid, 10))
  138. buf.Write(msg9)
  139. if v.AvFeature != nil {
  140. buf.Write(v.AvFeature)
  141. } else {
  142. buf.Write([]byte(`""`))
  143. }
  144. buf.Write(msg10)
  145. buf.WriteString("")
  146. buf.Write(msg11)
  147. if v.RcmdReason != nil {
  148. buf.WriteString(v.RcmdReason.Content)
  149. }
  150. buf.Write(msg12)
  151. }
  152. buf.Truncate(buf.Len() - 1)
  153. buf.Write(msg13)
  154. list = buf.String()
  155. buf.Reset()
  156. } else {
  157. list = noItem1 + l.typ + noItem2 + l.style + noItem3
  158. }
  159. showInf.Info(l.ip, l.now, l.api, l.buvid, l.mid, l.client, l.pull, list, l.disid, l.isRcmd, l.build, l.code, string(l.userFeature), l.zoneID, l.adResponse, l.deviceID, l.network, l.newUser, l.flush, l.autoPlay, trackID, l.deviceType)
  160. log.Info("infocproc %s param(mid:%s,buvid:%s,plat:%s,build:%s,isRcmd:%s,code:%s,zone_id:%s,user_feature:%s,ad_response:%s,device_id:%s,network:%s,new_user:%s,flush:%s,autoplay_card:%s,trackid:%s,device_type:%s) response(%s)", l.api, l.mid, l.buvid, l.client, l.build, l.isRcmd, l.code, l.zoneID, l.userFeature, l.adResponse, l.deviceID, l.network, l.newUser, l.flush, l.autoPlay, trackID, l.deviceType, list)
  161. }
  162. }
  163. }
  164. // gotoMapID is.
  165. func gotoMapID(gt string) (id string) {
  166. if gt == model.GotoAv {
  167. id = "1"
  168. } else if gt == model.GotoBangumi {
  169. id = "2"
  170. } else if gt == model.GotoLive {
  171. id = "3"
  172. } else if gt == model.GotoRank {
  173. id = "6"
  174. } else if gt == model.GotoAdAv {
  175. id = "8"
  176. } else if gt == model.GotoAdWeb {
  177. id = "9"
  178. } else if gt == model.GotoBangumiRcmd {
  179. id = "10"
  180. } else if gt == model.GotoLogin {
  181. id = "11"
  182. } else if gt == model.GotoBanner {
  183. id = "13"
  184. } else if gt == model.GotoAdWebS {
  185. id = "14"
  186. } else if gt == model.GotoConverge {
  187. id = "17"
  188. } else if gt == model.GotoSpecial {
  189. id = "18"
  190. } else if gt == model.GotoArticleS {
  191. id = "20"
  192. } else if gt == model.GotoGameDownloadS {
  193. id = "21"
  194. } else if gt == model.GotoShoppingS {
  195. id = "22"
  196. } else if gt == model.GotoAudio {
  197. id = "23"
  198. } else if gt == model.GotoPlayer {
  199. id = "24"
  200. } else if gt == model.GotoSpecialS {
  201. id = "25"
  202. } else if gt == model.GotoAdLarge {
  203. id = "26"
  204. } else if gt == model.GotoPlayerLive {
  205. id = "27"
  206. } else if gt == model.GotoSong {
  207. id = "28"
  208. } else if gt == model.GotoLiveUpRcmd {
  209. id = "29"
  210. } else if gt == model.GotoUpRcmdAv {
  211. id = "30"
  212. } else if gt == model.GotoSubscribe {
  213. id = "31"
  214. } else if gt == model.GotoChannelRcmd {
  215. id = "32"
  216. } else if gt == model.GotoMoe {
  217. id = "33"
  218. } else if gt == model.GotoPGC {
  219. id = "34"
  220. } else if gt == model.GotoSearchSubscribe {
  221. id = "35"
  222. } else {
  223. id = "-1"
  224. }
  225. return
  226. }