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 (
"fmt"
"net/http"
log "github.com/cihub/seelog"
"infini.sh/console/model"
"infini.sh/console/model/alerting"
httprouter "infini.sh/framework/core/api/router"
"infini.sh/framework/core/global"
"infini.sh/framework/core/orm"
"infini.sh/framework/core/util"
"net/http"
)
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
}
resBody = append(resBody, ExportData{
Type: meta.Type,
Data: result.Result,
Version: global.Env().GetVersion(),
Type: meta.Type,
Data: result.Result,
})
}
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)
return
}
err = indexExportData(reqBody)
needPatch := true
if len(reqBody) > 0 && len(reqBody[0].Version) > 0 {
needPatch = false
}
err = indexExportData(reqBody, needPatch)
if err != nil {
log.Error(err)
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)
}
func indexExportData(eds []ExportData) error {
func indexExportData(eds []ExportData, patch bool) error {
for _, ed := range eds {
for _, row := range ed.Data {
var obj interface{}
@ -73,6 +80,15 @@ func indexExportData(eds []ExportData) error {
}
buf := util.MustToJSONBytes(row)
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 {
return err
}
@ -81,7 +97,6 @@ func indexExportData(eds []ExportData) error {
return err
}
}
}
return nil
}
@ -103,7 +118,7 @@ func getExportData(meta ExportMetadata) (*orm.Result, error) {
}
if meta.Filter != nil {
query := util.MapStr{
"size": 1000,
"size": 1000,
"query": meta.Filter,
}
q.RawQuery = util.MustToJSONBytes(query)
@ -113,4 +128,4 @@ func getExportData(meta ExportMetadata) (*orm.Result, error) {
return nil, err
}
return &result, err
}
}

View File

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