agent.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package report
  2. import (
  3. "context"
  4. "encoding/json"
  5. "strconv"
  6. "time"
  7. "go-common/library/conf/env"
  8. "go-common/library/queue/databus"
  9. "github.com/pkg/errors"
  10. )
  11. type conf struct {
  12. Secret string
  13. Addr string
  14. }
  15. var (
  16. mn *databus.Databus
  17. user *databus.Databus
  18. // ErrInit report init error
  19. ErrInit = errors.New("report initialization failed")
  20. )
  21. const (
  22. _timeFormat = "2006-01-02 15:04:05"
  23. _uname = "uname"
  24. _uid = "uid"
  25. _business = "business"
  26. _type = "type"
  27. _oid = "oid"
  28. _action = "action"
  29. _ctime = "ctime"
  30. _platform = "platform"
  31. _build = "build"
  32. _buvid = "buvid"
  33. _ip = "ip"
  34. _mid = "mid"
  35. _indexInt = "int_"
  36. _indexStr = "str_"
  37. _extra = "extra_data"
  38. )
  39. // ManagerInfo manager report info.
  40. type ManagerInfo struct {
  41. // common
  42. Uname string
  43. UID int64
  44. Business int
  45. Type int
  46. Oid int64
  47. Action string
  48. Ctime time.Time
  49. // extra
  50. Index []interface{}
  51. Content map[string]interface{}
  52. }
  53. // UserInfo user report info
  54. type UserInfo struct {
  55. Mid int64
  56. Platform string
  57. Build int64
  58. Buvid string
  59. Business int
  60. Type int
  61. Oid int64
  62. Action string
  63. Ctime time.Time
  64. IP string
  65. // extra
  66. Index []interface{}
  67. Content map[string]interface{}
  68. }
  69. // UserActionLog 用户行为日志
  70. type UserActionLog struct {
  71. Uname string `json:"uname"`
  72. UID int64 `json:"uid"`
  73. Business int `json:"business"`
  74. Type int `json:"type"`
  75. Oid int64 `json:"oid"`
  76. Action string `json:"action"`
  77. Platform string `json:"platform"`
  78. Build int64 `json:"build"`
  79. Buvid string `json:"buvid"`
  80. IP string `json:"ip"`
  81. Mid int64 `json:"mid"`
  82. Int0 int64 `json:"int_0"`
  83. Int1 int64 `json:"int_1"`
  84. Int2 int64 `json:"int_2"`
  85. Str0 string `json:"str_0"`
  86. Str1 string `json:"str_1"`
  87. Str2 string `json:"str_2"`
  88. Ctime string `json:"ctime"`
  89. Extra string `json:"extra_data"`
  90. }
  91. // AuditLog 审核日志
  92. type AuditLog struct {
  93. Uname string `json:"uname"`
  94. UID int64 `json:"uid"`
  95. Business int `json:"business"`
  96. Type int `json:"type"`
  97. Oid int64 `json:"oid"`
  98. Action string `json:"action"`
  99. Int0 int64 `json:"int_0"`
  100. Int1 int64 `json:"int_1"`
  101. Int2 int64 `json:"int_2"`
  102. Str0 string `json:"str_0"`
  103. Str1 string `json:"str_1"`
  104. Str2 string `json:"str_2"`
  105. Ctime string `json:"ctime"`
  106. Extra string `json:"extra_data"`
  107. }
  108. // InitManager init manager report log agent.
  109. func InitManager(c *databus.Config) {
  110. if c == nil {
  111. c = _managerConfig
  112. if d, ok := _defaultManagerConfig[env.DeployEnv]; ok {
  113. c.Secret = d.Secret
  114. c.Addr = d.Addr
  115. }
  116. }
  117. mn = databus.New(c)
  118. }
  119. // InitUser init user report log agent.
  120. func InitUser(c *databus.Config) {
  121. if c == nil {
  122. c = _userConfig
  123. if d, ok := _defaultUserConfig[env.DeployEnv]; ok {
  124. c.Secret = d.Secret
  125. c.Addr = d.Addr
  126. }
  127. }
  128. user = databus.New(c)
  129. }
  130. // Manager log a message for manager, xx-admin.
  131. func Manager(m *ManagerInfo) error {
  132. if mn == nil || m == nil {
  133. return ErrInit
  134. }
  135. v := map[string]interface{}{}
  136. if len(m.Content) > 0 {
  137. extraData, _ := json.Marshal(m.Content)
  138. v[_extra] = string(extraData)
  139. }
  140. v[_business] = m.Business
  141. v[_type] = m.Type
  142. v[_uid] = m.UID
  143. v[_oid] = m.Oid
  144. v[_uname] = m.Uname
  145. v[_action] = m.Action
  146. v[_ctime] = m.Ctime.Format(_timeFormat)
  147. return report(mn, v, m.Index...)
  148. }
  149. // User log a message for user, xx-interface.
  150. func User(u *UserInfo) error {
  151. if user == nil || u == nil {
  152. return ErrInit
  153. }
  154. v := map[string]interface{}{}
  155. if len(u.Content) > 0 {
  156. extraData, _ := json.Marshal(u.Content)
  157. v[_extra] = string(extraData)
  158. }
  159. v[_business] = u.Business
  160. v[_type] = u.Type
  161. v[_mid] = u.Mid
  162. v[_oid] = u.Oid
  163. v[_build] = u.Build
  164. v[_action] = u.Action
  165. v[_platform] = u.Platform
  166. v[_buvid] = u.Buvid
  167. v[_ip] = u.IP
  168. v[_ctime] = u.Ctime.Format(_timeFormat)
  169. return report(user, v, u.Index...)
  170. }
  171. func report(h *databus.Databus, v map[string]interface{}, extras ...interface{}) error {
  172. var i, j int
  173. for _, extra := range extras {
  174. switch ex := extra.(type) {
  175. case string:
  176. v[_indexStr+strconv.Itoa(i)] = ex
  177. i++
  178. case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
  179. v[_indexInt+strconv.Itoa(j)] = ex
  180. j++
  181. }
  182. }
  183. return h.Send(context.Background(), v[_ctime].(string), v)
  184. }