model.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. package model
  2. import (
  3. xtime "go-common/library/time"
  4. )
  5. // const varialble
  6. const (
  7. DefaultAlgorithm = "default"
  8. WilsonLHRRAlgorithm = "wilsonLHRR"
  9. WilsonLHRRFluidAlgorithm = "wilsonLHRRFluid"
  10. OriginAlgorithm = "origin"
  11. LikeDescAlgorithm = "likeDesc"
  12. StateInactive = int(0)
  13. StateActive = int(1)
  14. StateDelete = int(2)
  15. // 用于redis中统计uv
  16. StatisticActionRootReply = "rr"
  17. StatisticActionChildReply = "cr"
  18. StatisticActionLike = "l"
  19. StatisticActionHate = "h"
  20. StatisticActionReport = "r"
  21. StatisticKindTotal = "t"
  22. StatisticKindHot = "h"
  23. DatabusActionReply = "reply"
  24. DatabusActionReport = "report_add"
  25. DatabusActionLike = "like"
  26. DatabusActionCancelLike = "like_cancel"
  27. DatabusActionHate = "hate"
  28. DatabusActionCancelHate = "hate_cancel"
  29. // user upper or admin delete
  30. DatabusActionDel = "reply_del"
  31. // admin delete by report
  32. DatabusActionRptDel = "report_del"
  33. // admin recover
  34. DatabusActionRecover = "reply_recover"
  35. // admin or upper top reply
  36. DatabusActionTop = "top"
  37. // admin or upper untop reply
  38. DatabusActionUnTop = "untop"
  39. DatabusActionReIdx = "re_idx"
  40. // 只有大于等于3个赞且评论区根评论数目多余20才会被加入热门评论列表
  41. MinLikeCount = 3
  42. MinRootReplyCount = 20
  43. SlotsNum = 100
  44. )
  45. // Statistics const
  46. var (
  47. StatisticActions = []string{StatisticActionRootReply, StatisticActionChildReply, StatisticActionLike, StatisticActionHate, StatisticActionReport}
  48. StatisticKinds = []string{StatisticKindTotal, StatisticKindHot}
  49. StatisticsDatabaseI = []string{"`name`", "`date`", "`hour`"}
  50. StatisticsDatabaseU = []string{"hot_like", "hot_hate", "hot_report", "hot_child", "total_like", "total_hate", "total_report", "total_root", "total_child"}
  51. StatisticsDatabaseS = []string{"hot_like_uv", "hot_hate_uv", "hot_report_uv", "hot_child_uv", "total_like_uv", "total_hate_uv", "total_report_uv", "total_child_uv", "total_root_uv"}
  52. )
  53. // ReplyScore reply score
  54. type ReplyScore struct {
  55. RpID int64
  56. Score float64
  57. }
  58. // ReplyStat 放在MC里的衡量一条根评论质量的各个参数
  59. type ReplyStat struct {
  60. RpID int64 `json:"rpid"`
  61. Like int `json:"like"`
  62. Hate int `json:"hate"`
  63. Reply int `json:"reply"`
  64. Report int `json:"report"`
  65. SubjectTime xtime.Time `json:"subject_time"`
  66. ReplyTime xtime.Time `json:"reply_time"`
  67. }
  68. // ReplyResp 返回给reply-interface的评论ID数组,已按热度排好序
  69. type ReplyResp struct {
  70. RpIDs []int64
  71. // 属于哪一个实验组
  72. TestSetName string
  73. }
  74. // ReplyList 存在redis sorted set中的数据结构
  75. type ReplyList struct {
  76. RpID []int64
  77. }
  78. // SlotStat slot stat
  79. type SlotStat struct {
  80. Name string
  81. Slot int
  82. Algorithm string
  83. Weight string
  84. }
  85. // SlotsStat SlotsStat
  86. type SlotsStat struct {
  87. Name string
  88. Slots []int
  89. Algorithm string
  90. Weight string
  91. }
  92. // SlotsMapping E group slots
  93. type SlotsMapping struct {
  94. Name string
  95. Slots []int
  96. }
  97. // StatisticsStat 实验组或者对照组的各项统计
  98. type StatisticsStat struct {
  99. // 流量所属槽位 0~99
  100. Slot int
  101. // 所属实验组名
  102. Name string
  103. // 用户在评论首页看到的热门评论被点赞点踩评论以及举报的次数
  104. HotLike int64
  105. HotHate int64
  106. HotChildReply int64
  107. HotReport int64
  108. // 整个评论区
  109. TotalLike int64
  110. TotalHate int64
  111. TotalReport int64
  112. TotalRootReply int64
  113. TotalChildReply int64
  114. HotLikeUV int64
  115. HotHateUV int64
  116. HotReportUV int64
  117. HotChildUV int64
  118. TotalLikeUV int64
  119. TotalHateUV int64
  120. TotalReportUV int64
  121. TotalChildUV int64
  122. TotalRootUV int64
  123. }
  124. // Merge merge two statistics
  125. func (stat1 *StatisticsStat) Merge(stat2 *StatisticsStat) (stat3 *StatisticsStat) {
  126. stat3 = new(StatisticsStat)
  127. stat3.TotalLike = stat1.TotalLike + stat2.TotalLike
  128. stat3.TotalHate = stat1.TotalHate + stat2.TotalHate
  129. stat3.TotalReport = stat1.TotalReport + stat2.TotalReport
  130. stat3.TotalRootReply = stat1.TotalRootReply + stat2.TotalRootReply
  131. stat3.TotalChildReply = stat1.TotalChildReply + stat2.TotalChildReply
  132. stat3.HotLike = stat1.HotLike + stat2.HotLike
  133. stat3.HotHate = stat1.HotHate + stat2.HotHate
  134. stat3.HotReport = stat1.HotReport + stat2.HotReport
  135. stat3.HotChildReply = stat1.HotChildReply + stat2.HotChildReply
  136. stat3.HotLikeUV = stat1.HotLikeUV + stat2.HotLikeUV
  137. stat3.HotHateUV = stat1.HotHateUV + stat2.HotHateUV
  138. stat3.HotReportUV = stat1.HotReportUV + stat2.HotReportUV
  139. stat3.HotChildUV = stat1.HotChildUV + stat2.HotChildUV
  140. stat3.TotalLikeUV = stat1.TotalLikeUV + stat2.TotalLikeUV
  141. stat3.TotalHateUV = stat1.TotalHateUV + stat2.TotalHateUV
  142. stat3.TotalReportUV = stat1.TotalReportUV + stat2.TotalReportUV
  143. stat3.TotalRootUV = stat1.TotalRootUV + stat2.TotalRootUV
  144. stat3.TotalChildUV = stat1.TotalChildUV + stat2.TotalChildUV
  145. return
  146. }
  147. // StrategyStat 实验组所使用算法,以及各个参数情况
  148. type StrategyStat struct {
  149. Name string `json:"name"`
  150. Percent int `json:"percent"`
  151. Algorithm string `json:"algorithm"`
  152. Args map[string]float64 `json:"args"`
  153. }
  154. // RefreshChecker 刷新热门评论的触发条件,用来对同一个评论区的所有请求进行聚合
  155. type RefreshChecker struct {
  156. Oid int64
  157. Type int
  158. LastTimeStamp int64
  159. }
  160. // WilsonLHRRWeight wilson score interval weight
  161. type WilsonLHRRWeight struct {
  162. Like float64
  163. Hate float64
  164. Reply float64
  165. Report float64
  166. }
  167. // WilsonLHRRFluidWeight wilson
  168. type WilsonLHRRFluidWeight struct {
  169. Like float64
  170. Hate float64
  171. Reply float64
  172. Report float64
  173. Slope float64
  174. }
  175. // EventMsg event message
  176. type EventMsg struct {
  177. Action string `json:"action"`
  178. Oid int64 `json:"oid"`
  179. Tp int `json:"tp"`
  180. }
  181. // StatsMsg stats message
  182. type StatsMsg struct {
  183. Action string `json:"action"`
  184. Mid int64 `json:"mid"`
  185. Subject *Subject `json:"subject"`
  186. Reply *Reply `json:"reply"`
  187. Report *Report `json:"report,omitempty"`
  188. }
  189. // Sharding 返回该用户属于哪一个组
  190. // 将流量划分为100份
  191. func (r *StatsMsg) Sharding() int64 {
  192. return r.Mid % SlotsNum
  193. }
  194. // HotCondition return if should check exists in hot reply
  195. func (r *StatsMsg) HotCondition() bool {
  196. if r.Action == DatabusActionReply && !r.Reply.IsRoot() {
  197. return true
  198. }
  199. if r.Reply.IsRoot() && r.Reply.Like >= MinLikeCount &&
  200. (r.Action == DatabusActionLike || r.Action == DatabusActionHate ||
  201. r.Action == DatabusActionCancelLike || r.Action == DatabusActionCancelHate || r.Action == DatabusActionReport) {
  202. return true
  203. }
  204. return false
  205. }
  206. // Reply define reply object
  207. type Reply struct {
  208. RpID int64 `json:"rpid"`
  209. Mid int64 `json:"mid"`
  210. Root int64 `json:"root"`
  211. Parent int64 `json:"parent"`
  212. RCount int `json:"rcount"`
  213. Floor int `json:"floor"`
  214. State int8 `json:"state"`
  215. Attr uint32 `json:"attr"`
  216. CTime xtime.Time `json:"ctime"`
  217. Like int `json:"like"`
  218. Hate int `json:"hate"`
  219. }
  220. // Legal return a reply legal
  221. func (r *Reply) Legal() bool {
  222. // 0,1,2,5,6 所有需要显示给用户的评论state
  223. return r.State == 0 || r.State == 1 || r.State == 2 || r.State == 5 || r.State == 6
  224. }
  225. // ShowAfterAudit ShowAfterAudit
  226. func (r *Reply) ShowAfterAudit() bool {
  227. return r.State == 11
  228. }
  229. // AuditButShow AuditButShow
  230. func (r *Reply) AuditButShow() bool {
  231. return r.State == 5
  232. }
  233. // IsRoot IsRoot
  234. func (r *Reply) IsRoot() bool {
  235. return r.Root == 0
  236. }
  237. // Qualified Qualified
  238. func (r *Reply) Qualified() bool {
  239. return r.Like >= MinLikeCount
  240. }
  241. // Report define reply report
  242. type Report struct {
  243. RpID int64 `json:"rpid"`
  244. Mid int64 `json:"mid"`
  245. Count int `json:"count"`
  246. Score int `json:"score"`
  247. State int8 `json:"state"`
  248. CTime xtime.Time `json:"ctime"`
  249. Attr uint32 `json:"attr"`
  250. }
  251. // Subject is subject of reply
  252. type Subject struct {
  253. Oid int64 `json:"oid"`
  254. Type int `json:"type"`
  255. Mid int64 `json:"mid"`
  256. RCount int `json:"rcount"`
  257. State int8 `json:"state"`
  258. Attr uint32 `json:"attr"`
  259. CTime xtime.Time `json:"ctime"`
  260. }
  261. // ShowHotReply if show
  262. func (s *Subject) ShowHotReply() bool {
  263. return s.RCount >= MinRootReplyCount
  264. }