dm.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "strconv"
  6. "strings"
  7. "time"
  8. dmmdl "go-common/app/interface/main/dm2/model"
  9. "go-common/app/job/main/archive/model/dm"
  10. "go-common/app/service/main/archive/api"
  11. "go-common/app/service/main/archive/model/archive"
  12. "go-common/library/log"
  13. "go-common/library/queue/databus"
  14. )
  15. const (
  16. _type = "archive"
  17. _table = "dm_subject_"
  18. _update = "update"
  19. _subjectTypeForAv = 1
  20. )
  21. func (s *Service) dmConsumer() {
  22. defer s.waiter.Done()
  23. for {
  24. var (
  25. msg *databus.Message
  26. ok bool
  27. err error
  28. canal = &dm.Canal{}
  29. )
  30. if msg, ok = <-s.dmSub.Messages(); !ok || s.closeSub {
  31. log.Error("s.dmSub Closed")
  32. return
  33. }
  34. msg.Commit()
  35. if err = json.Unmarshal(msg.Value, canal); err != nil {
  36. log.Error("json.Unmarshal(%s) error(%v)", msg.Value, canal)
  37. continue
  38. }
  39. // not dm_subject_
  40. if !strings.HasPrefix(canal.Table, _table) {
  41. log.Warn("table(%s) message(%s) skiped", canal.Table, msg.Value)
  42. continue
  43. }
  44. // not update
  45. if canal.Action != _update {
  46. log.Warn("action(%s) message(%s) skiped", canal.Action, msg.Value)
  47. continue
  48. }
  49. var subject *dm.Subject
  50. if err = json.Unmarshal(canal.New, &subject); err != nil {
  51. log.Error("json.Unmarshal(%s) error(%v)", canal.New, err)
  52. continue
  53. }
  54. // type must be av
  55. if subject.Type != _subjectTypeForAv {
  56. log.Warn("subject type(%s) is not av message(%+v)", subject.Type, subject)
  57. continue
  58. }
  59. s.dmMu.Lock()
  60. s.dmCids[subject.CID] = struct{}{}
  61. s.dmMu.Unlock()
  62. }
  63. }
  64. func (s *Service) dmCounter() {
  65. defer s.waiter.Done()
  66. for {
  67. time.Sleep(5 * time.Second)
  68. s.dmMu.Lock()
  69. cm := s.dmCids
  70. s.dmCids = make(map[int64]struct{})
  71. s.dmMu.Unlock()
  72. var (
  73. aids []int64
  74. err error
  75. c = context.TODO()
  76. am = make(map[int64][]int64)
  77. allCids []int64
  78. )
  79. for cid := range cm {
  80. if aids, err = s.archiveDao.Aids(c, cid); err != nil {
  81. log.Error("s.archiveDao.Aids(%d) error(%v)", err)
  82. continue
  83. }
  84. for _, aid := range aids {
  85. if _, ok := am[aid]; ok {
  86. continue
  87. }
  88. var pages []*api.Page
  89. if pages, err = s.arcServices[0].Page3(c, &archive.ArgAid2{Aid: aid}); err != nil {
  90. log.Error("s.arcServices[0].Page3(%d) error(%v)", aid, err)
  91. continue
  92. }
  93. for _, p := range pages {
  94. am[aid] = append(am[aid], p.Cid)
  95. allCids = append(allCids, p.Cid)
  96. }
  97. }
  98. }
  99. var (
  100. times int
  101. argCount = 100
  102. cids []int64
  103. cDmCount = make(map[int64]int64)
  104. )
  105. if len(allCids)%argCount == 0 {
  106. times = len(allCids) / argCount
  107. } else {
  108. times = len(allCids)/argCount + 1
  109. }
  110. for i := 0; i < times; i++ {
  111. if i == times-1 {
  112. cids = allCids[i*argCount:]
  113. } else {
  114. cids = allCids[i*argCount : (i+1)*argCount]
  115. }
  116. var sm map[int64]*dmmdl.SubjectInfo
  117. if sm, err = s.dm2RPC.SubjectInfos(c, &dmmdl.ArgOids{Type: 1, Oids: cids}); err != nil {
  118. log.Error("s.dm2RPC.SubjectInfos(%v) error(%v)", cids, err)
  119. continue
  120. }
  121. for cid, s := range sm {
  122. cDmCount[cid] = int64(s.Count)
  123. }
  124. }
  125. L:
  126. for aid, cids := range am {
  127. var sum int64
  128. for _, cid := range cids {
  129. var (
  130. cnt int64
  131. ok bool
  132. )
  133. if cnt, ok = cDmCount[cid]; !ok {
  134. log.Error("dm cid(%d) no count", cid)
  135. break L
  136. }
  137. sum += cnt
  138. }
  139. dMsg := &dm.Count{ID: aid, Count: sum, Type: _type, Timestamp: time.Now().Unix()}
  140. if err = s.dmPub.Send(c, strconv.FormatInt(aid, 10), dMsg); err != nil {
  141. log.Error("s.dmPub.Send error(%v)", err)
  142. continue
  143. }
  144. log.Info("s.dmPub.Send(%+v) success", dMsg)
  145. }
  146. if s.closeSub {
  147. return
  148. }
  149. }
  150. }