Files
aiops/backend/internal/api/handlers/alert_contacts.go
T
attacker d656520428 feat: v0.34 健康探针(Architecture B) + Agent v2.1 + 端口监控 + 告警联系人重构
- v0.31: AlertContact 重构(channel_type下拉+webhook_url替代三分立字段)
- v0.32: Agent ProcessTop 增强(cmdline/user/cpu_pct/mem_pct)
- v0.33: 端口监控 Tab(监听端口自动发现+down联动告警)
- v0.34: 健康探针(Agent本地执行HTTP/TCP/Exec→结果随心跳上报)
- Agent新增: ContextSwitch/FD/HealthProbe采集器
- 后端新增: health_probe handler + Agent model扩展
- HostMonitor新增Tab: 健康探针/端口监控
2026-06-24 19:18:10 +08:00

150 lines
5.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package handlers
import (
"encoding/json"
"net/http"
"strconv"
"ezops/internal/models"
"github.com/gin-gonic/gin"
)
// 告警联系人 + 联系组 CRUD(AlertHandler 方法接收者) // Logan.Li
// ==================== 联系人 ====================
// ListContacts GET /alerts/contacts
func (h *AlertHandler) ListContacts(c *gin.Context) {
var contacts []models.AlertContact
h.db.Order("created_at DESC").Find(&contacts)
c.JSON(http.StatusOK, models.Response{
Code: 200,
Message: "success",
Data: models.ListResponse{List: contacts, Total: int64(len(contacts))},
})
}
// CreateContact POST /alerts/contacts
func (h *AlertHandler) CreateContact(c *gin.Context) {
var req models.CreateContactRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, models.Response{Code: 400, Message: "请求参数错误: " + err.Error()})
return
}
contact := models.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, models.Response{Code: 500, Message: "创建失败"})
return
}
c.JSON(http.StatusOK, models.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 models.UpdateContactRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, models.Response{Code: 400, Message: "请求参数错误"})
return
}
var contact models.AlertContact
if err := h.db.First(&contact, id).Error; err != nil {
c.JSON(http.StatusNotFound, models.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, models.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(&models.AlertContact{}, id).Error; err != nil {
c.JSON(http.StatusInternalServerError, models.Response{Code: 500, Message: "删除失败"})
return
}
c.JSON(http.StatusOK, models.Response{Code: 200, Message: "删除成功"})
}
// ==================== 联系组 ====================
// ListContactGroups GET /alerts/contact-groups
func (h *AlertHandler) ListContactGroups(c *gin.Context) {
var groups []models.AlertContactGroup
h.db.Order("created_at DESC").Find(&groups)
c.JSON(http.StatusOK, models.Response{
Code: 200,
Message: "success",
Data: models.ListResponse{List: groups, Total: int64(len(groups))},
})
}
// CreateContactGroup POST /alerts/contact-groups
func (h *AlertHandler) CreateContactGroup(c *gin.Context) {
var req models.CreateContactGroupRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, models.Response{Code: 400, Message: "请求参数错误: " + err.Error()})
return
}
idsJSON, _ := json.Marshal(req.ContactIDs)
group := models.AlertContactGroup{
GroupName: req.GroupName,
Notes: req.Notes,
ContactIDs: string(idsJSON),
}
if err := h.db.Create(&group).Error; err != nil {
c.JSON(http.StatusInternalServerError, models.Response{Code: 500, Message: "创建失败"})
return
}
c.JSON(http.StatusOK, models.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 models.UpdateContactGroupRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, models.Response{Code: 400, Message: "请求参数错误"})
return
}
var group models.AlertContactGroup
if err := h.db.First(&group, id).Error; err != nil {
c.JSON(http.StatusNotFound, models.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, models.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(&models.AlertContactGroup{}, id).Error; err != nil {
c.JSON(http.StatusInternalServerError, models.Response{Code: 500, Message: "删除失败"})
return
}
c.JSON(http.StatusOK, models.Response{Code: 200, Message: "删除成功"})
}