model.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package model
  2. import (
  3. "go-common/library/ecode"
  4. "sort"
  5. )
  6. // const var
  7. const (
  8. WilsonLHRRAlgorithm = "wilsonLHRR"
  9. WilsonLHRRFluidAlgorithm = "wilsonLHRRFluid"
  10. OriginAlgorithm = "origin"
  11. LikeDescAlgorithm = "likeDesc"
  12. StateInactive = int(0)
  13. StateActive = int(1)
  14. SlotsNum = 100
  15. DefaultSlotName = "default"
  16. DefaultAlgorithm = "default"
  17. DefaultWeight = ""
  18. )
  19. // EventMsg EventMsg
  20. type EventMsg struct {
  21. Action string `json:"action"`
  22. Oid int64 `json:"oid"`
  23. Tp int `json:"tp"`
  24. }
  25. // SlotsMapping slot name mapping
  26. type SlotsMapping struct {
  27. Name string
  28. Slots []int
  29. State int
  30. }
  31. // SlotsStat slots stat
  32. type SlotsStat struct {
  33. Name string
  34. Slots []int
  35. Algorithm string
  36. Weight string
  37. State int
  38. }
  39. // StatisticsStats StatisticsStats
  40. type StatisticsStats []*StatisticsStat
  41. // GroupByName group statistics by name
  42. func (s StatisticsStats) GroupByName() (res map[string]StatisticsStats) {
  43. res = make(map[string]StatisticsStats)
  44. for _, stat := range s {
  45. if _, ok := res[stat.Name]; ok {
  46. res[stat.Name] = append(res[stat.Name], stat)
  47. } else {
  48. var tmp []*StatisticsStat
  49. tmp = append(tmp, stat)
  50. res[stat.Name] = tmp
  51. }
  52. }
  53. return
  54. }
  55. // StatisticsStat 实验组或者对照组的各项统计
  56. type StatisticsStat struct {
  57. // 流量所属槽位 0~99
  58. Slot int
  59. // 所属实验组名
  60. Name string
  61. State int
  62. Date int
  63. Hour int
  64. HotLike int64
  65. HotHate int64
  66. HotReport int64
  67. HotChildReply int64
  68. // 整个评论区
  69. TotalLike int64
  70. TotalHate int64
  71. TotalReport int64
  72. TotalChildReply int64
  73. TotalRootReply int64
  74. // 用户点开评论区次数
  75. View uint32
  76. // 评论列表接口调用次数
  77. TotalView uint32
  78. // 热门评论接口调用次数
  79. HotView uint32
  80. // 更多热门评论点击次数
  81. HotClick uint32
  82. // 用户在评论首页看到的热门评论被点赞点踩评论以及举报的次数
  83. // UV的统计数据
  84. HotLikeUV int64
  85. HotHateUV int64
  86. HotReportUV int64
  87. HotChildUV int64
  88. TotalLikeUV int64
  89. TotalHateUV int64
  90. TotalReportUV int64
  91. TotalChildUV int64
  92. TotalRootUV int64
  93. }
  94. // Merge Merge
  95. func (stat1 *StatisticsStat) Merge(stat2 *StatisticsStat) (stat3 *StatisticsStat) {
  96. stat3 = new(StatisticsStat)
  97. stat3.View = stat1.View + stat2.View
  98. stat3.HotView = stat1.HotView + stat2.HotView
  99. stat3.HotClick = stat1.HotClick + stat2.HotClick
  100. stat3.TotalView = stat1.TotalView + stat2.TotalView
  101. return
  102. }
  103. // DivideByPercent ...
  104. func (stat1 *StatisticsStat) DivideByPercent(percent int64) (stat2 *StatisticsStat) {
  105. stat2 = new(StatisticsStat)
  106. if percent <= 0 {
  107. return
  108. }
  109. stat2.Name = stat1.Name
  110. stat2.Date = stat1.Date
  111. stat2.Hour = stat1.Hour
  112. stat2.View = stat1.View / uint32(percent)
  113. stat2.HotView = stat1.HotView / uint32(percent)
  114. stat2.HotClick = stat1.HotClick / uint32(percent)
  115. stat2.TotalView = stat1.TotalView / uint32(percent)
  116. stat2.HotLike = stat1.HotLike / percent
  117. stat2.HotHate = stat1.HotHate / percent
  118. stat2.HotChildReply = stat1.HotChildReply / percent
  119. stat2.HotReport = stat1.HotReport / percent
  120. stat2.TotalLike = stat1.TotalLike / percent
  121. stat2.TotalHate = stat1.TotalHate / percent
  122. stat2.TotalReport = stat1.TotalReport / percent
  123. stat2.TotalRootReply = stat1.TotalRootReply / percent
  124. stat2.TotalChildReply = stat1.TotalChildReply / percent
  125. return
  126. }
  127. // MergeByDate MergeByDate
  128. func (stat1 *StatisticsStat) MergeByDate(stat2 *StatisticsStat) (stat3 *StatisticsStat) {
  129. stat3 = new(StatisticsStat)
  130. stat3.Name = stat1.Name
  131. stat3.Date = stat1.Date
  132. stat3.View = stat1.View + stat2.View
  133. stat3.HotView = stat1.HotView + stat2.HotView
  134. stat3.HotClick = stat1.HotClick + stat2.HotClick
  135. stat3.TotalView = stat1.TotalView + stat2.TotalView
  136. stat3.HotLike = stat1.HotLike + stat2.HotLike
  137. stat3.HotHate = stat1.HotHate + stat2.HotHate
  138. stat3.HotChildReply = stat1.HotChildReply + stat2.HotChildReply
  139. stat3.HotReport = stat1.HotReport + stat2.HotReport
  140. stat3.TotalLike = stat1.TotalLike + stat2.TotalLike
  141. stat3.TotalHate = stat1.TotalHate + stat2.TotalHate
  142. stat3.TotalReport = stat1.TotalReport + stat2.TotalReport
  143. stat3.TotalRootReply = stat1.TotalRootReply + stat2.TotalRootReply
  144. stat3.TotalChildReply = stat1.TotalChildReply + stat2.TotalChildReply
  145. return
  146. }
  147. // WilsonLHRRWeight wilson score interval weight
  148. type WilsonLHRRWeight struct {
  149. Like float64 `json:"like"`
  150. Hate float64 `json:"hate"`
  151. Reply float64 `json:"reply"`
  152. Report float64 `json:"report"`
  153. }
  154. // Validate Validate
  155. func (weight WilsonLHRRWeight) Validate() (err error) {
  156. if weight.Report*weight.Reply*weight.Hate*weight.Like <= 0 {
  157. err = ecode.RequestErr
  158. return
  159. }
  160. return
  161. }
  162. // WilsonLHRRFluidWeight WilsonLHRRFluidWeight
  163. type WilsonLHRRFluidWeight struct {
  164. Like float64 `json:"like"`
  165. Hate float64 `json:"hate"`
  166. Reply float64 `json:"reply"`
  167. Report float64 `json:"report"`
  168. Slope float64 `json:"slope"`
  169. }
  170. // Validate Validate
  171. func (weight WilsonLHRRFluidWeight) Validate() (err error) {
  172. if weight.Report*weight.Reply*weight.Hate*weight.Like*weight.Slope <= 0 {
  173. err = ecode.RequestErr
  174. return
  175. }
  176. return
  177. }
  178. // SSReq ss req
  179. type SSReq struct {
  180. DateFrom int64 `form:"date_from" validate:"required"`
  181. DateEnd int64 `form:"date_end" validate:"required"`
  182. Hour bool `form:"hour"`
  183. }
  184. // SSHourRes ss res
  185. type SSHourRes struct {
  186. Legend []string `json:"legend"`
  187. XAxis []string `json:"x_axis"`
  188. Series map[string][]*StatisticsStat `json:"series"`
  189. }
  190. // Sort ...
  191. func (s *SSHourRes) Sort() {
  192. sort.Strings(s.Legend)
  193. sort.Strings(s.XAxis)
  194. for _, v := range s.Series {
  195. sort.Slice(v, func(i, j int) bool { return v[i].Date*100+v[i].Hour < v[j].Date*100+v[j].Hour })
  196. }
  197. }
  198. // SSDateRes ss res
  199. type SSDateRes struct {
  200. Legend []string `json:"legend"`
  201. XAxis []int `json:"x_axis"`
  202. Series map[string][]*StatisticsStat `json:"series"`
  203. }
  204. // Sort ...
  205. func (s *SSDateRes) Sort() {
  206. sort.Strings(s.Legend)
  207. sort.Ints(s.XAxis)
  208. for _, v := range s.Series {
  209. sort.Slice(v, func(i, j int) bool { return v[i].Date < v[j].Date })
  210. }
  211. }