feat: implement V2 alert center and scoped alert governance

This commit is contained in:
Yvan
2026-11-05 20:08:29 +08:00
parent f8e26c5baf
commit 999c9a6315
57 changed files with 2711 additions and 216 deletions
@@ -0,0 +1,99 @@
package handler
import (
"github.com/gin-gonic/gin"
"github.com/go-kratos/kratos/v3/errors"
"kra/internal/service/business"
"kra/internal/service/dto"
"net/http"
"strconv"
)
type AlertGovernance struct {
service *business.AlertGovernanceService
}
func (h *AlertGovernance) Candidates(c *gin.Context) {
values, err := h.service.Candidates(c.Request.Context(), c.Query("keyword"))
if err != nil {
Fail(c, errors.FromError(err).Message)
return
}
OKWithData(c, values)
}
func NewAlertGovernance(service *business.AlertGovernanceService) *AlertGovernance {
return &AlertGovernance{service}
}
func (h *AlertGovernance) Gap(c *gin.Context) {
v, err := h.service.Gap(c.Request.Context())
if err != nil {
Fail(c, errors.FromError(err).Message)
return
}
OKWithData(c, v)
}
func (h *AlertGovernance) SaveGap(c *gin.Context) {
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 4096)
var req dto.AlertGapRequest
if c.ShouldBindJSON(&req) != nil {
Fail(c, "预警间隔参数不正确")
return
}
if err := h.service.SaveGap(c.Request.Context(), &req); err != nil {
Fail(c, errors.FromError(err).Message)
return
}
OK(c)
}
func (h *AlertGovernance) List(c *gin.Context) {
var req dto.AlertExemptionQuery
if c.ShouldBindQuery(&req) != nil {
Fail(c, "查询参数不正确")
return
}
v, err := h.service.List(c.Request.Context(), &req)
if err != nil {
Fail(c, errors.FromError(err).Message)
return
}
OKWithData(c, v)
}
func (h *AlertGovernance) Add(c *gin.Context) {
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 8192)
var req dto.AlertExemptionRequest
if c.ShouldBindJSON(&req) != nil {
Fail(c, "免预警参数不正确")
return
}
if err := h.service.Add(c.Request.Context(), &req); err != nil {
Fail(c, errors.FromError(err).Message)
return
}
OK(c)
}
func (h *AlertGovernance) Remove(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
Fail(c, "记录 ID 不正确")
return
}
if err = h.service.Remove(c.Request.Context(), id); err != nil {
Fail(c, errors.FromError(err).Message)
return
}
OK(c)
}
func (h *AlertRecord) Ignore(c *gin.Context) {
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 32768)
var req dto.AlertIgnoreRequest
if c.ShouldBindJSON(&req) != nil {
Fail(c, "预警记录参数不正确")
return
}
if err := h.service.Ignore(c.Request.Context(), &req); err != nil {
Fail(c, errors.FromError(err).Message)
return
}
OK(c)
}
+9
View File
@@ -17,6 +17,15 @@ func NewAlertRecord(service *business.AlertRecordService) *AlertRecord {
return &AlertRecord{service: service}
}
func (h *AlertRecord) Platforms(c *gin.Context) {
values, err := h.service.Platforms(c.Request.Context())
if err != nil {
Fail(c, errors.FromError(err).Message)
return
}
OKWithData(c, values)
}
func (h *AlertRecord) List(c *gin.Context) {
var req dto.AlertRecordQuery
if c.ShouldBindQuery(&req) != nil {
+16 -1
View File
@@ -5,6 +5,7 @@ import (
"github.com/go-kratos/kratos/v3/errors"
"kra/internal/service/business"
"kra/internal/service/dto"
"net/http"
"strconv"
)
@@ -46,17 +47,31 @@ func (h *AlertRule) Find(c *gin.Context) {
OKWithData(c, v)
}
func (h *AlertRule) Save(c *gin.Context) {
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 16384)
var req dto.AlertRuleRequest
if err := c.ShouldBindJSON(&req); err != nil {
Fail(c, "预警规则参数不正确")
return
}
var requestedID uint64
if req.ID != "" {
var err error
requestedID, err = strconv.ParseUint(req.ID.String(), 10, 64)
if err != nil {
Fail(c, "规则 ID 不正确")
return
}
}
if (c.Request.Method == http.MethodPost && requestedID != 0) || (c.Request.Method == http.MethodPut && requestedID == 0) {
Fail(c, "新建或编辑规则的 ID 不正确")
return
}
id, err := h.service.Save(c.Request.Context(), &req)
if err != nil {
Fail(c, errors.FromError(err).Message)
return
}
OKWithData(c, gin.H{"id": id})
OKWithData(c, gin.H{"id": strconv.FormatUint(id, 10)})
}
func (h *AlertRule) Delete(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
+1
View File
@@ -10,6 +10,7 @@ var ProviderSet = wire.NewSet(
NewBusiness,
NewBusinessPassword,
NewBusinessPromote,
NewAlertGovernance,
NewAlertRule,
NewAlertRecord,
NewActivation,
+1
View File
@@ -1,6 +1,7 @@
package handler
type Set struct {
AlertGovernance *AlertGovernance
BusinessPerformance *BusinessPerformance
OtherSettings *OtherSettings
Ranking *Ranking