viewer.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package data
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/admin/main/up/model/datamodel"
  7. "go-common/app/admin/main/up/service/cache"
  8. "go-common/app/interface/main/creative/model/tag"
  9. "go-common/library/log"
  10. "go-common/library/sync/errgroup"
  11. )
  12. // ViewerBase get up viewer base data.
  13. func (s *Service) ViewerBase(c context.Context, mid int64) (res *datamodel.ViewerBaseInfo, err error) {
  14. dt := getDateLastSunday()
  15. if res, err = s.data.ViewerBase(c, mid, dt); err != nil {
  16. log.Error("get viewer base error, err=%v", err)
  17. return
  18. }
  19. return
  20. }
  21. // ViewerArea get up viewer area data.
  22. func (s *Service) ViewerArea(c context.Context, mid int64) (res *datamodel.ViewerAreaInfo, err error) {
  23. dt := getDateLastSunday()
  24. res, err = s.data.ViewerArea(c, mid, dt)
  25. if err != nil {
  26. log.Error("get viewer area error, err=%v", err)
  27. return
  28. }
  29. return
  30. }
  31. // CacheTrend get trend from mc.
  32. func (s *Service) CacheTrend(c context.Context, mid int64) (res *datamodel.ViewerTrendInfo, err error) {
  33. dt := getDateLastSunday()
  34. if res, err = s.viewerTrend(c, mid, dt); err != nil {
  35. return
  36. }
  37. return
  38. }
  39. //GetTags get tag
  40. func (s *Service) GetTags(c context.Context, ids ...int64) (result map[int64]*tag.Meta) {
  41. var tagMetaMap, leftIDs = cache.GetTagCache(ids...)
  42. if len(leftIDs) > 0 {
  43. if tlist, err := s.dtag.TagList(c, leftIDs); err != nil {
  44. log.Error("trend s.dtag.TagList err(%v)", err)
  45. } else {
  46. for _, v := range tlist {
  47. tagMetaMap[v.TagID] = v
  48. cache.AddTagCache(v)
  49. }
  50. }
  51. }
  52. result = tagMetaMap
  53. return
  54. }
  55. // ViewerTrend get up viewer trend data.
  56. func (s *Service) viewerTrend(c context.Context, mid int64, dt time.Time) (res *datamodel.ViewerTrendInfo, err error) {
  57. ut, err := s.data.ViewerTrend(c, mid, dt)
  58. if err != nil || ut == nil {
  59. log.Error("trend s.data.ViewerTrend err(%v)", err)
  60. return
  61. }
  62. f := []string{"fan", "guest"}
  63. skeys := make([]int, 0) //for tag sort.
  64. tgs := make([]int64, 0) // for request tag name.
  65. res = &datamodel.ViewerTrendInfo{}
  66. var dataMap = make(map[string]*datamodel.ViewerTypeTagInfo)
  67. for _, fk := range f {
  68. td := ut[fk]
  69. vt := &datamodel.ViewerTypeTagInfo{}
  70. if td == nil {
  71. vt.Type = nil
  72. vt.Tag = nil
  73. dataMap[fk] = vt
  74. continue
  75. }
  76. typeMap := make(map[int]*datamodel.ViewerTypeData) //return type map to user.
  77. //deal type for type name.
  78. if td.Ty != nil {
  79. for k, v := range td.Ty {
  80. var tagInfo, ok = cache.VideoUpTypeCache[k]
  81. var name = ""
  82. if ok {
  83. name = tagInfo.Name
  84. }
  85. typeMap[k] = &datamodel.ViewerTypeData{
  86. Tid: k,
  87. Name: name,
  88. Play: v,
  89. }
  90. }
  91. } else {
  92. typeMap = nil
  93. }
  94. // deal tag for tag name.
  95. if td.Tag != nil {
  96. for k, v := range td.Tag {
  97. tgs = append(tgs, v)
  98. skeys = append(skeys, k)
  99. }
  100. var tagMetaMap, leftIDs = cache.GetTagCache(tgs...)
  101. if len(leftIDs) > 0 {
  102. var tlist []*tag.Meta
  103. if tlist, err = s.dtag.TagList(c, leftIDs); err != nil {
  104. log.Error("trend s.dtag.TagList err(%v)", err)
  105. } else {
  106. for _, v := range tlist {
  107. tagMetaMap[v.TagID] = v
  108. cache.AddTagCache(v)
  109. }
  110. }
  111. }
  112. for _, k := range skeys {
  113. var tagID = td.Tag[k]
  114. var tagData = &datamodel.ViewerTagData{
  115. Idx: k,
  116. TagID: int(tagID),
  117. Name: "",
  118. }
  119. if tagMeta, ok := tagMetaMap[tagID]; ok {
  120. tagData.Name = tagMeta.TagName
  121. }
  122. vt.Tag = append(vt.Tag, tagData)
  123. }
  124. }
  125. for _, v := range typeMap {
  126. vt.Type = append(vt.Type, v)
  127. }
  128. dataMap[fk] = vt
  129. }
  130. res.Fans = dataMap["fan"]
  131. res.Guest = dataMap["guest"]
  132. return
  133. }
  134. //ViewData view data for up
  135. type ViewData struct {
  136. Base *datamodel.ViewerBaseInfo `json:"viewer_base"`
  137. Trend *datamodel.ViewerTrendInfo `json:"viewer_trend"`
  138. Area *datamodel.ViewerAreaInfo `json:"viewer_area"`
  139. }
  140. //GetUpViewInfo get view info by arg
  141. func (s *Service) GetUpViewInfo(c context.Context, arg *datamodel.GetUpViewInfoArg) (result *ViewData, err error) {
  142. if arg == nil {
  143. log.Error("arg is nil")
  144. return
  145. }
  146. res, err := s.GetViewData(c, arg.Mid)
  147. result = &res
  148. return
  149. }
  150. //GetViewData get all view data
  151. func (s *Service) GetViewData(c context.Context, mid int64) (result ViewData, err error) {
  152. var group, ctx = errgroup.WithContext(c)
  153. group.Go(func() error {
  154. var e error
  155. result.Base, e = s.ViewerBase(ctx, mid)
  156. if e != nil {
  157. log.Error("get base view err happen, err=%v", e)
  158. }
  159. return nil
  160. })
  161. group.Go(func() error {
  162. var e error
  163. result.Area, e = s.ViewerArea(ctx, mid)
  164. if e != nil {
  165. log.Error("get area view err happen, err=%v", e)
  166. }
  167. return nil
  168. })
  169. group.Go(func() error {
  170. var e error
  171. result.Trend, e = s.CacheTrend(ctx, mid)
  172. if e != nil {
  173. log.Error("get trend view err happen, err=%v", e)
  174. }
  175. return nil
  176. })
  177. err = group.Wait()
  178. if err != nil {
  179. log.Error("get view data error, err=%+v", err)
  180. }
  181. return
  182. }
  183. //GetFansSummary get fan summary
  184. func (s *Service) GetFansSummary(c context.Context, arg *datamodel.GetFansSummaryArg) (result *datamodel.FansSummaryResult, err error) {
  185. result = &datamodel.FansSummaryResult{}
  186. var fanInfo, e = s.data.UpFansAnalysis(c, arg.Mid, datamodel.Thirty)
  187. if e != nil || fanInfo == nil {
  188. err = e
  189. log.Error("get fan analysis err happen, err=%v", e)
  190. return
  191. }
  192. result.FanSummary = fanInfo.Summary
  193. return
  194. }
  195. //GetRelationFansDay get fan day history
  196. func (s *Service) GetRelationFansDay(c context.Context, arg *datamodel.GetRelationFansHistoryArg) (result *datamodel.GetRelationFansHistoryResult, err error) {
  197. result = &datamodel.GetRelationFansHistoryResult{}
  198. if arg.DataType == 0 {
  199. arg.DataType = datamodel.DataType30Day
  200. }
  201. switch arg.DataType {
  202. case datamodel.DataType30Day:
  203. result.RelationFanHistoryData, err = s.data.RelationFansDay(c, arg.Mid)
  204. case datamodel.DataTypeMonth:
  205. result.RelationFanHistoryData, err = s.data.RelationFansMonth(c, arg.Mid)
  206. default:
  207. err = fmt.Errorf("invalid data type(%d)", arg.DataType)
  208. }
  209. if err != nil {
  210. log.Error("get relation fans day fail, err=%v", err)
  211. return
  212. }
  213. return
  214. }