click.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package service
  2. import (
  3. "context"
  4. "strings"
  5. "sync/atomic"
  6. "time"
  7. "go-common/app/job/main/click/model"
  8. "go-common/library/log"
  9. )
  10. func (s *Service) isAllow(ctx context.Context, c *model.ClickMsg) (rtype int8, err error) {
  11. var (
  12. f *model.Forbid
  13. ok bool
  14. duration int64
  15. )
  16. rtype = model.LogTypeForNotUse
  17. // 自动播放的恶心逻辑 开始
  18. if c.Plat == model.PlatForAutoPlayAndroid || c.Plat == model.PlatForAutoPlayInlineAndroid || c.Plat == model.PlatForAutoPlayIOS || c.Plat == model.PlafForAutoPlayInlineIOS ||
  19. strings.Contains(c.UserAgent, "(inline_play_begin)") { // plat的逻辑更换为UA中添加(inline_play_begin)
  20. log.Warn("no count! hit autoplay plat(%d) aid(%d)", c.Plat, c.AID)
  21. rtype = model.LogTypeForInlineBegin // 2
  22. if c.Buvid != "" {
  23. if err = s.setRealDid(ctx, c.Buvid, c.AID, c.Did); err != nil {
  24. log.Error("s.setRealDid(%s, %s) error(%v)", c.Buvid, c.Did, err)
  25. return
  26. }
  27. }
  28. return
  29. }
  30. // 自动播放的恶心逻辑 结束
  31. if c.MID > 0 {
  32. if _, ok := s.forbidMids[c.MID]; ok {
  33. log.Warn("mid(%d) forbidden", c.MID)
  34. return
  35. }
  36. }
  37. if f, ok = s.forbids[c.AID][c.Plat]; ok {
  38. if f.Lv == -2 && (strings.HasSuffix(c.UserAgent, "(no_accesskey)") || c.MID == 0) {
  39. log.Warn("no count! hit no_accesskey! agent(%s) aid(%d) mid(%d) plat(%d)", c.UserAgent, c.AID, c.MID, c.Plat)
  40. return
  41. } else if f.Lv == -1 && c.MID == 0 {
  42. // 游客不计算点击数
  43. log.Warn("no count! hit forbid_lv(%d) mid(%d)", f.Lv, c.MID)
  44. return
  45. } else if f.Lv >= 0 && (c.Lv <= f.Lv || c.MID == 0) {
  46. // 游客和低于锁定等级的不计算点击数
  47. return
  48. }
  49. }
  50. if c.EpID > 0 {
  51. if err = s.checkEpAvRelation(ctx, c.AID, c.EpID, c.SeasonType); err != nil {
  52. log.Error("s.getOid(%d, %d, %d) error(%v)", c.AID, c.EpID, c.SeasonType)
  53. return
  54. }
  55. }
  56. if !s.canCount(ctx, c.AID, c.EpID, c.IP, c.STime, c.Did) {
  57. log.Warn("same ip(%s) and av(%d) replay", c.IP, c.AID)
  58. return
  59. }
  60. duration = s.ArcDuration(ctx, c.AID)
  61. if s.isReplay(ctx, c.MID, c.AID, c.Did, duration) {
  62. log.Warn("mid(%d) bvid(%s) aid(%d) epid(%d) isPGC(%v) gapTime(%d) is replay", c.MID, c.Did, c.AID, c.EpID, c.EpID > 0, duration)
  63. return
  64. }
  65. rtype = model.LogTypeForTurly // 1
  66. return
  67. }
  68. func (s *Service) checkEpAvRelation(ctx context.Context, aid, epid int64, seasonType int) (err error) {
  69. s.etamMutex.RLock()
  70. _, ok := s.eTam[epid]
  71. s.etamMutex.RUnlock()
  72. if ok {
  73. return
  74. }
  75. var isLegal bool
  76. if isLegal, err = s.db.IsLegal(ctx, aid, epid, seasonType); err != nil {
  77. // 接口请求失败时按ugc维度走
  78. err = nil
  79. } else if !isLegal {
  80. // epid not exist
  81. log.Error("aid(%d) epid(%d) type(%d) not exist", aid, epid, seasonType)
  82. return
  83. }
  84. s.etamMutex.Lock()
  85. s.eTam[epid] = aid
  86. s.etamMutex.Unlock()
  87. return
  88. }
  89. // 播放计数主方法
  90. func (s *Service) countClick(ctx context.Context, msg *model.ClickMsg, i int64) (err error) {
  91. var (
  92. ci *model.ClickInfo
  93. ok bool
  94. now = time.Now().Unix()
  95. )
  96. if msg == nil {
  97. log.Info("svr close s.aidMap length is %d", len(s.aidMap[i]))
  98. for _, ci = range s.aidMap[i] {
  99. // 反正这些视频点击数很多,不差这些,照顾小透明
  100. if ci.GetSum() > 100 {
  101. continue
  102. }
  103. s.upClick(ctx, ci)
  104. }
  105. return
  106. }
  107. idx := msg.AID % s.c.ChanNum
  108. if atomic.LoadInt64(&s.lockedMap[idx]) == _locked {
  109. log.Info("locking aidMap[%d] current length(%d)", idx, len(s.aidMap[idx]))
  110. for _, ci := range s.aidMap[idx] {
  111. if ci.GetSum() > 100 {
  112. continue
  113. }
  114. s.upClick(ctx, ci)
  115. delete(s.aidMap[idx], ci.Aid)
  116. time.Sleep(1 * time.Millisecond)
  117. }
  118. atomic.StoreInt64(&s.lockedMap[idx], _unLock)
  119. log.Info("unlocked aidMap[%d] current length(%d)", idx, len(s.aidMap[idx]))
  120. }
  121. if ci, ok = s.aidMap[idx][msg.AID]; !ok {
  122. if ci, err = s.db.Click(ctx, msg.AID); err != nil {
  123. log.Error("s.db.Click(%d) error(%v)", msg.AID, err)
  124. return
  125. }
  126. if ci == nil {
  127. if _, err = s.db.AddClick(ctx, msg.AID, 0, 0, 0, 0, 0, 0); err != nil {
  128. log.Error("s.db.AddClick(%d) error(%v)", msg.AID, err)
  129. return
  130. }
  131. ci = &model.ClickInfo{Aid: msg.AID}
  132. }
  133. ci.Ready(now)
  134. s.aidMap[idx][msg.AID] = ci
  135. }
  136. switch msg.Plat {
  137. case 0:
  138. ci.Web++
  139. case 1:
  140. ci.H5++
  141. case 2:
  142. ci.Outer++
  143. case 3:
  144. ci.Ios++
  145. case 4:
  146. ci.Android++
  147. case 5:
  148. ci.AndroidTV++
  149. }
  150. if ci.Sum == 0 || now-ci.LastChangeTime > s.c.LastChangeTime {
  151. if err = s.upClick(ctx, ci); err != nil {
  152. log.Error("s.upClick(%v) error(%v)", ci, err)
  153. return
  154. }
  155. log.Info("truly add click message(%+v)", msg)
  156. if now-ci.LastChangeTime > s.c.ReleaseTime || ci.Aid == s.c.BnjMainAid || ci.NeedRelease() {
  157. delete(s.aidMap[idx], msg.AID)
  158. return
  159. }
  160. ci.Ready(now)
  161. }
  162. return
  163. }
  164. func (s *Service) upClick(c context.Context, ci *model.ClickInfo) (err error) {
  165. if _, err = s.db.UpClick(c, ci); err != nil {
  166. log.Error("s.db.UpClick(%+v) error(%v)", ci, err)
  167. return
  168. }
  169. s.busChan <- &model.StatMsg{AID: ci.Aid, Click: int(ci.Sum + ci.GetSum())}
  170. // 拜年祭需求 单品视频的播放数算进主视频
  171. if _, ok := s.bnjListAidMap[ci.Aid]; ok {
  172. bnj := &model.ClickInfo{
  173. Aid: s.c.BnjMainAid,
  174. Web: ci.Web,
  175. H5: ci.H5,
  176. Outer: ci.Outer,
  177. Ios: ci.Ios,
  178. Android: ci.Android,
  179. AndroidTV: ci.AndroidTV,
  180. }
  181. if _, err = s.db.UpClick(c, bnj); err != nil {
  182. log.Error("s.db.UpClick(%d) error(%v)", s.c.BnjMainAid, err)
  183. return
  184. }
  185. log.Info("bnjaid(%d) Forced to increase click(%v) by relateAid(%d)", s.c.BnjMainAid, bnj, ci.Aid)
  186. }
  187. // 拜年祭需求 单品视频的播放数算进主视频
  188. return
  189. }
  190. // SetSpecial http set special aid click
  191. func (s *Service) SetSpecial(c context.Context, aid, num int64, tp string) (err error) {
  192. _, err = s.db.UpSpecial(c, aid, tp, num)
  193. return
  194. }