loader.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "go-common/app/job/main/reply-feed/model"
  6. "go-common/library/log"
  7. )
  8. func (s *Service) loadAlgorithm() (err error) {
  9. ss, err := s.dao.SlotStats(context.Background())
  10. if err != nil {
  11. return
  12. }
  13. // 按name聚合slot
  14. ssMap := make(map[string]*model.SlotsStat)
  15. for _, s := range ss {
  16. if v, exists := ssMap[s.Name]; exists {
  17. v.Slots = append(v.Slots, s.Slot)
  18. } else {
  19. ssMap[s.Name] = &model.SlotsStat{
  20. Name: s.Name,
  21. Slots: []int{s.Slot},
  22. Algorithm: s.Algorithm,
  23. Weight: s.Weight,
  24. }
  25. }
  26. }
  27. var algorithms []model.Algorithm
  28. for name, ss := range ssMap {
  29. if ss.Weight == "" {
  30. continue
  31. }
  32. var (
  33. algorithm model.Algorithm
  34. w interface{}
  35. )
  36. if ss.Algorithm == model.WilsonLHRRAlgorithm || ss.Algorithm == model.WilsonLHRRFluidAlgorithm {
  37. if err = json.Unmarshal([]byte(ss.Weight), &w); err != nil {
  38. log.Error("json.Unmarshal() error(%v), name (%s), algorightm (%s), weight (%s)", err, name, ss.Algorithm, ss.Weight)
  39. return
  40. }
  41. }
  42. switch ss.Algorithm {
  43. case model.WilsonLHRRAlgorithm:
  44. weight := w.(map[string]interface{})
  45. algorithm = model.NewWilsonLHRR(name, ss.Slots, &model.WilsonLHRRWeight{
  46. Like: weight["like"].(float64),
  47. Hate: weight["hate"].(float64),
  48. Reply: weight["reply"].(float64),
  49. Report: weight["report"].(float64),
  50. })
  51. case model.WilsonLHRRFluidAlgorithm:
  52. weight := w.(map[string]interface{})
  53. algorithm = model.NewWilsonLHRRFluid(name, ss.Slots, &model.WilsonLHRRFluidWeight{
  54. Like: weight["like"].(float64),
  55. Hate: weight["hate"].(float64),
  56. Reply: weight["reply"].(float64),
  57. Report: weight["report"].(float64),
  58. Slope: weight["slope"].(float64),
  59. })
  60. case model.OriginAlgorithm:
  61. algorithm = model.NewOrigin(name, ss.Slots)
  62. case model.LikeDescAlgorithm:
  63. algorithm = model.NewLikeDesc(name, ss.Slots)
  64. case model.DefaultAlgorithm:
  65. continue
  66. default:
  67. log.Warn("invalid algorithm")
  68. continue
  69. }
  70. if algorithm != nil {
  71. algorithms = append(algorithms, algorithm)
  72. }
  73. }
  74. s.algorithmsLock.Lock()
  75. s.algorithms = algorithms
  76. s.algorithmsLock.Unlock()
  77. return
  78. }
  79. func (s *Service) loadSlots() (err error) {
  80. ctx := context.Background()
  81. slotsMap, err := s.dao.SlotsMapping(ctx)
  82. if err != nil {
  83. return
  84. }
  85. s.statisticsLock.Lock()
  86. for name, mapping := range slotsMap {
  87. for _, slot := range mapping.Slots {
  88. s.statisticsStats[slot].Name = name
  89. s.statisticsStats[slot].Slot = slot
  90. }
  91. log.Warn("name stat (name: %s, slots: %v)", name, mapping.Slots)
  92. }
  93. log.Warn("statistics stat (%v)", s.statisticsStats)
  94. s.statisticsLock.Unlock()
  95. return
  96. }