playlist.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package model
  2. import (
  3. "fmt"
  4. "go-common/library/time"
  5. )
  6. const (
  7. // ViewCountType view count type.
  8. ViewCountType = "view"
  9. // FavCountType fav count type.
  10. FavCountType = "favorite"
  11. // ReplyCountType reply count type.
  12. ReplyCountType = "reply"
  13. // ShareCountType share count type.
  14. ShareCountType = "share"
  15. )
  16. // StatM playlist's topic stat message in databus.
  17. type StatM struct {
  18. Type string `json:"type"`
  19. ID int64 `json:"id"`
  20. Aid int64 `json:"aid"`
  21. Count *int64 `json:"count"`
  22. Timestamp time.Time `json:"timestamp"`
  23. IP string `json:"ip"`
  24. }
  25. // StatMsg means playlist's stat message in databus.
  26. type StatMsg struct {
  27. Type string `json:"type"`
  28. Pid int64 `json:"pid"`
  29. Mid int64 `json:"mid"`
  30. Fid int64 `json:"fid"`
  31. Aid int64 `json:"aid"`
  32. View *int64 `json:"view"`
  33. Favorite *int64 `json:"fav"`
  34. Reply *int64 `json:"reply"`
  35. Share *int64 `json:"share"`
  36. MTime time.Time `json:"mtime"`
  37. IP string `json:"ip"`
  38. }
  39. // String format sm
  40. func (sm *StatM) String(tp string) (res string) {
  41. if sm == nil {
  42. res = "<nil>"
  43. return
  44. }
  45. res = fmt.Sprintf("pid: %v, aid: %v, ip: %v, "+tp+"(%s) count(%d)", sm.ID, sm.Aid, sm.IP, formatPInt(sm.Count))
  46. return
  47. }
  48. func formatPInt(s *int64) (res string) {
  49. if s == nil {
  50. return "<nil>"
  51. }
  52. return fmt.Sprintf("%d", *s)
  53. }
  54. // Merge merges stat.
  55. func Merge(last, m *StatMsg) {
  56. if m.View != nil && *m.View >= 0 {
  57. *last.View = *m.View
  58. }
  59. if m.Share != nil && *m.Share >= 0 {
  60. *last.Share = *m.Share
  61. }
  62. if m.Favorite != nil && *m.Favorite >= 0 {
  63. *last.Favorite = *m.Favorite
  64. }
  65. if m.Reply != nil && *m.Reply >= 0 {
  66. *last.Reply = *m.Reply
  67. }
  68. }