diff --git a/agent/app/api/v2/alert.go b/agent/app/api/v2/alert.go index 01cc39522..260ebf60c 100644 --- a/agent/app/api/v2/alert.go +++ b/agent/app/api/v2/alert.go @@ -1,6 +1,7 @@ package v2 import ( + "encoding/json" "errors" "net/http" "net/url" @@ -293,7 +294,7 @@ func (b *BaseApi) PageAlertConfig(c *gin.Context) { // @Security ApiKeyAuth // @Security Timestamp // @Router /alert/config/update [post] -// @x-panel-log {"bodyKeys":["id","displayName"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新告警配置 [id][displayName]","formatEN":"update alert config [id][displayName]"} +// @x-panel-log {"bodyKeys":["displayName"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新告警配置 [displayName]","formatEN":"update alert config [displayName]"} func (b *BaseApi) UpdateAlertConfig(c *gin.Context) { var req dto.AlertConfigUpdate if err := helper.CheckBindAndValidate(&req, c); err != nil { @@ -321,13 +322,15 @@ func (b *BaseApi) UpdateAlertConfig(c *gin.Context) { // @Security ApiKeyAuth // @Security Timestamp // @Router /alert/config/status [post] -// @x-panel-log {"bodyKeys":["id","status"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新告警配置状态 [id][status]","formatEN":"update alert config status [id][status]"} +// @x-panel-log {"bodyKeys":["status"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新告警配置状态 [name][status]","formatEN":"update alert config status [name][status]"} func (b *BaseApi) UpdateAlertConfigStatus(c *gin.Context) { var req dto.AlertConfigStatusUpdate if err := helper.CheckBindAndValidate(&req, c); err != nil { return } - if err := alertService.UpdateAlertConfigStatus(req, loadAuditUser(c)); err != nil { + err := alertService.UpdateAlertConfigStatus(req, loadAuditUser(c)) + setAlertConfigLogName(c, req.ID) + if err != nil { helper.InternalServer(c, err) return } @@ -353,12 +356,13 @@ func loadAuditUser(c *gin.Context) string { // @Security ApiKeyAuth // @Security Timestamp // @Router /alert/config/del [post] -// @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"删除告警配置 [id]","formatEN":"delete alert config [id]"} +// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"删除告警配置 [name]","formatEN":"delete alert config [name]"} func (b *BaseApi) DeleteAlertConfig(c *gin.Context) { var req dto.DeleteRequest if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + setAlertConfigLogName(c, req.ID) err := alertService.DeleteAlertConfig(req.ID) if err != nil { helper.InternalServer(c, err) @@ -410,3 +414,22 @@ func (b *BaseApi) TestAlertConfig(c *gin.Context) { } helper.SuccessWithData(c, flag) } + +// Resolve the current name for the operation log. Call before deleting the record. +func setAlertConfigLogName(c *gin.Context, id uint) { + name := "" + if config, err := repo.NewIAlertRepo().GetConfigById(id); err == nil { + name = alertConfigLogName(config.Config, config.Title) + } + helper.SetOperationLogValues(c, map[string]string{"name": name}) +} + +func alertConfigLogName(config, title string) string { + var value struct { + DisplayName string `json:"displayName"` + } + if json.Unmarshal([]byte(config), &value) == nil && strings.TrimSpace(value.DisplayName) != "" { + return strings.TrimSpace(value.DisplayName) + } + return title +} diff --git a/agent/app/api/v2/helper/helper.go b/agent/app/api/v2/helper/helper.go index e15c4244a..b5e4e0454 100644 --- a/agent/app/api/v2/helper/helper.go +++ b/agent/app/api/v2/helper/helper.go @@ -3,6 +3,7 @@ package helper import ( "compress/gzip" "context" + "encoding/base64" "encoding/json" "fmt" "net/http" @@ -184,3 +185,23 @@ func GetParamInt32(paramName string, c *gin.Context) (int32, error) { intNum, _ := strconv.Atoi(idParam) return int32(intNum), nil } + +// SetOperationLogValues supplies server-resolved values to Core's operation log. +// Call before writing the response. Only pass public audit fields, never credentials. +func SetOperationLogValues(c *gin.Context, values map[string]string) { + const header = "X-Op-Resolved" + resolved := make(map[string]string) + if data, err := base64.RawURLEncoding.DecodeString(c.Writer.Header().Get(header)); err == nil { + _ = json.Unmarshal(data, &resolved) + } + if resolved == nil { + resolved = make(map[string]string) + } + for key, value := range values { + resolved[key] = value + } + data, err := json.Marshal(resolved) + if err == nil { + c.Header(header, base64.RawURLEncoding.EncodeToString(data)) + } +} diff --git a/agent/cmd/server/docs/x-log.json b/agent/cmd/server/docs/x-log.json index d701570be..8da7eeb69 100644 --- a/agent/cmd/server/docs/x-log.json +++ b/agent/cmd/server/docs/x-log.json @@ -108,33 +108,29 @@ "formatEN": "create alert [title]" }, "/alert/config/del": { - "bodyKeys": [ - "id" - ], + "bodyKeys": [], "paramKeys": [], - "beforeFunctions": [], - "formatZH": "删除告警配置 [id]", - "formatEN": "delete alert config [id]" + "formatZH": "删除告警配置 [name]", + "formatEN": "delete alert config [name]", + "beforeFunctions": [] }, "/alert/config/status": { "bodyKeys": [ - "id", "status" ], "paramKeys": [], - "beforeFunctions": [], - "formatZH": "更新告警配置状态 [id][status]", - "formatEN": "update alert config status [id][status]" + "formatZH": "更新告警配置状态 [name][status]", + "formatEN": "update alert config status [name][status]", + "beforeFunctions": [] }, "/alert/config/update": { "bodyKeys": [ - "id", "displayName" ], "paramKeys": [], - "beforeFunctions": [], - "formatZH": "更新告警配置 [id][displayName]", - "formatEN": "update alert config [id][displayName]" + "formatZH": "更新告警配置 [displayName]", + "formatEN": "update alert config [displayName]", + "beforeFunctions": [] }, "/alert/del": { "bodyKeys": [ diff --git a/core/cmd/server/docs/docs.go b/core/cmd/server/docs/docs.go index 10ee65c37..def903419 100644 --- a/core/cmd/server/docs/docs.go +++ b/core/cmd/server/docs/docs.go @@ -3906,13 +3906,11 @@ const docTemplate = `{ "Alert" ], "x-panel-log": { - "BeforeFunctions": [], - "bodyKeys": [ - "id" - ], - "formatEN": "delete alert config [id]", - "formatZH": "删除告警配置 [id]", - "paramKeys": [] + "bodyKeys": [], + "paramKeys": [], + "formatZH": "删除告警配置 [name]", + "formatEN": "delete alert config [name]", + "BeforeFunctions": [] } } }, @@ -4041,14 +4039,13 @@ const docTemplate = `{ "Alert" ], "x-panel-log": { - "BeforeFunctions": [], "bodyKeys": [ - "id", "displayName" ], - "formatEN": "update alert config [id][displayName]", - "formatZH": "更新告警配置 [id][displayName]", - "paramKeys": [] + "paramKeys": [], + "formatZH": "更新告警配置 [displayName]", + "formatEN": "update alert config [displayName]", + "BeforeFunctions": [] } } }, diff --git a/core/cmd/server/docs/swagger.json b/core/cmd/server/docs/swagger.json index 549829bd8..254f8cb10 100644 --- a/core/cmd/server/docs/swagger.json +++ b/core/cmd/server/docs/swagger.json @@ -3902,13 +3902,11 @@ "Alert" ], "x-panel-log": { - "BeforeFunctions": [], - "bodyKeys": [ - "id" - ], - "formatEN": "delete alert config [id]", - "formatZH": "删除告警配置 [id]", - "paramKeys": [] + "bodyKeys": [], + "paramKeys": [], + "formatZH": "删除告警配置 [name]", + "formatEN": "delete alert config [name]", + "BeforeFunctions": [] } } }, @@ -4037,14 +4035,13 @@ "Alert" ], "x-panel-log": { - "BeforeFunctions": [], "bodyKeys": [ - "id", "displayName" ], - "formatEN": "update alert config [id][displayName]", - "formatZH": "更新告警配置 [id][displayName]", - "paramKeys": [] + "paramKeys": [], + "formatZH": "更新告警配置 [displayName]", + "formatEN": "update alert config [displayName]", + "BeforeFunctions": [] } } }, diff --git a/core/cmd/server/docs/x-log.json b/core/cmd/server/docs/x-log.json index d701570be..8da7eeb69 100644 --- a/core/cmd/server/docs/x-log.json +++ b/core/cmd/server/docs/x-log.json @@ -108,33 +108,29 @@ "formatEN": "create alert [title]" }, "/alert/config/del": { - "bodyKeys": [ - "id" - ], + "bodyKeys": [], "paramKeys": [], - "beforeFunctions": [], - "formatZH": "删除告警配置 [id]", - "formatEN": "delete alert config [id]" + "formatZH": "删除告警配置 [name]", + "formatEN": "delete alert config [name]", + "beforeFunctions": [] }, "/alert/config/status": { "bodyKeys": [ - "id", "status" ], "paramKeys": [], - "beforeFunctions": [], - "formatZH": "更新告警配置状态 [id][status]", - "formatEN": "update alert config status [id][status]" + "formatZH": "更新告警配置状态 [name][status]", + "formatEN": "update alert config status [name][status]", + "beforeFunctions": [] }, "/alert/config/update": { "bodyKeys": [ - "id", "displayName" ], "paramKeys": [], - "beforeFunctions": [], - "formatZH": "更新告警配置 [id][displayName]", - "formatEN": "update alert config [id][displayName]" + "formatZH": "更新告警配置 [displayName]", + "formatEN": "update alert config [displayName]", + "beforeFunctions": [] }, "/alert/del": { "bodyKeys": [ diff --git a/core/middleware/operation.go b/core/middleware/operation.go index 5e35f203b..ca1ea76b2 100644 --- a/core/middleware/operation.go +++ b/core/middleware/operation.go @@ -133,11 +133,9 @@ func OperationLog() gin.HandlerFunc { record.AuthMethod = "session" } + // Handlers may supply audit values even without database resolution metadata. + mergeResolvedData(writer.resolvedHeader, formatMap) if len(operationDic.BeforeFunctions) != 0 { - if needAgentResolve { - mergeResolvedData(writer.resolvedHeader, formatMap) - } - if allowCoreFallback && !hasAllResolvedData(formatMap, operationDic.BeforeFunctions) { dbItem, err := newDB(record.Path) if err == nil {