fix: alert rule import patch for v1.6.0 (#346)

修复[344](https://git.infini.ltd/infini/console/issues/344)

Reviewed-on: https://git.infini.ltd/infini/console/pulls/346
Co-authored-by: hardy <luohf@infinilabs.com>
Co-committed-by: hardy <luohf@infinilabs.com>
This commit is contained in:
hardy 2024-05-08 08:58:56 +08:00 committed by medcl
parent 10c317f07c
commit 659a30c1a1
2 changed files with 30 additions and 14 deletions

View File

@ -6,13 +6,15 @@ package data
import ( import (
"fmt" "fmt"
"net/http"
log "github.com/cihub/seelog" log "github.com/cihub/seelog"
"infini.sh/console/model" "infini.sh/console/model"
"infini.sh/console/model/alerting" "infini.sh/console/model/alerting"
httprouter "infini.sh/framework/core/api/router" httprouter "infini.sh/framework/core/api/router"
"infini.sh/framework/core/global"
"infini.sh/framework/core/orm" "infini.sh/framework/core/orm"
"infini.sh/framework/core/util" "infini.sh/framework/core/util"
"net/http"
) )
func (h *DataAPI) exportData(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { func (h *DataAPI) exportData(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
@ -32,8 +34,9 @@ func (h *DataAPI) exportData(w http.ResponseWriter, req *http.Request, ps httpro
return return
} }
resBody = append(resBody, ExportData{ resBody = append(resBody, ExportData{
Type: meta.Type, Version: global.Env().GetVersion(),
Data: result.Result, Type: meta.Type,
Data: result.Result,
}) })
} }
h.WriteJSON(w, resBody, http.StatusOK) h.WriteJSON(w, resBody, http.StatusOK)
@ -48,7 +51,11 @@ func (h *DataAPI) importData(w http.ResponseWriter, req *http.Request, ps httpro
h.WriteError(w, err.Error(), http.StatusInternalServerError) h.WriteError(w, err.Error(), http.StatusInternalServerError)
return return
} }
err = indexExportData(reqBody) needPatch := true
if len(reqBody) > 0 && len(reqBody[0].Version) > 0 {
needPatch = false
}
err = indexExportData(reqBody, needPatch)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
h.WriteError(w, err.Error(), http.StatusInternalServerError) h.WriteError(w, err.Error(), http.StatusInternalServerError)
@ -57,7 +64,7 @@ func (h *DataAPI) importData(w http.ResponseWriter, req *http.Request, ps httpro
h.WriteAckOKJSON(w) h.WriteAckOKJSON(w)
} }
func indexExportData(eds []ExportData) error { func indexExportData(eds []ExportData, patch bool) error {
for _, ed := range eds { for _, ed := range eds {
for _, row := range ed.Data { for _, row := range ed.Data {
var obj interface{} var obj interface{}
@ -73,6 +80,15 @@ func indexExportData(eds []ExportData) error {
} }
buf := util.MustToJSONBytes(row) buf := util.MustToJSONBytes(row)
err := util.FromJSONBytes(buf, obj) err := util.FromJSONBytes(buf, obj)
// 当导出无版本号,并且为告警规则,并且导出数据无分类时
if patch && ed.Type == DataTypeAlertRule {
if rule, ok := obj.(*alerting.Rule); ok {
if len(obj.(*alerting.Rule).Category) == 0 {
rule.Category = "Platform"
obj = rule
}
}
}
if err != nil { if err != nil {
return err return err
} }
@ -81,7 +97,6 @@ func indexExportData(eds []ExportData) error {
return err return err
} }
} }
} }
return nil return nil
} }
@ -103,7 +118,7 @@ func getExportData(meta ExportMetadata) (*orm.Result, error) {
} }
if meta.Filter != nil { if meta.Filter != nil {
query := util.MapStr{ query := util.MapStr{
"size": 1000, "size": 1000,
"query": meta.Filter, "query": meta.Filter,
} }
q.RawQuery = util.MustToJSONBytes(query) q.RawQuery = util.MustToJSONBytes(query)
@ -113,4 +128,4 @@ func getExportData(meta ExportMetadata) (*orm.Result, error) {
return nil, err return nil, err
} }
return &result, err return &result, err
} }

View File

@ -9,17 +9,18 @@ type ExportDataRequest struct {
} }
type ExportMetadata struct { type ExportMetadata struct {
Type string `json:"type"` Type string `json:"type"`
Filter interface{} `json:"filter,omitempty"` Filter interface{} `json:"filter,omitempty"`
} }
type ExportData struct { type ExportData struct {
Type string `json:"type"` Version string `json:"version,omitempty"`
Data []interface{} `json:"data"` Type string `json:"type"`
Data []interface{} `json:"data"`
} }
const ( const (
DataTypeAlertRule = "AlertRule" DataTypeAlertRule = "AlertRule"
DataTypeAlertChannel = "AlertChannel" DataTypeAlertChannel = "AlertChannel"
DataTypeAlertEmailServer = "EmailServer" DataTypeAlertEmailServer = "EmailServer"
) )