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:
parent
10c317f07c
commit
659a30c1a1
|
@ -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,6 +34,7 @@ func (h *DataAPI) exportData(w http.ResponseWriter, req *http.Request, ps httpro
|
|||
return
|
||||
}
|
||||
resBody = append(resBody, ExportData{
|
||||
Version: global.Env().GetVersion(),
|
||||
Type: meta.Type,
|
||||
Data: result.Result,
|
||||
})
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ type ExportMetadata struct {
|
|||
}
|
||||
|
||||
type ExportData struct {
|
||||
Version string `json:"version,omitempty"`
|
||||
Type string `json:"type"`
|
||||
Data []interface{} `json:"data"`
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue