archive.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package data
  2. import (
  3. "context"
  4. "sort"
  5. "sync"
  6. "time"
  7. "go-common/app/interface/main/creative/model/data"
  8. "go-common/app/service/main/archive/api"
  9. "go-common/library/log"
  10. )
  11. // UpPlaySourceAnalysis get play analysis.
  12. func (s *Service) UpPlaySourceAnalysis(c context.Context, mid int64) (res *data.PlaySource, err error) {
  13. if res, err = s.data.UpPlaySourceAnalysis(c, mid); err != nil {
  14. log.Error("s.data.UpPlaySourceAnalysis err(%v)", err)
  15. }
  16. return
  17. }
  18. // reverse reverses a slice of ints in place.
  19. func reverse(s []int64) {
  20. for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
  21. s[i], s[j] = s[j], s[i]
  22. }
  23. }
  24. // UpArcPlayAnalysis get play list.
  25. func (s *Service) UpArcPlayAnalysis(c context.Context, mid int64, cp int, ip string) (res *data.ArchivePlayList, err error) {
  26. var (
  27. originAIDs []int64
  28. )
  29. res = &data.ArchivePlayList{} //初始化返回值
  30. dt := "0030" //最近30个稿件
  31. originAIDs, err = s.data.UpArcQuery(c, mid, dt, cp)
  32. if err != nil {
  33. log.Error("s.data.UpArcQuery mid(%d)|err(%v)", err, mid)
  34. return
  35. }
  36. log.Info("s.data.UpArcQuery originAIDs(%+v)|len(%d)|mid(%d)", originAIDs, len(originAIDs), mid)
  37. reverse(originAIDs) //反转获取最近aid list.
  38. aids := make([]int64, 0, 30)
  39. aidsMap := make(map[int]int64)
  40. sortK := make([]int, 0, 30)
  41. for k, aid := range originAIDs { //取最近30个aid
  42. aidsMap[k] = aid
  43. sortK = append(sortK, k)
  44. if len(sortK) == 30 {
  45. break
  46. }
  47. }
  48. sort.Slice(sortK, func(i, j int) bool { //30个aid索引从小到大排列
  49. return sortK[i] < sortK[j]
  50. })
  51. for _, ak := range sortK {
  52. if aid, ok := aidsMap[ak]; ok {
  53. aids = append(aids, aid)
  54. }
  55. }
  56. count := len(aids)
  57. log.Info("s.data.UpArcQuery aids(%+v)|len(%d)|mid(%d)", aids, count, mid)
  58. if count == 0 {
  59. return
  60. }
  61. aps := make([]*data.ArchivePlay, 0, count)
  62. apsMap := make(map[int64]*data.ArchivePlay, count)
  63. var (
  64. wg sync.WaitGroup
  65. l sync.RWMutex
  66. avm map[int64]*api.Arc
  67. )
  68. start := time.Now()
  69. for _, aid := range aids { //并发获取aid对应的播放信息
  70. wg.Add(1)
  71. go func(aid int64) {
  72. defer wg.Done()
  73. start = time.Now()
  74. var ap *data.ArchivePlay
  75. if ap, err = s.data.UpArcPlayAnalysis(context.Background(), aid); err != nil {
  76. log.Error("s.data.UpArcPlayAnalysis err(%v)", err)
  77. }
  78. elapsed := time.Since(start)
  79. log.Info("s.data.UpArcPlayAnalysis ap(%+v) elapsed(%v)", ap, elapsed)
  80. if ap != nil {
  81. l.Lock()
  82. apsMap[aid] = ap
  83. l.Unlock()
  84. }
  85. }(aid)
  86. }
  87. wg.Add(1)
  88. go func() {
  89. defer wg.Done()
  90. if avm, err = s.arc.Archives(c, aids, ip); err != nil {
  91. log.Error("s.arc.Archives mid(%d)|aids(%+v)|ip(%s)|err(%v)", mid, aids, ip, err)
  92. err = nil
  93. }
  94. }()
  95. wg.Wait()
  96. elapsed := time.Since(start)
  97. log.Info("s.data.UpArcPlayAnalysis aids(%+v)|apsMap(%+v)|len(apsMap|%d)|len(avm|%d)|elapsed(%v)", apsMap, aids, len(avm), len(apsMap), elapsed)
  98. for _, aid := range aids {
  99. if ap, ok := apsMap[aid]; ok {
  100. if av, ok := avm[aid]; ok && av != nil && ap != nil {
  101. ap.Title = av.Title
  102. }
  103. aps = append(aps, ap)
  104. }
  105. }
  106. sort.Slice(aps, func(i, j int) bool { //30个aid按创建时间从小到大排列
  107. return aps[i].CTime < aps[j].CTime
  108. })
  109. res.ArcPlayList = aps
  110. return
  111. }
  112. // ThirtyDayArchive for Play/Dm/Reply/Fav/Share/Elec/Coin for 30 days.
  113. func (s *Service) ThirtyDayArchive(c context.Context, mid int64, ty int8) (res []*data.ThirtyDay, err error) {
  114. tyStr, _ := data.IncrTy(ty)
  115. if res, err = s.data.ThirtyDayArchiveCache(c, mid, tyStr); err != nil && len(res) != 0 {
  116. log.Info("creatorDataArchive mid(%d) type(%d) cache hit", mid, ty)
  117. return
  118. }
  119. if res, err = s.data.ThirtyDayArchive(c, mid, ty); len(res) != 0 {
  120. timeSlice := make([]int64, 0, 30)
  121. resMap := make(map[int64]int64)
  122. for i := 30; i > 0; i-- { //从昨天往前推算30天生成30天对应的时间戳
  123. var tm time.Time
  124. dt := time.Now().AddDate(0, 0, -1-i).Add(-12 * time.Hour).Format("20060102")
  125. tm, err = time.Parse("20060102", dt)
  126. if err != nil {
  127. log.Error("time.Parse error(%v)", err)
  128. return
  129. }
  130. t := tm.Unix()
  131. timeSlice = append(timeSlice, t)
  132. }
  133. for _, v := range res { //映射原始时间戳集合
  134. resMap[v.DateKey] = v.DateKey
  135. }
  136. if len(res) < 30 { //如果当前的数据不够30条则补齐30条
  137. for _, v := range timeSlice {
  138. if _, ok := resMap[v]; !ok { //当前时间戳不在原始时间戳集合存在则使用生成的时间戳
  139. td := &data.ThirtyDay{
  140. DateKey: v,
  141. }
  142. res = append(res, td)
  143. }
  144. if len(res) == 30 { //添加到30条为止
  145. break
  146. }
  147. }
  148. log.Info("s.data.ThirtyDayArchive append mid(%d) type(%d) return data(%+v)", mid, ty, res)
  149. }
  150. tds := make([]*data.ThirtyDay, 0, 30)
  151. skeys := make([]int64, 0, 30)
  152. tdMap := make(map[int64]int64)
  153. for _, v := range res {
  154. if v != nil {
  155. tdMap[v.DateKey] = v.TotalIncr //设置时间戳和动态数据的键值对
  156. skeys = append(skeys, v.DateKey) //获取时间戳集合
  157. }
  158. }
  159. sort.Slice(skeys, func(i, j int) bool { //对时间戳做降序排列
  160. return skeys[i] > skeys[j]
  161. })
  162. for _, k := range skeys { //按照排序好的时间戳组装最终的返回数据
  163. if v, ok := tdMap[k]; ok {
  164. td := &data.ThirtyDay{}
  165. td.DateKey = k
  166. td.TotalIncr = v
  167. tds = append(tds, td)
  168. if len(tds) == 30 { //只取30条
  169. break
  170. }
  171. }
  172. }
  173. log.Info("s.data.ThirtyDayArchive mid(%d) type(%d) cache miss res(%+v)", mid, ty, tds)
  174. res = tds
  175. s.data.AddCache(func() {
  176. s.data.AddThirtyDayArchiveCache(context.Background(), mid, tyStr, tds)
  177. })
  178. }
  179. return
  180. }