contain.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package view
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/tv/model"
  6. "go-common/app/interface/main/tv/model/view"
  7. arcwar "go-common/app/service/main/archive/api"
  8. "go-common/app/service/main/archive/model/archive"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. "go-common/library/sync/errgroup"
  12. )
  13. var (
  14. _rate = map[int]int64{15: 464, 16: 464, 32: 1028, 48: 1328, 64: 2192, 74: 3192, 80: 3192, 112: 6192, 116: 6192, 66: 1820}
  15. )
  16. func initPage(v *arcwar.Page, isBangumi bool) (page *view.Page) {
  17. page = &view.Page{}
  18. metas := make([]*view.Meta, 0, 4)
  19. for q, r := range _rate {
  20. meta := &view.Meta{
  21. Quality: q,
  22. Size: int64(float64(r*v.Duration) * 1.1 / 8.0),
  23. }
  24. metas = append(metas, meta)
  25. }
  26. if isBangumi {
  27. v.From = "bangumi"
  28. }
  29. page.Page = v
  30. page.Metas = metas
  31. return
  32. }
  33. func (s *Service) initPages(c context.Context, vs *view.Static, ap []*arcwar.Page) (err error) {
  34. var (
  35. cids []int64
  36. pages = make([]*view.Page, 0, len(ap))
  37. vsAuth map[int64]*model.VideoCMS
  38. isBangumi = vs.AttrVal(archive.AttrBitIsBangumi) == archive.AttrYes
  39. emptyArc = true
  40. )
  41. for _, v := range ap {
  42. cids = append(cids, v.Cid)
  43. }
  44. if vsAuth, err = s.cmsDao.LoadVideosMeta(c, cids); err != nil {
  45. log.Error("initPages LoadVideosMeta Cid %v, Err %v", cids, err)
  46. return
  47. }
  48. for _, v := range ap {
  49. if auth, ok := vsAuth[v.Cid]; ok { // auditing data can't show
  50. if !auth.Auditing() {
  51. pages = append(pages, initPage(v, isBangumi))
  52. }
  53. if auth.CanPlay() {
  54. emptyArc = false
  55. }
  56. }
  57. }
  58. if emptyArc { // if the arc doesn't have any video that can play, we put its valid field to 0 in an asynchronous manner
  59. log.Info("emptyArc add Aid %d, Cids %v", vs.Aid, cids)
  60. s.emptyArcCh <- vs.Aid
  61. }
  62. if len(pages) == 0 {
  63. err = ecode.TvAllDataAuditing
  64. return
  65. }
  66. vs.Pages = pages
  67. return
  68. }
  69. // initRelates init Relates
  70. func (s *Service) initRelates(c context.Context, v *view.View, ip string, now time.Time) {
  71. var (
  72. rls []*view.Relate
  73. err error
  74. )
  75. if rls, err = s.dealRcmdRelate(ctx, v.Aid, ip); err != nil {
  76. log.Error("initRelates For Aid %d, Error %v", v.Aid, err)
  77. return
  78. }
  79. if len(rls) == 0 {
  80. s.prom.Incr("zero_relates")
  81. return
  82. }
  83. v.Relates = rls
  84. }
  85. func (s *Service) dealRcmdRelate(c context.Context, aid int64, ip string) (rls []*view.Relate, err error) {
  86. if rls, err = s.arcDao.RelatesCache(c, aid); err != nil { // mc error
  87. return
  88. }
  89. if len(rls) != 0 {
  90. s.pHit.Incr("relate_cache")
  91. return
  92. }
  93. var (
  94. aids []int64
  95. as map[int64]*arcwar.Arc
  96. arcMetas map[int64]*model.ArcCMS
  97. )
  98. s.pMiss.Incr("relate_cache")
  99. if aids, err = s.arcDao.RelateAids(c, aid, ip); err != nil { // backsource
  100. return
  101. }
  102. if len(aids) == 0 {
  103. return
  104. }
  105. g, errCtx := errgroup.WithContext(c)
  106. g.Go(func() (err error) {
  107. as, err = s.arcDao.Archives(errCtx, aids)
  108. return
  109. })
  110. g.Go(func() (err error) {
  111. arcMetas, err = s.cmsDao.LoadArcsMediaMap(errCtx, aids)
  112. return
  113. })
  114. if err = g.Wait(); err != nil {
  115. log.Error("dealRcmdRelate For Aid %d, Err %v", aid, err)
  116. return
  117. }
  118. for _, aid := range aids {
  119. if a, ok := as[aid]; ok {
  120. // auth, filter can't play ones
  121. if arcCMS, okCMS := arcMetas[aid]; !okCMS {
  122. log.Error("LoadArcsMediaMap Missing Aid %d Info", aid)
  123. continue
  124. } else if canplay, _ := s.cmsDao.UgcErrMsg(arcCMS.Deleted, arcCMS.Result, arcCMS.Valid); !canplay {
  125. log.Warn("LoadArcsMediaMap Aid %d Can't play, Struct %v", aid, arcCMS)
  126. continue
  127. }
  128. // can play, init them
  129. r := &view.Relate{}
  130. r.FromAv(a, "")
  131. rls = append(rls, r)
  132. }
  133. }
  134. if len(rls) != 0 {
  135. s.arcDao.AddRelatesCache(aid, rls)
  136. }
  137. return
  138. }