stat.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package service
  2. import (
  3. "context"
  4. plamdl "go-common/app/interface/main/playlist/model"
  5. "go-common/app/job/main/playlist/conf"
  6. "go-common/app/job/main/playlist/dao"
  7. "go-common/app/job/main/playlist/model"
  8. "go-common/library/log"
  9. )
  10. func (s *Service) viewproc(i int64) {
  11. defer s.waiter.Done()
  12. var (
  13. c = context.TODO()
  14. ch = s.statCh[i]
  15. )
  16. for {
  17. sm, ok := <-ch
  18. if !ok {
  19. log.Warn("statproc(%d) quit", i)
  20. return
  21. }
  22. // filter view count
  23. if conf.Conf.Job.InterceptOn && sm.Count != nil && *sm.Count > 0 {
  24. if s.intercept(sm) {
  25. log.Info("intercept view count (pid:%d, aid:%d, ip:%s)", sm.ID, sm.Aid, sm.IP)
  26. dao.PromInfo("stat:访问计数拦截")
  27. continue
  28. }
  29. }
  30. s.upStat(c, sm, model.ViewCountType)
  31. }
  32. }
  33. func (s *Service) upStat(c context.Context, sm *model.StatM, tp string) {
  34. // update cache
  35. s.updateCache(c, sm, tp)
  36. // update db
  37. s.updateDB(c, sm, tp)
  38. }
  39. // updateDB update stat in db.
  40. func (s *Service) updateDB(c context.Context, stat *model.StatM, tp string) (err error) {
  41. if _, err = s.dao.Update(context.TODO(), stat, tp); err != nil {
  42. return
  43. }
  44. log.Info("update db success "+tp+" pid(%d) count(%d) ", stat.ID, *stat.Count)
  45. dao.PromInfo("stat:更新计数DB")
  46. return
  47. }
  48. // updateCache update stat in cache
  49. func (s *Service) updateCache(c context.Context, sm *model.StatM, tp string) (err error) {
  50. var (
  51. mid int64
  52. fid int64
  53. st *plamdl.PlStat
  54. stat *plamdl.ArgStats
  55. )
  56. if st, err = s.dao.Stat(c, sm.ID); err != nil {
  57. log.Error("s.dao.Stat(%d) error(%v)", sm.ID, err)
  58. return
  59. }
  60. switch tp {
  61. case model.ViewCountType:
  62. stat = &plamdl.ArgStats{PlStat: &plamdl.PlStat{
  63. ID: sm.ID,
  64. Mid: st.Mid,
  65. Fid: st.Fid,
  66. View: *sm.Count,
  67. Reply: st.Reply,
  68. Fav: st.Fav,
  69. Share: st.Share,
  70. MTime: st.MTime,
  71. }}
  72. case model.FavCountType:
  73. stat = &plamdl.ArgStats{PlStat: &plamdl.PlStat{
  74. ID: sm.ID,
  75. Mid: st.Mid,
  76. Fid: st.Fid,
  77. View: st.View,
  78. Reply: st.Reply,
  79. Fav: *sm.Count,
  80. Share: st.Share,
  81. MTime: st.MTime,
  82. }}
  83. case model.ReplyCountType:
  84. stat = &plamdl.ArgStats{PlStat: &plamdl.PlStat{
  85. ID: sm.ID,
  86. Mid: st.Mid,
  87. Fid: st.Fid,
  88. View: st.View,
  89. Reply: *sm.Count,
  90. Fav: st.Fav,
  91. Share: st.Share,
  92. MTime: st.MTime,
  93. }}
  94. case model.ShareCountType:
  95. stat = &plamdl.ArgStats{PlStat: &plamdl.PlStat{
  96. ID: sm.ID,
  97. Mid: st.Mid,
  98. Fid: st.Fid,
  99. View: st.View,
  100. Reply: st.Reply,
  101. Fav: st.Fav,
  102. Share: *sm.Count,
  103. MTime: st.MTime,
  104. }}
  105. }
  106. if err = s.playlistRPC.SetStat(c, stat); err != nil {
  107. dao.PromError("stat:更新计数缓存",
  108. "s.playlistRPC.SetStat "+tp+" pid(%d) fid(%d) mid(%d) view(%d) favorite(%d) reply(%d) share(%d) error(%v)",
  109. sm.ID, fid, mid, *sm.Count, st.Fav, st.Reply, st.Share, err)
  110. }
  111. log.Info("update cache success "+tp+" pid(%d) aid(%d) fid(%d) mid(%d) view(%d) favorite(%d) reply(%d) share(%d)",
  112. sm.ID, sm.Aid, fid, mid, *sm.Count, st.Fav, st.Reply, st.Share)
  113. dao.PromInfo("stat:更新计数缓存")
  114. return
  115. }
  116. // intercept intercepts illegal views.
  117. func (s *Service) intercept(stat *model.StatM) bool {
  118. return s.dao.Intercept(context.TODO(), stat.ID, stat.Aid, stat.IP)
  119. }