statistics.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/admin/main/mcn/model"
  7. dtmdl "go-common/app/interface/main/mcn/model/datamodel"
  8. accgrpc "go-common/app/service/main/account/api"
  9. arcgrpc "go-common/app/service/main/archive/api"
  10. "go-common/library/log"
  11. "go-common/library/sync/errgroup"
  12. xtime "go-common/library/time"
  13. "github.com/pkg/errors"
  14. )
  15. // ArcTopDataStatistics .
  16. func (s *Service) ArcTopDataStatistics(c context.Context, arg *model.McnGetRankReq) (res *model.McnGetRankUpFansReply, err error) {
  17. return s.dao.ArcTopDataStatistics(c, arg)
  18. }
  19. // McnsTotalDatas .
  20. func (s *Service) McnsTotalDatas(c context.Context, arg *model.TotalMcnDataReq) (res *model.TotalMcnDataInfo, err error) {
  21. var (
  22. signUpsTotal, FanTotal, videoupTotal, playTotal int64
  23. mids, fansMids, ArcsMids, avids, tids, arcsTids, typeTids []int64
  24. m *model.McnDataOverview
  25. mrf map[int8][]*model.McnRankFansOverview
  26. ras []*model.McnRankArchiveLikesOverview
  27. mmd map[string][]*model.McnDataTypeSummary
  28. )
  29. td := new(thirdDataMap)
  30. res = new(model.TotalMcnDataInfo)
  31. res.TopInfo = new(model.McnDataTopInfo)
  32. res.TypesInfo = new(model.McnDataTypesInfo)
  33. date := xtime.Time(time.Date(arg.Date.Time().Year(), arg.Date.Time().Month(), arg.Date.Time().Day()-1, 0, 0, 0, 0, time.Local).Unix())
  34. if m, err = s.dao.McnDataOverview(c, date); err != nil {
  35. return
  36. }
  37. res.BaseInfo = m
  38. if mrf, fansMids, err = s.dao.McnRankFansOverview(c, model.DataTypeDay, date, model.TopDataLenth); err != nil {
  39. return
  40. }
  41. if ras, ArcsMids, avids, arcsTids, err = s.dao.McnRankArchiveLikesOverview(c, model.DataTypeDay, date, model.TopDataLenth); err != nil {
  42. return
  43. }
  44. if mmd, typeTids, err = s.dao.McnDataTypeSummary(c, date); err != nil {
  45. return
  46. }
  47. mids = append(mids, fansMids...)
  48. mids = append(mids, ArcsMids...)
  49. tids = append(tids, arcsTids...)
  50. tids = append(tids, typeTids...)
  51. if td.infosReply, err = s.accGRPC.Infos3(c, &accgrpc.MidsReq{Mids: mids}); err != nil {
  52. log.Error("s.accGRPC.Infos3(%+v) error(%+v)", mids, err)
  53. err = nil
  54. }
  55. if td.archivesReply, err = s.arcGRPC.Arcs(c, &arcgrpc.ArcsRequest{Aids: avids}); err != nil {
  56. log.Error("s.arcGRPC.Arcs(%+v) error(%+v)", avids, err)
  57. err = nil
  58. }
  59. td.tpNames = s.videoup.GetTidName(tids)
  60. for view, data := range mrf {
  61. for _, v := range data {
  62. var (
  63. rateIncr int64
  64. name, _, _, _ = td.getTypeName(v.Mid, 0, 0, 0)
  65. )
  66. if v.Fans != 0 {
  67. rateIncr = (10000 * v.FansIncr) / v.Fans
  68. }
  69. fr := &model.FansRankIncr{Mid: v.Mid, Name: name, Rank: v.Rank, FansIncr: v.FansIncr, Fans: v.Fans, RateIncr: rateIncr, SignID: v.SignID}
  70. switch model.DataViewFansTop(view) {
  71. case model.McnFansIncr:
  72. res.TopInfo.McnFansIncr = append(res.TopInfo.McnFansIncr, fr)
  73. case model.McnFansIncrRate:
  74. res.TopInfo.McnFansRateIncr = append(res.TopInfo.McnFansRateIncr, fr)
  75. case model.UpFansIncr:
  76. res.TopInfo.UpFansIncr = append(res.TopInfo.UpFansIncr, fr)
  77. case model.UpFansIncrRate:
  78. res.TopInfo.UpFansRateIncr = append(res.TopInfo.UpFansRateIncr, fr)
  79. }
  80. }
  81. }
  82. for _, v := range ras {
  83. var mcnName, upName, avTitle, tpName = td.getTypeName(v.McnMid, v.UpMid, v.Avid, int64(v.Tid))
  84. lr := &model.LikesRankIncr{McnMid: v.McnMid, McnName: mcnName,
  85. UpMid: v.UpMid, UpName: upName, AVID: v.Avid, AVTitle: avTitle,
  86. TID: v.Tid, TypeName: tpName, LikesIncr: v.Likes, PlayIncr: v.Plays, SignID: v.SignID}
  87. res.TopInfo.ArcLikesIncr = append(res.TopInfo.ArcLikesIncr, lr)
  88. }
  89. for vt, data := range mmd {
  90. for _, v := range data {
  91. var _, _, _, tpName = td.getTypeName(0, 0, 0, int64(v.Tid))
  92. dt := &model.DataTypes{TID: v.Tid, TypeName: tpName, Amount: v.Amount}
  93. switch vt {
  94. case fmt.Sprintf("%d-%d", model.SignUpsAccumulate, model.DataTypeAccumulate):
  95. signUpsTotal += v.Amount
  96. res.TypesInfo.SignUps = append(res.TypesInfo.SignUps, dt)
  97. case fmt.Sprintf("%d-%d", model.FansIncr, model.DataTypeDay):
  98. FanTotal += v.Amount
  99. res.TypesInfo.FansIncr = append(res.TypesInfo.FansIncr, dt)
  100. case fmt.Sprintf("%d-%d", model.VideoUpsIncr, model.DataTypeDay):
  101. videoupTotal += v.Amount
  102. res.TypesInfo.VideoupIncr = append(res.TypesInfo.VideoupIncr, dt)
  103. case fmt.Sprintf("%d-%d", model.PlaysIncr, model.DataTypeDay):
  104. playTotal += v.Amount
  105. res.TypesInfo.PlayIncr = append(res.TypesInfo.PlayIncr, dt)
  106. }
  107. }
  108. }
  109. statTypeRate(res.TypesInfo.SignUps, signUpsTotal)
  110. statTypeRate(res.TypesInfo.FansIncr, FanTotal)
  111. statTypeRate(res.TypesInfo.VideoupIncr, videoupTotal)
  112. statTypeRate(res.TypesInfo.PlayIncr, playTotal)
  113. return
  114. }
  115. type thirdDataMap struct {
  116. tpNames map[int64]string
  117. infosReply *accgrpc.InfosReply
  118. archivesReply *arcgrpc.ArcsReply
  119. }
  120. func (td *thirdDataMap) getTypeName(mcnMid, upMid, avid, tid int64) (mcnName, upName, avTitle, tpName string) {
  121. if td.infosReply != nil {
  122. infos := td.infosReply.Infos
  123. if info, ok := infos[mcnMid]; ok {
  124. mcnName = info.Name
  125. }
  126. if info, ok := infos[upMid]; ok {
  127. upName = info.Name
  128. }
  129. }
  130. if td.archivesReply != nil {
  131. archives := td.archivesReply.Arcs
  132. if archive, ok := archives[avid]; ok {
  133. avTitle = archive.Title
  134. }
  135. }
  136. tpName = td.tpNames[tid]
  137. return
  138. }
  139. func statTypeRate(dts []*model.DataTypes, total int64) {
  140. for _, v := range dts {
  141. if total != 0 {
  142. v.Rate = (10000 * v.Amount) / total
  143. }
  144. v.Total = total
  145. }
  146. }
  147. // McnFansAnalyze .
  148. func (s *Service) McnFansAnalyze(c context.Context, arg *model.McnCommonReq) (res *model.McnGetMcnFansReply, err error) {
  149. var (
  150. f *dtmdl.DmConMcnFansD
  151. sex *dtmdl.DmConMcnFansSexW
  152. age *dtmdl.DmConMcnFansAgeW
  153. playWay *dtmdl.DmConMcnFansPlayWayW
  154. areas []*dtmdl.DmConMcnFansAreaW
  155. types []*dtmdl.DmConMcnFansTypeW
  156. tags []*dtmdl.DmConMcnFansTagW
  157. g errgroup.Group
  158. )
  159. res = new(model.McnGetMcnFansReply)
  160. g.Go(func() (err error) {
  161. if f, err = s.dao.DataFans(c, arg); err != nil {
  162. log.Error("s.dao.DataFans(%+v) error(%v)", arg, err)
  163. return
  164. }
  165. res.FansOverview = f
  166. return
  167. })
  168. g.Go(func() (err error) {
  169. if sex, age, playWay, err = s.dao.DataFansBaseAttr(c, arg); err != nil {
  170. log.Error("s.dao.DataFansBaseAttr(%+v) error(%v)", arg, err)
  171. return
  172. }
  173. res.FansSex = sex
  174. res.FansAge = age
  175. res.FansPlayWay = playWay
  176. return
  177. })
  178. g.Go(func() (err error) {
  179. if areas, err = s.dao.DataFansArea(c, arg); err != nil {
  180. log.Error("s.dao.DataFansArea(%+v) error(%v)", arg, err)
  181. return
  182. }
  183. res.FansArea = areas
  184. return
  185. })
  186. g.Go(func() (err error) {
  187. if types, err = s.dao.DataFansType(c, arg); err != nil {
  188. log.Error("s.dao.DataFansType(%+v) error(%v)", arg, err)
  189. return
  190. }
  191. res.FansType = types
  192. return
  193. })
  194. g.Go(func() (err error) {
  195. if tags, err = s.dao.DataFansTag(c, arg); err != nil {
  196. log.Error("s.dao.DataFansTag(%+v) error(%v)", arg, err)
  197. return
  198. }
  199. res.FansTag = tags
  200. return
  201. })
  202. if err = g.Wait(); err != nil {
  203. err = errors.WithStack(err)
  204. }
  205. return
  206. }