academy.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package service
  2. import (
  3. "context"
  4. "math"
  5. "time"
  6. "go-common/app/interface/openplatform/article/model"
  7. "go-common/app/service/main/archive/api"
  8. "go-common/library/log"
  9. )
  10. //FlushHot for compute archive hot value by time.
  11. func (s *Service) FlushHot(bs int) {
  12. var (
  13. c = context.TODO()
  14. id int64
  15. limit = 30
  16. )
  17. for {
  18. arcs, err := s.aca.Archives(c, id, bs, limit)
  19. if err != nil {
  20. log.Error("s.aca.Archives id(%d) error(%v)", id, err)
  21. return
  22. }
  23. count := len(arcs)
  24. if count == 0 {
  25. id = 0 //重新按定时跑数据
  26. time.Sleep(time.Hour * 1)
  27. continue
  28. }
  29. oids := make([]int64, 0)
  30. for _, a := range arcs {
  31. oids = append(oids, a.OID)
  32. id = a.ID //标记当前处理到的id
  33. }
  34. hots, err := s.computeHotByOIDs(c, oids, bs)
  35. if err != nil {
  36. log.Error("s.aca.Archives id(%d) error(%v)", id, err)
  37. return
  38. }
  39. if s.aca.UPHotByAIDs(c, hots) != nil {
  40. log.Error("s.aca.UPHotByAIDs hots(%+v) error(%v)", hots, err)
  41. return
  42. }
  43. }
  44. }
  45. func (s *Service) computeHotByOIDs(c context.Context, oids []int64, bs int) (res map[int64]int64, err error) {
  46. res = make(map[int64]int64)
  47. if bs == 1 {
  48. arcs, err := s.arc.Archives(c, oids)
  49. if err != nil {
  50. return nil, err
  51. }
  52. stat, err := s.arc.Stats(c, oids)
  53. if err != nil {
  54. log.Error("s.arc.Stats oids(%+v)|business(%d)|error(%v)", oids, bs, err)
  55. return nil, err
  56. }
  57. for _, oid := range oids {
  58. if v, ok := arcs[oid]; ok && v != nil {
  59. if t, ok := stat[oid]; ok && t != nil {
  60. res[oid] = countArcHot(t, int64(v.PubDate))
  61. }
  62. }
  63. }
  64. } else if bs == 2 {
  65. arts, err := s.arc.ArticleMetas(c, oids)
  66. if err != nil {
  67. return nil, err
  68. }
  69. for _, oid := range oids {
  70. if v, ok := arts[oid]; ok && v != nil {
  71. res[oid] = countArtHot(v)
  72. }
  73. }
  74. }
  75. return
  76. }
  77. //countArcHot 视频=硬币*0.4+收藏*0.3+弹幕*0.4+评论*0.4+播放*0.25+点赞*0.4+分享*0.6 最新视频(一天内发布)提权[总值*1.5]
  78. func countArcHot(t *api.Stat, ptime int64) int64 {
  79. if t == nil {
  80. return 0
  81. }
  82. hot := float64(t.Coin)*0.4 +
  83. float64(t.Fav)*0.3 +
  84. float64(t.Danmaku)*0.4 +
  85. float64(t.Reply)*0.4 +
  86. float64(t.View)*0.25 +
  87. float64(t.Like)*0.4 +
  88. float64(t.Share)*0.6
  89. if ptime >= time.Now().AddDate(0, 0, -1).Unix() && ptime <= time.Now().Unix() {
  90. hot *= 1.5
  91. }
  92. return int64(math.Floor(hot))
  93. }
  94. // countArtHot 专栏=硬币*0.4+收藏*0.3+评论*0.4+阅读*0.25+点赞*0.4+分享*0.6 最新专栏(一天内发布)提权[总值*1.5]
  95. func countArtHot(t *model.Meta) int64 {
  96. if t.Stats == nil {
  97. return 0
  98. }
  99. hot := float64(t.Stats.Coin)*0.4 +
  100. float64(t.Stats.Favorite)*0.3 +
  101. float64(t.Stats.Reply)*0.4 +
  102. float64(t.Stats.View)*0.25 +
  103. float64(t.Stats.Like)*0.4 +
  104. float64(t.Stats.Share)*0.6
  105. if int64(t.PublishTime) >= time.Now().AddDate(0, 0, -1).Unix() && int64(t.PublishTime) <= time.Now().Unix() {
  106. hot *= 1.5
  107. }
  108. return int64(math.Floor(hot))
  109. }