alert.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package http
  2. import (
  3. "go-common/app/interface/openplatform/monitor-end/model"
  4. "go-common/app/interface/openplatform/monitor-end/model/monitor"
  5. "go-common/library/ecode"
  6. bm "go-common/library/net/http/blademaster"
  7. "go-common/library/net/metadata"
  8. "strconv"
  9. )
  10. func groupList(c *bm.Context) {
  11. var params = &model.GroupListParams{}
  12. if err := c.Bind(params); err != nil {
  13. c.JSON(nil, err)
  14. return
  15. }
  16. c.JSON(mfSvc.GroupList(c, params))
  17. }
  18. func groupAdd(c *bm.Context) {
  19. var params = &model.Group{}
  20. if err := c.Bind(params); err != nil {
  21. c.JSON(nil, err)
  22. return
  23. }
  24. c.JSON(mfSvc.AddGroup(c, params))
  25. }
  26. func groupUpdate(c *bm.Context) {
  27. var (
  28. params = &model.Group{}
  29. err error
  30. )
  31. if err = c.Bind(params); err != nil {
  32. c.JSON(nil, err)
  33. return
  34. }
  35. err = mfSvc.UpdateGroup(c, params)
  36. c.JSON(nil, err)
  37. }
  38. func groupDelete(c *bm.Context) {
  39. var (
  40. params = &model.Target{}
  41. err error
  42. )
  43. if err = c.Bind(params); err != nil {
  44. c.JSON(nil, err)
  45. return
  46. }
  47. err = mfSvc.DeleteGroup(c, params.ID)
  48. c.JSON(nil, err)
  49. }
  50. func targetList(c *bm.Context) {
  51. var (
  52. params = &model.Target{}
  53. pn, ps int
  54. err error
  55. form = c.Request.Form
  56. sort string
  57. draw int
  58. res *model.Targets
  59. )
  60. if err = c.Bind(params); err != nil {
  61. c.JSON(nil, err)
  62. return
  63. }
  64. pnStr := form.Get("page")
  65. psStr := form.Get("pagesize")
  66. drawStr := form.Get("draw")
  67. sort = form.Get("sort")
  68. if sort == "" {
  69. sort = "ctime,0"
  70. }
  71. if drawStr != "" {
  72. if draw, err = strconv.Atoi(drawStr); err != nil {
  73. err = ecode.RequestErr
  74. c.JSON(nil, err)
  75. return
  76. }
  77. pnStr = form.Get("start")
  78. psStr = form.Get("length")
  79. }
  80. if pn, err = strconv.Atoi(pnStr); err != nil || pn < 0 {
  81. err = ecode.RequestErr
  82. c.JSON(nil, err)
  83. return
  84. }
  85. if ps, err = strconv.Atoi(psStr); err != nil || ps < 0 {
  86. err = ecode.RequestErr
  87. c.JSON(nil, err)
  88. return
  89. }
  90. if ps == 0 {
  91. ps = 20
  92. }
  93. if draw > 0 {
  94. pn = (pn + ps) / ps
  95. }
  96. if pn == 0 {
  97. pn = 1
  98. }
  99. res, err = mfSvc.TargetList(c, params, pn, ps, sort)
  100. if draw > 0 {
  101. res.Draw = draw
  102. }
  103. c.JSON(res, err)
  104. }
  105. func targetAdd(c *bm.Context) {
  106. var params = &model.Target{}
  107. if err := c.Bind(params); err != nil {
  108. c.JSON(nil, err)
  109. return
  110. }
  111. c.JSON(mfSvc.AddTarget(c, params))
  112. }
  113. func targetUpdate(c *bm.Context) {
  114. var (
  115. params = &model.Target{}
  116. err error
  117. )
  118. if err = c.Bind(params); err != nil {
  119. c.JSON(nil, err)
  120. return
  121. }
  122. err = mfSvc.UpdateTarget(c, params)
  123. c.JSON(nil, err)
  124. }
  125. func targetSync(c *bm.Context) {
  126. var (
  127. params = &model.Target{}
  128. err error
  129. )
  130. if err = c.Bind(params); err != nil {
  131. c.JSON(nil, err)
  132. return
  133. }
  134. if params.ID == 0 {
  135. err = ecode.RequestErr
  136. c.JSON(nil, err)
  137. return
  138. }
  139. err = mfSvc.TargetSync(c, params.ID, params.State)
  140. c.JSON(nil, err)
  141. }
  142. func productAdd(c *bm.Context) {
  143. var (
  144. params = &model.Product{}
  145. err error
  146. )
  147. if err = c.Bind(params); err != nil {
  148. c.JSON(nil, err)
  149. return
  150. }
  151. c.JSON(mfSvc.AddProduct(c, params))
  152. }
  153. func productUpdate(c *bm.Context) {
  154. var (
  155. params = &model.Product{}
  156. err error
  157. )
  158. if err = c.Bind(params); err != nil {
  159. c.JSON(nil, err)
  160. return
  161. }
  162. err = mfSvc.UpdateProduct(c, params)
  163. c.JSON(nil, err)
  164. }
  165. func productDelete(c *bm.Context) {
  166. var (
  167. params = &model.Product{}
  168. err error
  169. )
  170. if err = c.Bind(params); err != nil {
  171. c.JSON(nil, err)
  172. return
  173. }
  174. if params.ID == 0 {
  175. err = ecode.RequestErr
  176. c.JSON(nil, err)
  177. return
  178. }
  179. err = mfSvc.DeleteProduct(c, params.ID)
  180. c.JSON(nil, err)
  181. }
  182. func productList(c *bm.Context) {
  183. c.JSON(mfSvc.AllProducts(c))
  184. }
  185. func collect(c *bm.Context) {
  186. var (
  187. params = &monitor.Log{}
  188. err error
  189. )
  190. if err = c.Bind(params); err != nil {
  191. c.JSON(nil, err)
  192. return
  193. }
  194. midInter, _ := c.Get("mid")
  195. if midInter != nil {
  196. params.Mid = strconv.FormatInt(midInter.(int64), 10)
  197. }
  198. params.IP = metadata.String(c, metadata.RemoteIP)
  199. params.Buvid = c.Request.Header.Get("Buvid")
  200. params.UserAgent = c.Request.Header.Get("User-Agent")
  201. mfSvc.Collect(c, params)
  202. c.JSON(nil, nil)
  203. }