extra_func.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package service
  2. import (
  3. "context"
  4. "errors"
  5. "time"
  6. tmod "go-common/app/job/main/videoup-report/model/task"
  7. account "go-common/app/service/main/account/api"
  8. upsrpc "go-common/app/service/main/up/api/v1"
  9. "go-common/library/log"
  10. "go-common/library/sync/errgroup"
  11. "fmt"
  12. "math"
  13. )
  14. //ERROR
  15. var (
  16. ErrRPCEmpty = errors.New("rpc reply empty")
  17. )
  18. func (s *Service) upGroupMids(c context.Context, gid int64) (mids []int64, err error) {
  19. var (
  20. total int
  21. maxps = 10000
  22. req = &upsrpc.UpGroupMidsReq{
  23. Pn: 1,
  24. GroupID: gid,
  25. Ps: maxps,
  26. }
  27. reply *upsrpc.UpGroupMidsReply
  28. )
  29. for {
  30. reply, err = s.upsRPC.UpGroupMids(c, req)
  31. if err == nil && (reply == nil || reply.Mids == nil) {
  32. err = ErrRPCEmpty
  33. }
  34. if err != nil {
  35. log.Error("UpGroupMids req(%+v) error(%v)", req, err)
  36. return
  37. }
  38. total = reply.Total
  39. mids = append(mids, reply.Mids...)
  40. if reply.Size() != maxps {
  41. break
  42. }
  43. req.Pn++
  44. }
  45. log.Info("upGroupMids(%d) reply total(%d) len(%d)", gid, total, len(mids))
  46. return
  47. }
  48. func (s *Service) upSpecial(c context.Context) (ups map[int8]map[int64]struct{}, err error) {
  49. var (
  50. g errgroup.Group
  51. whitegroup, blackgroup, policesgroup, enterprisegroup, signedgroup map[int64]struct{}
  52. )
  53. ups = make(map[int8]map[int64]struct{})
  54. f := func(gid int8) (map[int64]struct{}, error) {
  55. group := make(map[int64]struct{})
  56. mids, e := s.upGroupMids(c, int64(gid))
  57. if e != nil {
  58. return group, e
  59. }
  60. for _, mid := range mids {
  61. group[mid] = struct{}{}
  62. }
  63. return group, nil
  64. }
  65. g.Go(func() error {
  66. whitegroup, err = f(tmod.UpperTypeWhite)
  67. return err
  68. })
  69. g.Go(func() error {
  70. blackgroup, err = f(tmod.UpperTypeBlack)
  71. return err
  72. })
  73. g.Go(func() error {
  74. policesgroup, err = f(tmod.UpperTypePolitices)
  75. return err
  76. })
  77. g.Go(func() error {
  78. enterprisegroup, err = f(tmod.UpperTypeEnterprise)
  79. return err
  80. })
  81. g.Go(func() error {
  82. signedgroup, err = f(tmod.UpperTypeSigned)
  83. return err
  84. })
  85. if err = g.Wait(); err != nil {
  86. return
  87. }
  88. ups[tmod.UpperTypeWhite] = whitegroup
  89. ups[tmod.UpperTypeBlack] = blackgroup
  90. ups[tmod.UpperTypePolitices] = policesgroup
  91. ups[tmod.UpperTypeEnterprise] = enterprisegroup
  92. ups[tmod.UpperTypeSigned] = signedgroup
  93. return
  94. }
  95. //
  96. func (s *Service) profile(c context.Context, mid int64) (p *account.ProfileStatReply, err error) {
  97. if p, err = s.accRPC.ProfileWithStat3(c, &account.MidReq{Mid: mid}); err != nil {
  98. p = nil
  99. log.Error("s.accRPC.ProfileWithStat3(%d) error(%v)", mid, err)
  100. }
  101. return
  102. }
  103. func (s *Service) getUpperFans(c context.Context, mid int64) (fans int64, failed bool) {
  104. card, err := s.profile(c, mid)
  105. if err != nil {
  106. failed = true
  107. log.Error("s.profile(mid=%d) error(%v)", mid, err)
  108. return
  109. }
  110. fans = card.Follower
  111. log.Info("s.profile(mid=%d) fans(%d)", mid, fans)
  112. return
  113. }
  114. func (s *Service) isWhite(mid int64) bool {
  115. if ups, ok := s.upperCache[tmod.UpperTypeWhite]; ok {
  116. _, isWhite := ups[mid]
  117. return isWhite
  118. }
  119. return false
  120. }
  121. func (s *Service) isBlack(mid int64) bool {
  122. if ups, ok := s.upperCache[tmod.UpperTypeBlack]; ok {
  123. _, isBlack := ups[mid]
  124. return isBlack
  125. }
  126. return false
  127. }
  128. func (s *Service) isPolitices(mid int64) bool {
  129. if ups, ok := s.upperCache[tmod.UpperTypePolitices]; ok {
  130. _, isShiZheng := ups[mid]
  131. return isShiZheng
  132. }
  133. return false
  134. }
  135. func (s *Service) isEnterprise(mid int64) bool {
  136. if ups, ok := s.upperCache[tmod.UpperTypeEnterprise]; ok {
  137. _, isQiYe := ups[mid]
  138. return isQiYe
  139. }
  140. return false
  141. }
  142. func (s *Service) isSigned(mid int64) bool {
  143. if ups, ok := s.upperCache[tmod.UpperTypeSigned]; ok {
  144. _, signed := ups[mid]
  145. return signed
  146. }
  147. return false
  148. }
  149. // Until next day x hours
  150. func nextDay(hour int) time.Duration {
  151. n := time.Now().Add(24 * time.Hour)
  152. d := time.Date(n.Year(), n.Month(), n.Day(), hour, 0, 0, 0, n.Location())
  153. return time.Until(d)
  154. }
  155. func secondsFormat(sec int) (str string) {
  156. if sec < 0 {
  157. return "--:--:--"
  158. }
  159. if sec == 0 {
  160. return "00:00:00"
  161. }
  162. h := math.Floor(float64(sec) / 3600)
  163. m := math.Floor((float64(sec) - 3600*h) / 60)
  164. se := sec % 60
  165. return fmt.Sprintf("%02d:%02d:%02d", int64(h), int64(m), se)
  166. }