event.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package apis
  2. import (
  3. "fmt"
  4. "go-admin/app/observe/service"
  5. "go-admin/app/observe/service/dto"
  6. "go-admin/utils"
  7. "net/http"
  8. "time"
  9. "github.com/gin-gonic/gin"
  10. )
  11. type Event struct {
  12. utils.OtApi
  13. }
  14. // List 事件s列表
  15. func (e Event) List(c *gin.Context) {
  16. req := new(dto.EventListReq)
  17. svc := new(service.Event)
  18. if err := e.Init(c, req, &svc.OtService); err != nil {
  19. return
  20. }
  21. // // e.Errors = nil
  22. // err := e.MakeContext(c).MakeDB().Bind(req).MakeService(&svc.OtService).Errors
  23. // if err != nil {
  24. // // 是否可以把ErrMsg放到Api中
  25. // // e.Error(http.StatusInternalServerError, err, request.ErrorMsg(param, err)) // 方式待优化,暂时不这么搞
  26. // e.Error(http.StatusInternalServerError, err, err.Error())
  27. // return
  28. // }
  29. count := int64(0)
  30. result := make([]dto.EventListResp, 0)
  31. if err := svc.GetPage(req, &result, &count); err != nil {
  32. e.Error(http.StatusInternalServerError, err, fmt.Sprintf("获取事件列表失败: %s", err.Error()))
  33. return
  34. }
  35. e.PageOK(result, int(count), req.GetPageIndex(), req.GetPageSize(), "sucess")
  36. }
  37. func (e Event) ExceptionNums(c *gin.Context) {
  38. req := new(dto.EventExceptionNumsReq)
  39. svc := new(service.Event)
  40. if err := e.Init(c, req, &svc.OtService); err != nil {
  41. return
  42. }
  43. if req.StartTime == 0 || req.EndTime == 0 {
  44. // 未传时间,默认展示1小时之内的异常事件告警
  45. req.EndTime = time.Now().Unix()
  46. req.StartTime = req.EndTime - int64((60 * time.Minute).Seconds())
  47. }
  48. resp := []dto.EventExceptionNumsResp{}
  49. count := int64(0)
  50. if err := svc.ExceptionNums(req, &resp, &count); err != nil {
  51. e.Error(http.StatusInternalServerError, err, err.Error())
  52. return
  53. }
  54. e.PageOK(resp, int(count), req.GetPageIndex(), req.GetPageSize(), "success")
  55. }
  56. func (e Event) ExceptionNumByID(c *gin.Context) {
  57. req := new(dto.EventExecptionNumGetByIDReq)
  58. svc := new(service.Event)
  59. if err := e.Init(c, req, &svc.OtService); err != nil {
  60. return
  61. }
  62. count := int64(0)
  63. if err := svc.ExceptionNumByID(req, &count); err != nil {
  64. e.Error(http.StatusInternalServerError, err, err.Error())
  65. return
  66. }
  67. e.OK(int(count), "查询成功")
  68. }
  69. func (e Event) EventStatistic(c *gin.Context) {
  70. req := new(dto.EventStatisticReq)
  71. se := new(service.Event)
  72. if err := e.Init(c, req, &se.OtService); err != nil {
  73. return
  74. }
  75. if req.StartTime == 0 || req.EndTime == 0 {
  76. // 未传时间,默认展示1小时之内的异常事件告警
  77. req.EndTime = time.Now().Unix()
  78. req.StartTime = req.EndTime - int64((60 * time.Minute).Seconds())
  79. }
  80. result := make([]dto.EventStatisticResp, 0)
  81. if err := se.EventStatistic(req, &result); err != nil {
  82. e.Error(http.StatusInternalServerError, err, fmt.Sprintf("异常统计失败: %s", err.Error()))
  83. return
  84. }
  85. e.OK(result, "sucess")
  86. }
  87. func (e Event) EventList(c *gin.Context) {
  88. req := new(dto.EventStatisticReq)
  89. se := new(service.Event)
  90. if err := e.Init(c, req, &se.OtService); err != nil {
  91. return
  92. }
  93. if req.StartTime == 0 || req.EndTime == 0 {
  94. // 未传时间,默认展示1小时之内的异常事件告警
  95. req.EndTime = time.Now().Unix()
  96. req.StartTime = req.EndTime - int64((60 * time.Minute).Seconds())
  97. }
  98. result := make([]dto.EventListsResp, 0)
  99. if err := se.EventLists(req, &result); err != nil {
  100. e.Error(http.StatusInternalServerError, err, fmt.Sprintf("聚合异常失败: %s", err.Error()))
  101. return
  102. }
  103. e.OK(result, "sucess")
  104. }