package apis import ( "fmt" "go-admin/app/observe/service" "go-admin/app/observe/service/dto" "go-admin/utils" "net/http" "time" "github.com/gin-gonic/gin" ) type Event struct { utils.OtApi } // List 事件s列表 func (e Event) List(c *gin.Context) { req := new(dto.EventListReq) svc := new(service.Event) if err := e.Init(c, req, &svc.OtService); err != nil { return } // // e.Errors = nil // err := e.MakeContext(c).MakeDB().Bind(req).MakeService(&svc.OtService).Errors // if err != nil { // // 是否可以把ErrMsg放到Api中 // // e.Error(http.StatusInternalServerError, err, request.ErrorMsg(param, err)) // 方式待优化,暂时不这么搞 // e.Error(http.StatusInternalServerError, err, err.Error()) // return // } count := int64(0) result := make([]dto.EventListResp, 0) if err := svc.GetPage(req, &result, &count); err != nil { e.Error(http.StatusInternalServerError, err, fmt.Sprintf("获取事件列表失败: %s", err.Error())) return } e.PageOK(result, int(count), req.GetPageIndex(), req.GetPageSize(), "sucess") } func (e Event) ExceptionNums(c *gin.Context) { req := new(dto.EventExceptionNumsReq) svc := new(service.Event) if err := e.Init(c, req, &svc.OtService); err != nil { return } if req.StartTime == 0 || req.EndTime == 0 { // 未传时间,默认展示1小时之内的异常事件告警 req.EndTime = time.Now().Unix() req.StartTime = req.EndTime - int64((60 * time.Minute).Seconds()) } resp := []dto.EventExceptionNumsResp{} count := int64(0) if err := svc.ExceptionNums(req, &resp, &count); err != nil { e.Error(http.StatusInternalServerError, err, err.Error()) return } e.PageOK(resp, int(count), req.GetPageIndex(), req.GetPageSize(), "success") } func (e Event) ExceptionNumByID(c *gin.Context) { req := new(dto.EventExecptionNumGetByIDReq) svc := new(service.Event) if err := e.Init(c, req, &svc.OtService); err != nil { return } count := int64(0) if err := svc.ExceptionNumByID(req, &count); err != nil { e.Error(http.StatusInternalServerError, err, err.Error()) return } e.OK(int(count), "查询成功") } func (e Event) EventStatistic(c *gin.Context) { req := new(dto.EventStatisticReq) se := new(service.Event) if err := e.Init(c, req, &se.OtService); err != nil { return } if req.StartTime == 0 || req.EndTime == 0 { // 未传时间,默认展示1小时之内的异常事件告警 req.EndTime = time.Now().Unix() req.StartTime = req.EndTime - int64((60 * time.Minute).Seconds()) } result := make([]dto.EventStatisticResp, 0) if err := se.EventStatistic(req, &result); err != nil { e.Error(http.StatusInternalServerError, err, fmt.Sprintf("异常统计失败: %s", err.Error())) return } e.OK(result, "sucess") } func (e Event) EventList(c *gin.Context) { req := new(dto.EventStatisticReq) se := new(service.Event) if err := e.Init(c, req, &se.OtService); err != nil { return } if req.StartTime == 0 || req.EndTime == 0 { // 未传时间,默认展示1小时之内的异常事件告警 req.EndTime = time.Now().Unix() req.StartTime = req.EndTime - int64((60 * time.Minute).Seconds()) } result := make([]dto.EventListsResp, 0) if err := se.EventLists(req, &result); err != nil { e.Error(http.StatusInternalServerError, err, fmt.Sprintf("聚合异常失败: %s", err.Error())) return } e.OK(result, "sucess") }