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 ✅
150 lines
5.1 KiB
Go
150 lines
5.1 KiB
Go
package controller
|
||
|
||
import (
|
||
"encoding/json"
|
||
"net/http"
|
||
"strconv"
|
||
|
||
"ezops/model"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// 告警联系人 + 联系组 CRUD(AlertHandler 方法接收者) // Logan.Li
|
||
|
||
// ==================== 联系人 ====================
|
||
|
||
// ListContacts GET /alerts/contacts
|
||
func (h *AlertHandler) ListContacts(c *gin.Context) {
|
||
var contacts []model.AlertContact
|
||
h.db.Order("created_at DESC").Find(&contacts)
|
||
c.JSON(http.StatusOK, model.Response{
|
||
Code: 200,
|
||
Message: "success",
|
||
Data: model.ListResponse{List: contacts, Total: int64(len(contacts))},
|
||
})
|
||
}
|
||
|
||
// CreateContact POST /alerts/contacts
|
||
func (h *AlertHandler) CreateContact(c *gin.Context) {
|
||
var req model.CreateContactRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, model.Response{Code: 400, Message: "请求参数错误: " + err.Error()})
|
||
return
|
||
}
|
||
contact := model.AlertContact{
|
||
Name: req.Name,
|
||
Phone: req.Phone,
|
||
Email: req.Email,
|
||
ChannelType: req.ChannelType,
|
||
WebhookURL: req.WebhookURL,
|
||
}
|
||
if err := h.db.Create(&contact).Error; err != nil {
|
||
c.JSON(http.StatusInternalServerError, model.Response{Code: 500, Message: "创建失败"})
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, model.Response{Code: 200, Message: "创建成功", Data: contact})
|
||
}
|
||
|
||
// UpdateContact PUT /alerts/contacts/:id
|
||
func (h *AlertHandler) UpdateContact(c *gin.Context) {
|
||
id, _ := strconv.Atoi(c.Param("id"))
|
||
var req model.UpdateContactRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, model.Response{Code: 400, Message: "请求参数错误"})
|
||
return
|
||
}
|
||
var contact model.AlertContact
|
||
if err := h.db.First(&contact, id).Error; err != nil {
|
||
c.JSON(http.StatusNotFound, model.Response{Code: 404, Message: "联系人不存在"})
|
||
return
|
||
}
|
||
updates := map[string]interface{}{}
|
||
if req.Name != "" { updates["name"] = req.Name }
|
||
if req.Phone != "" { updates["phone"] = req.Phone }
|
||
if req.Email != "" { updates["email"] = req.Email }
|
||
if req.ChannelType != "" { updates["channel_type"] = req.ChannelType }
|
||
if req.WebhookURL != "" { updates["webhook_url"] = req.WebhookURL }
|
||
h.db.Model(&contact).Updates(updates)
|
||
h.db.First(&contact, id)
|
||
c.JSON(http.StatusOK, model.Response{Code: 200, Message: "更新成功", Data: contact})
|
||
}
|
||
|
||
// DeleteContact DELETE /alerts/contacts/:id
|
||
func (h *AlertHandler) DeleteContact(c *gin.Context) {
|
||
id, _ := strconv.Atoi(c.Param("id"))
|
||
if err := h.db.Delete(&model.AlertContact{}, id).Error; err != nil {
|
||
c.JSON(http.StatusInternalServerError, model.Response{Code: 500, Message: "删除失败"})
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, model.Response{Code: 200, Message: "删除成功"})
|
||
}
|
||
|
||
// ==================== 联系组 ====================
|
||
|
||
// ListContactGroups GET /alerts/contact-groups
|
||
func (h *AlertHandler) ListContactGroups(c *gin.Context) {
|
||
var groups []model.AlertContactGroup
|
||
h.db.Order("created_at DESC").Find(&groups)
|
||
c.JSON(http.StatusOK, model.Response{
|
||
Code: 200,
|
||
Message: "success",
|
||
Data: model.ListResponse{List: groups, Total: int64(len(groups))},
|
||
})
|
||
}
|
||
|
||
// CreateContactGroup POST /alerts/contact-groups
|
||
func (h *AlertHandler) CreateContactGroup(c *gin.Context) {
|
||
var req model.CreateContactGroupRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, model.Response{Code: 400, Message: "请求参数错误: " + err.Error()})
|
||
return
|
||
}
|
||
idsJSON, _ := json.Marshal(req.ContactIDs)
|
||
group := model.AlertContactGroup{
|
||
GroupName: req.GroupName,
|
||
Notes: req.Notes,
|
||
ContactIDs: string(idsJSON),
|
||
}
|
||
if err := h.db.Create(&group).Error; err != nil {
|
||
c.JSON(http.StatusInternalServerError, model.Response{Code: 500, Message: "创建失败"})
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, model.Response{Code: 200, Message: "创建成功", Data: group})
|
||
}
|
||
|
||
// UpdateContactGroup PUT /alerts/contact-groups/:id
|
||
func (h *AlertHandler) UpdateContactGroup(c *gin.Context) {
|
||
id, _ := strconv.Atoi(c.Param("id"))
|
||
var req model.UpdateContactGroupRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, model.Response{Code: 400, Message: "请求参数错误"})
|
||
return
|
||
}
|
||
var group model.AlertContactGroup
|
||
if err := h.db.First(&group, id).Error; err != nil {
|
||
c.JSON(http.StatusNotFound, model.Response{Code: 404, Message: "联系组不存在"})
|
||
return
|
||
}
|
||
updates := map[string]interface{}{}
|
||
if req.GroupName != "" { updates["group_name"] = req.GroupName }
|
||
if req.Notes != "" { updates["notes"] = req.Notes }
|
||
if len(req.ContactIDs) > 0 {
|
||
idsJSON, _ := json.Marshal(req.ContactIDs)
|
||
updates["contact_ids"] = string(idsJSON)
|
||
}
|
||
h.db.Model(&group).Updates(updates)
|
||
h.db.First(&group, id)
|
||
c.JSON(http.StatusOK, model.Response{Code: 200, Message: "更新成功", Data: group})
|
||
}
|
||
|
||
// DeleteContactGroup DELETE /alerts/contact-groups/:id
|
||
func (h *AlertHandler) DeleteContactGroup(c *gin.Context) {
|
||
id, _ := strconv.Atoi(c.Param("id"))
|
||
if err := h.db.Delete(&model.AlertContactGroup{}, id).Error; err != nil {
|
||
c.JSON(http.StatusInternalServerError, model.Response{Code: 500, Message: "删除失败"})
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, model.Response{Code: 200, Message: "删除成功"})
|
||
}
|