fix: alert config log text

This commit is contained in:
guoyuqi
2026-10-14 18:51:05 +08:00
parent 547f9775b0
commit 363e950aad
7 changed files with 88 additions and 60 deletions
+27 -4
View File
@@ -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
}
+21
View File
@@ -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))
}
}
+10 -14
View File
@@ -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": [
+9 -12
View File
@@ -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": []
}
}
},
+9 -12
View File
@@ -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": []
}
}
},
+10 -14
View File
@@ -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": [
+2 -4
View File
@@ -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 {