167 lines
4.5 KiB
Go
167 lines
4.5 KiB
Go
package v1
|
|
|
|
import (
|
|
"encoding/csv"
|
|
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/controller"
|
|
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/model"
|
|
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/model/event"
|
|
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/pkg/errors"
|
|
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/pkg/request"
|
|
"github.com/gin-gonic/gin"
|
|
"time"
|
|
)
|
|
|
|
func GetEventRecordById(c *gin.Context) {
|
|
er := &event.EventRecordStruct{}
|
|
eventId := c.Param("eventId")
|
|
mRule, err := er.GetById(eventId)
|
|
if err != nil {
|
|
controller.FailCode(c, errors.InvalidParameter, err, "请检查参数!")
|
|
return
|
|
}
|
|
controller.Success(c, mRule)
|
|
}
|
|
|
|
type EventRecordCondition struct {
|
|
EventName string `json:"eventName"`
|
|
ResourceName string `json:"resourceName"`
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
type EventRecordPageBody struct {
|
|
PageInfo *request.PageInfo `json:"pageInfo" binding:"required"`
|
|
EventRecordCondition
|
|
}
|
|
|
|
func PageEventRecords(c *gin.Context) {
|
|
var pageBody EventRecordPageBody
|
|
err := c.ShouldBindJSON(&pageBody)
|
|
if err != nil {
|
|
controller.FailCode(c, errors.InvalidParameter, err, "请检查参数!")
|
|
return
|
|
}
|
|
er := &event.EventRecordStruct{}
|
|
fields := map[string]interface{}{}
|
|
if pageBody.EventName != "" {
|
|
fields["event_name"] = pageBody.EventName
|
|
}
|
|
if pageBody.ResourceName != "" {
|
|
fields["resource_name"] = pageBody.ResourceName
|
|
}
|
|
if pageBody.Status != "" {
|
|
fields["task_status"] = pageBody.Status
|
|
}
|
|
page := &model.Page[event.EventRecordStruct]{}
|
|
page.Covert(pageBody.PageInfo)
|
|
err = er.Page(page, fields)
|
|
if err != nil {
|
|
controller.FailCode(c, errors.ServerError, err, "内部错误!")
|
|
return
|
|
}
|
|
controller.Success(c, page)
|
|
}
|
|
|
|
func ExportEventRecords(c *gin.Context) {
|
|
var condition EventRecordCondition
|
|
err := c.ShouldBindJSON(&condition)
|
|
if err != nil {
|
|
controller.FailCode(c, errors.InvalidParameter, err, "请检查参数!")
|
|
return
|
|
}
|
|
er := &event.EventRecordStruct{}
|
|
fields := map[string]interface{}{}
|
|
if condition.EventName != "" {
|
|
fields["event_name"] = condition.EventName
|
|
}
|
|
if condition.ResourceName != "" {
|
|
fields["resource_name"] = condition.ResourceName
|
|
}
|
|
if condition.Status != "" {
|
|
fields["task_status"] = condition.Status
|
|
}
|
|
count, err := er.Count(fields)
|
|
if err != nil {
|
|
controller.FailCode(c, errors.ServerError, err, "服务器错误!")
|
|
return
|
|
}
|
|
|
|
if count > 1000 {
|
|
controller.FailCode(c, errors.InvalidParameter, nil, "数据量过大,请调整查询范围!")
|
|
return
|
|
}
|
|
|
|
events, err := er.GetEvents(fields)
|
|
if err != nil {
|
|
controller.FailCode(c, errors.ServerError, err, "服务器错误!")
|
|
return
|
|
}
|
|
|
|
c.Writer.Header().Set("Content-Disposition", "attachment; filename=events.xlsx")
|
|
c.Writer.Header().Set("Content-Type", "application/octet-stream")
|
|
w := csv.NewWriter(c.Writer)
|
|
c.Writer.Write([]byte("\xEF\xBB\xBF"))
|
|
w.Write([]string{"事件等级", "事件编号", "事件名称", "资源名称", "资源类型", "所在算力节点", "发生时间", "事件内容", "处理状态"})
|
|
for _, record := range events {
|
|
w.Write([]string{GetLevel(record.Level), record.Id, record.EventName, record.ResourceName, record.ResourceType, record.ResourceNode, GetTimeStr(record.EventTime), record.EventInfo, GetStatus(record.TaskStatus)})
|
|
}
|
|
|
|
w.Flush()
|
|
c.Writer.Flush()
|
|
}
|
|
|
|
func GetLevel(level string) string {
|
|
if level == "WARN" {
|
|
return "警告"
|
|
} else if level == "CRITICAL" {
|
|
return "严重"
|
|
} else {
|
|
return "信息"
|
|
}
|
|
}
|
|
|
|
func GetStatus(status string) string {
|
|
if status == "0" {
|
|
return "初始化"
|
|
} else if status == "1" {
|
|
return "成功"
|
|
} else if status == "2" {
|
|
return "失败"
|
|
} else if status == "3" {
|
|
return "执行中"
|
|
} else if status == "4" {
|
|
return "触发失败"
|
|
} else {
|
|
return "未处理"
|
|
}
|
|
}
|
|
|
|
func GetTimeStr(t time.Time) string {
|
|
return t.Local().Format("2006-01-02 15:04:05")
|
|
}
|
|
|
|
func EventRecordsStatistic(c *gin.Context) {
|
|
var condition EventRecordCondition
|
|
err := c.ShouldBindJSON(&condition)
|
|
if err != nil {
|
|
controller.FailCode(c, errors.InvalidParameter, err, "请检查参数!")
|
|
return
|
|
}
|
|
fields := map[string]interface{}{}
|
|
if condition.EventName != "" {
|
|
fields["event_name"] = condition.EventName
|
|
}
|
|
if condition.ResourceName != "" {
|
|
fields["resource_name"] = condition.ResourceName
|
|
}
|
|
if condition.Status != "" {
|
|
fields["task_status"] = condition.Status
|
|
}
|
|
er := &event.EventRecordStruct{}
|
|
statistic, err := er.Statistic(fields)
|
|
if err != nil {
|
|
controller.FailCode(c, errors.ServerError, err, "服务器错误!")
|
|
return
|
|
}
|
|
controller.Success(c, statistic)
|
|
}
|