fix.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package service
  2. import (
  3. "context"
  4. "runtime/debug"
  5. "time"
  6. "go-common/app/job/main/figure-timer/model"
  7. "go-common/library/log"
  8. )
  9. func (s *Service) fixproc() {
  10. defer func() {
  11. if x := recover(); x != nil {
  12. log.Error("fixproc panic(%+v)", x)
  13. log.Error("%+v", string(debug.Stack()))
  14. }
  15. }()
  16. var (
  17. ctx = context.TODO()
  18. err error
  19. weekVerRecordsFrom = time.Now().AddDate(0, 0, -7*52).Unix()
  20. weekVerRecordsTo = time.Now().Unix() + 1
  21. )
  22. for shard := s.c.Property.PendingMidStart; shard < 100; shard++ {
  23. log.Info("start fix: %d", shard)
  24. var (
  25. figures []*model.Figure
  26. fromMid = int64(shard)
  27. end bool
  28. )
  29. for !end {
  30. if figures, end, err = s.dao.Figures(ctx, fromMid, 100); err != nil {
  31. log.Error("%+v", err)
  32. break
  33. }
  34. for _, figure := range figures {
  35. var (
  36. records []*model.FigureRecord
  37. )
  38. if fromMid < figure.Mid {
  39. fromMid = figure.Mid
  40. }
  41. if records, err = s.dao.CalcRecords(ctx, figure.Mid, weekVerRecordsFrom, weekVerRecordsTo); err != nil {
  42. log.Error("%+v", err)
  43. continue
  44. }
  45. s.fixRecord(ctx, figure.Mid, records)
  46. }
  47. }
  48. log.Info("end fix: %d", shard)
  49. }
  50. }
  51. // 全量清洗用户mid所有的records
  52. func (s *Service) fixRecord(c context.Context, mid int64, records []*model.FigureRecord) {
  53. var (
  54. err error
  55. action *model.ActionCounter
  56. x float64
  57. )
  58. for _, record := range records {
  59. time.Sleep(time.Millisecond)
  60. beforeRecord := *record
  61. // 获得本次record 记录对应的 action 记录
  62. if action, err = s.dao.ActionCounter(c, mid, record.Version.Unix()); err != nil {
  63. log.Error("%+v", err)
  64. continue
  65. }
  66. actions := []*model.ActionCounter{action}
  67. // lawful
  68. x, _ = s.calcActionX(s.c.Property.Calc.LawfulPosL, bizTypePosLawful, actions, nil, record.Version.Unix())
  69. record.XPosLawful = int64(x)
  70. x, _ = s.calcActionX(s.c.Property.Calc.LawfulNegL, bizTypeNegLawful, actions, nil, record.Version.Unix())
  71. record.XNegLawful = int64(x)
  72. // wide
  73. record.XPosWide = 0
  74. record.XNegWide = 0
  75. // friendly
  76. x, _ = s.calcActionX(s.c.Property.Calc.FriendlyPosL, bizTypePosFriendly, actions, nil, record.Version.Unix())
  77. record.XPosFriendly = int64(x)
  78. x, _ = s.calcActionX(s.c.Property.Calc.FriendlyNegL, bizTypeNegFriendly, actions, nil, record.Version.Unix())
  79. record.XNegFriendly = int64(x)
  80. // bounty
  81. x, _ = s.calcActionX(s.c.Property.Calc.BountyPosL, bizTypePosBounty, actions, nil, record.Version.Unix())
  82. record.XPosBounty = int64(x)
  83. record.XNegBounty = 0
  84. // creativity
  85. x, _ = s.calcActionX(s.c.Property.Calc.CreativityPosL1, bizTypePosCreativity, actions, nil, record.Version.Unix())
  86. record.XPosCreativity = int64(x)
  87. record.XNegCreativity = 0
  88. // 更新本次的fix
  89. if err = s.dao.PutCalcRecord(c, record, record.Version.Unix()); err != nil {
  90. log.Error("%+v", err)
  91. } else {
  92. log.Info("fix figure record before [%+v] --> now [%+v]", beforeRecord, record)
  93. }
  94. }
  95. }