statistics.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. package service
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "strconv"
  7. "time"
  8. "go-common/app/job/main/up-rating/model"
  9. "go-common/library/log"
  10. xtime "go-common/library/time"
  11. "golang.org/x/sync/errgroup"
  12. )
  13. const (
  14. // TotalType total type
  15. TotalType int = iota
  16. // CreativeType creative type
  17. CreativeType
  18. // InfluenceType influence type
  19. InfluenceType
  20. // CreditType Credit type
  21. CreditType
  22. )
  23. var (
  24. _offset int64 = 10
  25. _sumTotal int64
  26. _creativeTotal int64
  27. _influenceTotal int64
  28. _creditTotal int64
  29. )
  30. func initSection(min, max, section int64, ctype int, tagID int64, date xtime.Time) *model.RatingStatis {
  31. return &model.RatingStatis{
  32. Section: section,
  33. Tips: fmt.Sprintf("\"%d-%d\"", min, max),
  34. TagID: tagID,
  35. CDate: date,
  36. CType: ctype,
  37. }
  38. }
  39. func initSections(totalScore, tagID int64, ctype int, date xtime.Time) (statis []*model.RatingStatis) {
  40. var (
  41. idx int64
  42. )
  43. statis = make([]*model.RatingStatis, totalScore/_offset)
  44. for idx*_offset < totalScore {
  45. statis[idx] = initSection(idx*_offset, (idx+1)*_offset, idx, ctype, tagID, date)
  46. idx++
  47. }
  48. return
  49. }
  50. // RunStatistics run up rating statistics
  51. func (s *Service) RunStatistics(c context.Context, date time.Time) (err error) {
  52. err = s.initTotalScore(c)
  53. if err != nil {
  54. log.Error("s.initTotalScore error(%v)", err)
  55. return
  56. }
  57. err = s.delStatistics(c, date)
  58. if err != nil {
  59. log.Error("s.delStatistics error(%v)", err)
  60. return
  61. }
  62. err = s.statistics(c, date)
  63. if err != nil {
  64. log.Error("s.scoreStatistics error(%v)", err)
  65. }
  66. return
  67. }
  68. func (s *Service) initTotalScore(c context.Context) (err error) {
  69. params, err := s.getAllParamter(c)
  70. if err != nil {
  71. log.Error("s.getAllParamter error(%v)", err)
  72. return
  73. }
  74. _sumTotal = params.WCSR + params.HR + params.WISR
  75. _creativeTotal = params.WCSR
  76. _influenceTotal = params.WISR
  77. _creditTotal = params.HR
  78. return
  79. }
  80. func (s *Service) delStatistics(c context.Context, date time.Time) (err error) {
  81. err = s.delRatingCom(c, "up_rating_statistics", date)
  82. if err != nil {
  83. return
  84. }
  85. err = s.delRatingCom(c, "up_rating_top", date)
  86. return
  87. }
  88. // delRatingCom del com
  89. func (s *Service) delRatingCom(c context.Context, table string, date time.Time) (err error) {
  90. for {
  91. var rows int64
  92. rows, err = s.dao.DelRatingCom(c, table, date, _limit)
  93. if err != nil {
  94. return
  95. }
  96. if rows == 0 {
  97. break
  98. }
  99. }
  100. return
  101. }
  102. func (s *Service) statistics(c context.Context, date time.Time) (err error) {
  103. var (
  104. readGroup errgroup.Group
  105. sourceCh = make(chan []*model.Rating, _limit)
  106. statisCh = make(chan []*model.Rating, _limit)
  107. topCh = make(chan []*model.Rating, _limit)
  108. sections map[int]map[int64][]*model.RatingStatis
  109. topRating map[int]map[int64]*RatingHeap
  110. )
  111. baseInfo, err := s.BaseTotal(c, date)
  112. if err != nil {
  113. log.Error("s.BaseTotal error(%v)", err)
  114. return
  115. }
  116. // get rating info
  117. readGroup.Go(func() (err error) {
  118. err = s.RatingInfos(c, date, sourceCh)
  119. if err != nil {
  120. log.Error("s.RatingInfos error(%v)", err)
  121. }
  122. return
  123. })
  124. // dispatch
  125. readGroup.Go(func() (err error) {
  126. defer func() {
  127. close(topCh)
  128. close(statisCh)
  129. }()
  130. for rating := range sourceCh {
  131. statisCh <- rating
  132. topCh <- rating
  133. }
  134. return
  135. })
  136. // top
  137. readGroup.Go(func() (err error) {
  138. topRating, err = s.ratingTop(c, date, topCh)
  139. if err != nil {
  140. log.Error("s.RatingTop error(%v)", err)
  141. }
  142. return
  143. })
  144. // statis
  145. readGroup.Go(func() (err error) {
  146. sections, err = s.scoreStatistics(c, date, statisCh, baseInfo)
  147. if err != nil {
  148. log.Error("s.scoreStatistics error(%v)", err)
  149. }
  150. return
  151. })
  152. if err = readGroup.Wait(); err != nil {
  153. log.Error("run readGroup.Wait error(%v)", err)
  154. return
  155. }
  156. // persistent
  157. var writeGroup errgroup.Group
  158. //up_rating_statistics
  159. writeGroup.Go(func() (err error) {
  160. err = s.insertSections(c, sections)
  161. if err != nil {
  162. log.Error("s.insertSections error(%v)", err)
  163. }
  164. return
  165. })
  166. // up_rating_top
  167. writeGroup.Go(func() (err error) {
  168. _, err = s.insertTopRating(c, date, topRating, baseInfo)
  169. if err != nil {
  170. log.Error("s.insertSections error(%v)", err)
  171. }
  172. return
  173. })
  174. if err = writeGroup.Wait(); err != nil {
  175. log.Error("run writeGroup.Wait error(%v)", err)
  176. }
  177. return
  178. }
  179. func (s *Service) scoreStatistics(c context.Context, date time.Time, source chan []*model.Rating, baseInfo map[int64]*model.BaseInfo) (sections map[int]map[int64][]*model.RatingStatis, err error) {
  180. sections = make(map[int]map[int64][]*model.RatingStatis) // map[ctype][tagID][]*model.RatingStatis
  181. sections[TotalType] = make(map[int64][]*model.RatingStatis)
  182. sections[CreativeType] = make(map[int64][]*model.RatingStatis)
  183. sections[InfluenceType] = make(map[int64][]*model.RatingStatis)
  184. sections[CreditType] = make(map[int64][]*model.RatingStatis)
  185. for rating := range source {
  186. for _, r := range rating {
  187. statisScoreCtype(TotalType, r.CreativityScore+r.InfluenceScore+r.CreditScore, _sumTotal, sections, date, r, baseInfo[r.MID])
  188. statisScoreCtype(CreativeType, r.CreativityScore, _creativeTotal, sections, date, r, baseInfo[r.MID])
  189. statisScoreCtype(InfluenceType, r.InfluenceScore, _influenceTotal, sections, date, r, baseInfo[r.MID])
  190. statisScoreCtype(CreditType, r.CreditScore, _creditTotal, sections, date, r, baseInfo[r.MID])
  191. }
  192. }
  193. return
  194. }
  195. func statisScoreCtype(ctype int, score, totalScore int64, sections map[int]map[int64][]*model.RatingStatis, date time.Time, rate *model.Rating, base *model.BaseInfo) {
  196. if _, ok := sections[ctype][rate.TagID]; !ok {
  197. sections[ctype][rate.TagID] = initSections(totalScore, rate.TagID, ctype, xtime.Time(date.Unix()))
  198. }
  199. idx := score / _offset
  200. if idx >= int64(len(sections[ctype][rate.TagID])) {
  201. idx = int64(len(sections[ctype][rate.TagID]) - 1)
  202. }
  203. sections[ctype][rate.TagID][idx].Ups++
  204. sections[ctype][rate.TagID][idx].TotalScore += rate.CreativityScore + rate.InfluenceScore + rate.CreditScore
  205. sections[ctype][rate.TagID][idx].CreativityScore += rate.CreativityScore
  206. sections[ctype][rate.TagID][idx].InfluenceScore += rate.InfluenceScore
  207. sections[ctype][rate.TagID][idx].CreditScore += rate.CreditScore
  208. if base != nil {
  209. sections[ctype][rate.TagID][idx].Fans += base.TotalFans
  210. sections[ctype][rate.TagID][idx].Avs += base.TotalAvs
  211. sections[ctype][rate.TagID][idx].Coin += base.TotalCoin
  212. sections[ctype][rate.TagID][idx].Play += base.TotalPlay
  213. }
  214. }
  215. func (s *Service) insertSections(c context.Context, sections map[int]map[int64][]*model.RatingStatis) (err error) {
  216. for ctype, tags := range sections {
  217. for tagID, statis := range tags {
  218. _, err = s.insertRatingStatis(c, ctype, tagID, statis)
  219. if err != nil {
  220. return
  221. }
  222. }
  223. }
  224. return
  225. }
  226. func (s *Service) insertRatingStatis(c context.Context, ctype int, tagID int64, statis []*model.RatingStatis) (rows int64, err error) {
  227. return s.dao.InsertRatingStatis(c, assembleRatingStatis(c, ctype, tagID, statis))
  228. }
  229. func assembleRatingStatis(c context.Context, ctype int, tagID int64, statis []*model.RatingStatis) (vals string) {
  230. var buf bytes.Buffer
  231. for _, s := range statis {
  232. buf.WriteString("(")
  233. buf.WriteString(strconv.FormatInt(s.Ups, 10))
  234. buf.WriteByte(',')
  235. buf.WriteString(strconv.FormatInt(s.Section, 10))
  236. buf.WriteByte(',')
  237. buf.WriteString(s.Tips)
  238. buf.WriteByte(',')
  239. buf.WriteString(strconv.FormatInt(s.TotalScore, 10))
  240. buf.WriteByte(',')
  241. buf.WriteString(strconv.FormatInt(s.CreativityScore, 10))
  242. buf.WriteByte(',')
  243. buf.WriteString(strconv.FormatInt(s.InfluenceScore, 10))
  244. buf.WriteByte(',')
  245. buf.WriteString(strconv.FormatInt(s.CreditScore, 10))
  246. buf.WriteByte(',')
  247. buf.WriteString(strconv.FormatInt(s.Fans, 10))
  248. buf.WriteByte(',')
  249. buf.WriteString(strconv.FormatInt(s.Avs, 10))
  250. buf.WriteByte(',')
  251. buf.WriteString(strconv.FormatInt(s.Coin, 10))
  252. buf.WriteByte(',')
  253. buf.WriteString(strconv.FormatInt(s.Play, 10))
  254. buf.WriteByte(',')
  255. buf.WriteString(strconv.FormatInt(s.TagID, 10))
  256. buf.WriteByte(',')
  257. buf.WriteString(strconv.Itoa(s.CType))
  258. buf.WriteByte(',')
  259. buf.WriteString("'" + s.CDate.Time().Format(_layout) + "'")
  260. buf.WriteString(")")
  261. buf.WriteByte(',')
  262. }
  263. if buf.Len() > 0 {
  264. buf.Truncate(buf.Len() - 1)
  265. }
  266. vals = buf.String()
  267. buf.Reset()
  268. return
  269. }