statistics.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/job/main/reply-feed/model"
  6. "go-common/library/log"
  7. )
  8. func hourNow() int {
  9. return time.Now().Hour()
  10. }
  11. func lastHour() int {
  12. hour := hourNow()
  13. if hour == 0 {
  14. return 23
  15. }
  16. return hour - 1
  17. }
  18. // AddUV ...
  19. func (s *Service) addUV(ctx context.Context, value *model.StatsMsg, isHot bool) {
  20. var action string
  21. switch value.Action {
  22. case model.DatabusActionLike:
  23. action = model.StatisticActionLike
  24. case model.DatabusActionHate:
  25. action = model.StatisticActionHate
  26. case model.DatabusActionReport:
  27. action = model.StatisticActionReport
  28. case model.DatabusActionReply:
  29. if value.Reply.IsRoot() {
  30. action = model.StatisticActionRootReply
  31. } else {
  32. action = model.StatisticActionChildReply
  33. }
  34. }
  35. if action == "" || value.Mid == 0 {
  36. return
  37. }
  38. s.uvQ.Do(ctx, func(ctx context.Context) {
  39. if isHot {
  40. s.dao.AddUV(ctx, action, hourNow(), int(value.Sharding()), value.Mid, model.StatisticKindHot)
  41. }
  42. s.dao.AddUV(ctx, action, hourNow(), int(value.Sharding()), value.Mid, model.StatisticKindTotal)
  43. })
  44. }
  45. // uvStatistics ...
  46. func (s *Service) uvStatistics(ctx context.Context, slots []int, stat *model.StatisticsStat) {
  47. var (
  48. keys []string
  49. lastHour = lastHour()
  50. x, y, z = len(model.StatisticKinds), len(model.StatisticActions), len(slots)
  51. idxMap = make([][][]int, x)
  52. idx int
  53. )
  54. for i, kind := range model.StatisticKinds {
  55. idxMap[i] = make([][]int, y)
  56. for j, action := range model.StatisticActions {
  57. idxMap[i][j] = make([]int, z)
  58. for k, slot := range slots {
  59. keys = append(keys, s.dao.KeyUV(action, lastHour, slot, kind))
  60. idxMap[i][j][k] = idx
  61. idx++
  62. }
  63. }
  64. }
  65. counts, err := s.dao.CountUV(ctx, keys)
  66. if err != nil || len(counts) != len(keys) {
  67. return
  68. }
  69. for i, kind := range model.StatisticKinds {
  70. for j, action := range model.StatisticActions {
  71. for k := range slots {
  72. count := counts[idxMap[i][j][k]]
  73. switch {
  74. case kind == model.StatisticKindTotal:
  75. switch action {
  76. case model.StatisticActionRootReply:
  77. stat.TotalRootUV += count
  78. case model.StatisticActionChildReply:
  79. stat.TotalChildUV += count
  80. case model.StatisticActionLike:
  81. stat.TotalLikeUV += count
  82. case model.StatisticActionHate:
  83. stat.TotalHateUV += count
  84. case model.StatisticActionReport:
  85. stat.TotalReportUV += count
  86. }
  87. case kind == model.StatisticKindHot:
  88. switch action {
  89. case model.StatisticActionChildReply:
  90. stat.HotChildUV += count
  91. case model.StatisticActionLike:
  92. stat.HotLikeUV += count
  93. case model.StatisticActionHate:
  94. stat.HotHateUV += count
  95. case model.StatisticActionReport:
  96. stat.HotReportUV += count
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. // persistStatistics persist statistics
  104. func (s *Service) persistStatistics() {
  105. ctx := context.Background()
  106. statisticsMap := make(map[string]*model.StatisticsStat)
  107. nameMapping := make(map[string][]int)
  108. s.statisticsLock.RLock()
  109. for slot, stat := range s.statisticsStats {
  110. nameMapping[stat.Name] = append(nameMapping[stat.Name], slot)
  111. s, ok := statisticsMap[stat.Name]
  112. if ok {
  113. statisticsMap[stat.Name] = s.Merge(&stat)
  114. } else {
  115. statisticsMap[stat.Name] = &stat
  116. }
  117. }
  118. s.statisticsLock.RUnlock()
  119. now := time.Now()
  120. year, month, day := now.Date()
  121. date := year*10000 + int(month)*100 + day
  122. hour := now.Hour()
  123. for name, stat := range statisticsMap {
  124. s.uvStatistics(ctx, nameMapping[name], stat)
  125. err := s.dao.UpsertStatistics(ctx, name, date, hour, stat)
  126. var (
  127. retryTimes = 0
  128. maxRetryTimes = 5
  129. )
  130. for err != nil && retryTimes < maxRetryTimes {
  131. time.Sleep(s.bc.Backoff(retryTimes))
  132. err = s.dao.UpsertStatistics(ctx, name, date, hour, stat)
  133. retryTimes++
  134. }
  135. if retryTimes >= maxRetryTimes {
  136. log.Error("upsert statistics error retry reached max retry times.")
  137. }
  138. }
  139. for i := range s.statisticsStats {
  140. reset(&s.statisticsStats[i])
  141. }
  142. log.Warn("reset statistics at %v", now)
  143. }
  144. func reset(stat *model.StatisticsStat) {
  145. stat.HotChildReply = 0
  146. stat.HotHate = 0
  147. stat.HotLike = 0
  148. stat.HotReport = 0
  149. stat.TotalChildReply = 0
  150. stat.TotalRootReply = 0
  151. stat.TotalReport = 0
  152. stat.TotalLike = 0
  153. stat.TotalHate = 0
  154. }