admin.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package service
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "go-common/app/service/main/reply-feed/model"
  7. "go-common/library/log"
  8. )
  9. /*
  10. SlotsMapping
  11. */
  12. // SlotStatsManager SlotStatsManager
  13. func (s *Service) SlotStatsManager(ctx context.Context) ([]*model.SlotsStat, error) {
  14. return s.dao.SlotsStatManager(ctx)
  15. }
  16. // NewEGroup new experimental group.
  17. func (s *Service) NewEGroup(ctx context.Context, name, algorithm, weight string, percent int) (err error) {
  18. var (
  19. usedSlots []int64
  20. slots []int64
  21. )
  22. if usedSlots, _, _, err = s.dao.SlotsStatByName(ctx, name); err != nil {
  23. return
  24. }
  25. if len(usedSlots) > 0 {
  26. return errors.New("duplicate name")
  27. }
  28. if slots, err = s.dao.IdleSlots(ctx, percent); err != nil {
  29. return
  30. }
  31. return s.dao.UpdateSlotsStat(ctx, name, algorithm, weight, slots, model.StateInactive)
  32. }
  33. // ResizeSlots resize slots
  34. func (s *Service) ResizeSlots(ctx context.Context, name string, percent int) (err error) {
  35. var (
  36. count int
  37. slots []int64
  38. idelSlots []int64
  39. algorithm, weight string
  40. )
  41. if slots, algorithm, weight, err = s.dao.SlotsStatByName(ctx, name); err != nil {
  42. return
  43. }
  44. if percent > len(slots) {
  45. if count, err = s.dao.CountIdleSlot(ctx); err != nil {
  46. return
  47. }
  48. if percent > count {
  49. err = errors.New("out of slot")
  50. return
  51. }
  52. if idelSlots, err = s.dao.IdleSlots(ctx, percent-len(slots)); err != nil {
  53. return
  54. }
  55. if err = s.dao.UpdateSlotsStat(ctx, name, algorithm, weight, idelSlots, model.StateActive); err != nil {
  56. return
  57. }
  58. } else {
  59. if err = s.dao.UpdateSlotsStat(ctx, model.DefaultSlotName, model.DefaultAlgorithm, model.DefaultWeight, slots[percent:], model.StateActive); err != nil {
  60. return
  61. }
  62. }
  63. return
  64. }
  65. // EditSlotsStat edit a test set weight.
  66. func (s *Service) EditSlotsStat(ctx context.Context, name, algorithm, weight string, slots []int64) (err error) {
  67. if err = s.dao.UpdateSlotsStat(ctx, name, algorithm, weight, slots, model.StateInactive); err != nil {
  68. log.Error("Edit SlotsMapping Failed, Error (%v)", err)
  69. }
  70. return
  71. }
  72. // ModifyState modify test set state, activate or inactivate by name.
  73. func (s *Service) ModifyState(ctx context.Context, name string, state int) (err error) {
  74. return s.dao.ModifyState(ctx, name, state)
  75. }
  76. // ResetEGroup ResetEGroup
  77. func (s *Service) ResetEGroup(ctx context.Context, name string) (err error) {
  78. var slots []int64
  79. if slots, _, _, err = s.dao.SlotsStatByName(ctx, name); err != nil {
  80. return
  81. }
  82. if err = s.dao.UpdateSlotsStat(ctx, model.DefaultSlotName, model.DefaultAlgorithm, model.DefaultWeight, slots, model.StateActive); err != nil {
  83. return
  84. }
  85. return
  86. }
  87. /*
  88. TODO(Statistics)
  89. Statistics
  90. */
  91. // StatisticsByDate GetStatisticsByDate
  92. func (s *Service) StatisticsByDate(ctx context.Context, req *model.SSReq) (res map[string]map[int]*model.StatisticsStat, err error) {
  93. res = make(map[string]map[int]*model.StatisticsStat)
  94. stats, err := s.dao.StatisticsByDate(ctx, req.DateFrom, req.DateEnd)
  95. if err != nil {
  96. return
  97. }
  98. groupedStatistics := stats.GroupByName()
  99. for name, ss := range groupedStatistics {
  100. dateStatistics := make(map[int]*model.StatisticsStat)
  101. for _, s := range ss {
  102. if _, ok := dateStatistics[s.Date]; ok {
  103. dateStatistics[s.Date] = dateStatistics[s.Date].MergeByDate(s)
  104. } else {
  105. dateStatistics[s.Date] = s
  106. }
  107. }
  108. res[name] = dateStatistics
  109. }
  110. return
  111. }
  112. // StatisticsByHour GetStatisticsByHour
  113. func (s *Service) StatisticsByHour(ctx context.Context, req *model.SSReq) (res map[string]map[string]*model.StatisticsStat, err error) {
  114. res = make(map[string]map[string]*model.StatisticsStat)
  115. stats, err := s.dao.StatisticsByDate(ctx, req.DateFrom, req.DateEnd)
  116. if err != nil {
  117. return
  118. }
  119. groupedStatistics := stats.GroupByName()
  120. for name, ss := range groupedStatistics {
  121. hourStatistics := make(map[string]*model.StatisticsStat)
  122. for _, s := range ss {
  123. if s.Hour < 10 {
  124. hourStatistics[fmt.Sprintf("%d-0%d", s.Date, s.Hour)] = s
  125. } else {
  126. hourStatistics[fmt.Sprintf("%d-%d", s.Date, s.Hour)] = s
  127. }
  128. }
  129. res[name] = hourStatistics
  130. }
  131. return
  132. }