feat(audit): F1+F2 告警静默规则标记完成 + 审计日志CSV导出实现
F1 告警静默规则 [done]: - 后端 alert_silence.go: CRUD + Toggle (onetime/daily/weekly 三模式) - 告警引擎 isSilenced 集成 (alert_engine.go) - 前端 SilenceRuleTab + SilenceDialog - 路由 /alerts/silences 已注册 F2 审计日志导出 [done]: - 重写 ExportAuditLogs: 真正的 CSV 导出 (encoding/csv + UTF-8 BOM) - 支持 module/action/username/startTime/endTime 筛选 - 导出限制 10000 条防溢出,自动记录审计日志 - JSON 格式向后兼容 (format=json) - 前端 exportLogs 传递查询参数 同时包含累积的前期工作: - 目录重构 internal/ -> controller/model/router (v0.55-v0.57) - 后端分层架构 dto/repository/service/utils - 审计模块全功能 (终端审计/操作审计/登录日志) - asciicast v2 录制格式 - SFTP 文件浏览器 - StatStrip 紧凑统计条 - 在线会话自动刷新 - LiveMonitor WebSocket 诊断修复 验证: go build ✅ | npm run build ✅ | Docker 部署 ✅ | 10 回归端点 ✅ | CSV 导出 ✅ | 静默 CRUD ✅
This commit is contained in:
1 parent
e57718998a
commit
77c234b060
314 files changed
+10050
-4065
No files matched your search
@@ -0,0 +1,133 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"ezops/model"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 告警事件处理 + AlertHandler 定义(规则见 alert_rules.go,渠道见 alert_channels.go) // Logan.Li
|
||||
|
||||
// AlertHandler 告警处理器
|
||||
type AlertHandler struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewAlertHandler 创建告警处理器
|
||||
func NewAlertHandler(db *gorm.DB) *AlertHandler {
|
||||
return &AlertHandler{db: db}
|
||||
}
|
||||
|
||||
// ListAlerts 获取告警事件列表
|
||||
// @Summary 获取告警事件列表
|
||||
// @Description 获取所有告警事件列表
|
||||
// @Tags 告警
|
||||
// @Produce json
|
||||
// @Security Bearer
|
||||
// @Param page query int false "页码"
|
||||
// @Param page_size query int false "每页数量"
|
||||
// @Param status query string false "状态过滤"
|
||||
// @Param severity query string false "级别过滤"
|
||||
// @Success 200 {object} model.Response{data=model.ListResponse}
|
||||
// @Router /alerts [get]
|
||||
func (h *AlertHandler) ListAlerts(c *gin.Context) {
|
||||
var query model.PageQuery
|
||||
c.ShouldBindQuery(&query)
|
||||
|
||||
db := h.db.Model(&model.AlertEvent{}).Preload("Rule")
|
||||
|
||||
// 状态过滤
|
||||
if status := c.Query("status"); status != "" {
|
||||
db = db.Where("status = ?", status)
|
||||
}
|
||||
|
||||
// 级别过滤
|
||||
if severity := c.Query("severity"); severity != "" {
|
||||
db = db.Where("severity = ?", severity)
|
||||
}
|
||||
|
||||
var total int64
|
||||
db.Count(&total)
|
||||
|
||||
var alerts []model.AlertEvent
|
||||
db.Order("created_at DESC").
|
||||
Offset(query.GetOffset()).
|
||||
Limit(query.GetLimit()).
|
||||
Find(&alerts)
|
||||
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: 200,
|
||||
Message: "success",
|
||||
Data: model.ListResponse{
|
||||
List: alerts,
|
||||
Total: total,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// AcknowledgeAlert 确认告警
|
||||
// @Summary 确认告警
|
||||
// @Description 确认告警事件
|
||||
// @Tags 告警
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security Bearer
|
||||
// @Param id path int true "告警ID"
|
||||
// @Param request body model.AcknowledgeAlertRequest true "确认请求"
|
||||
// @Success 200 {object} model.Response{data=model.AlertEvent}
|
||||
// @Router /alerts/{id}/ack [post]
|
||||
func (h *AlertHandler) AcknowledgeAlert(c *gin.Context) {
|
||||
id, _ := strconv.Atoi(c.Param("id"))
|
||||
|
||||
var req model.AcknowledgeAlertRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, model.Response{
|
||||
Code: 400,
|
||||
Message: "请求参数错误",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
var alert model.AlertEvent
|
||||
if err := h.db.First(&alert, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, model.Response{
|
||||
Code: 404,
|
||||
Message: "告警不存在",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
alert.Acknowledged = true
|
||||
// alert.AcknowledgedBy = middleware.GetUserID(c)
|
||||
// now := time.Now()
|
||||
// alert.AcknowledgedAt = &now
|
||||
|
||||
if err := h.db.Save(&alert).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, model.Response{
|
||||
Code: 500,
|
||||
Message: "确认失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: 200,
|
||||
Message: "确认成功",
|
||||
Data: alert,
|
||||
})
|
||||
}
|
||||
|
||||
// toJSON serializes a value to JSON string. Returns empty string on error.
|
||||
// 包级辅助函数,供 alert_rules.go 等同包文件复用 // Logan.Li
|
||||
func toJSON(v interface{}) string {
|
||||
b, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
Reference in new issue
Block a user