| @@ -7,6 +7,7 @@ import ( | |||||
| "strconv" | "strconv" | ||||
| "strings" | "strings" | ||||
| "text/template" | "text/template" | ||||
| "time" | |||||
| "github.com/ccfos/nightingale/v6/pkg/ctx" | "github.com/ccfos/nightingale/v6/pkg/ctx" | ||||
| "github.com/ccfos/nightingale/v6/pkg/tplx" | "github.com/ccfos/nightingale/v6/pkg/tplx" | ||||
| @@ -492,7 +493,7 @@ func (m *AlertCurEvent) UpdateFieldsMap(ctx *ctx.Context, fields map[string]inte | |||||
| func AlertCurEventUpgradeToV6(ctx *ctx.Context, dsm map[string]Datasource) error { | func AlertCurEventUpgradeToV6(ctx *ctx.Context, dsm map[string]Datasource) error { | ||||
| var lst []*AlertCurEvent | var lst []*AlertCurEvent | ||||
| err := DB(ctx).Find(&lst).Error | |||||
| err := DB(ctx).Where("trigger_time > ?", time.Now().Unix()-3600*24*30).Find(&lst).Error | |||||
| if err != nil { | if err != nil { | ||||
| return err | return err | ||||
| } | } | ||||
| @@ -515,7 +516,21 @@ func AlertCurEventUpgradeToV6(ctx *ctx.Context, dsm map[string]Datasource) error | |||||
| b, _ := json.Marshal(ruleConfig) | b, _ := json.Marshal(ruleConfig) | ||||
| lst[i].RuleConfig = string(b) | lst[i].RuleConfig = string(b) | ||||
| err = lst[i].UpdateFieldsMap(ctx, map[string]interface{}{"datasource_id": lst[i].DatasourceId, "rule_config": lst[i].RuleConfig}) | |||||
| if lst[i].RuleProd == "" { | |||||
| lst[i].RuleProd = METRIC | |||||
| } | |||||
| if lst[i].Cate == "" { | |||||
| lst[i].Cate = PROMETHEUS | |||||
| } | |||||
| err = lst[i].UpdateFieldsMap(ctx, map[string]interface{}{ | |||||
| "datasource_id": lst[i].DatasourceId, | |||||
| "rule_config": lst[i].RuleConfig, | |||||
| "rule_prod": lst[i].RuleProd, | |||||
| "cate": lst[i].Cate, | |||||
| }) | |||||
| if err != nil { | if err != nil { | ||||
| logger.Errorf("update alert rule:%d datasource ids failed, %v", lst[i].Id, err) | logger.Errorf("update alert rule:%d datasource ids failed, %v", lst[i].Id, err) | ||||
| } | } | ||||
| @@ -4,6 +4,7 @@ import ( | |||||
| "encoding/json" | "encoding/json" | ||||
| "strconv" | "strconv" | ||||
| "strings" | "strings" | ||||
| "time" | |||||
| "github.com/ccfos/nightingale/v6/pkg/ctx" | "github.com/ccfos/nightingale/v6/pkg/ctx" | ||||
| "github.com/toolkits/pkg/logger" | "github.com/toolkits/pkg/logger" | ||||
| @@ -215,7 +216,7 @@ func (m *AlertHisEvent) UpdateFieldsMap(ctx *ctx.Context, fields map[string]inte | |||||
| func AlertHisEventUpgradeToV6(ctx *ctx.Context, dsm map[string]Datasource) error { | func AlertHisEventUpgradeToV6(ctx *ctx.Context, dsm map[string]Datasource) error { | ||||
| var lst []*AlertHisEvent | var lst []*AlertHisEvent | ||||
| err := DB(ctx).Find(&lst).Error | |||||
| err := DB(ctx).Where("trigger_time > ?", time.Now().Unix()-3600*24*30).Find(&lst).Error | |||||
| if err != nil { | if err != nil { | ||||
| return err | return err | ||||
| } | } | ||||
| @@ -238,7 +239,20 @@ func AlertHisEventUpgradeToV6(ctx *ctx.Context, dsm map[string]Datasource) error | |||||
| b, _ := json.Marshal(ruleConfig) | b, _ := json.Marshal(ruleConfig) | ||||
| lst[i].RuleConfig = string(b) | lst[i].RuleConfig = string(b) | ||||
| err = lst[i].UpdateFieldsMap(ctx, map[string]interface{}{"datasource_id": lst[i].DatasourceId, "rule_config": lst[i].RuleConfig}) | |||||
| if lst[i].RuleProd == "" { | |||||
| lst[i].RuleProd = METRIC | |||||
| } | |||||
| if lst[i].Cate == "" { | |||||
| lst[i].Cate = PROMETHEUS | |||||
| } | |||||
| err = lst[i].UpdateFieldsMap(ctx, map[string]interface{}{ | |||||
| "datasource_id": lst[i].DatasourceId, | |||||
| "rule_config": lst[i].RuleConfig, | |||||
| "rule_prod": lst[i].RuleProd, | |||||
| "cate": lst[i].Cate, | |||||
| }) | |||||
| if err != nil { | if err != nil { | ||||
| logger.Errorf("update alert rule:%d datasource ids failed, %v", lst[i].Id, err) | logger.Errorf("update alert rule:%d datasource ids failed, %v", lst[i].Id, err) | ||||
| } | } | ||||
| @@ -281,7 +281,19 @@ func AlertMuteUpgradeToV6(ctx *ctx.Context, dsm map[string]Datasource) error { | |||||
| } | } | ||||
| lst[i].DatasourceIds = string(b) | lst[i].DatasourceIds = string(b) | ||||
| err = lst[i].UpdateFieldsMap(ctx, map[string]interface{}{"datasource_ids": lst[i].DatasourceIds}) | |||||
| if lst[i].Prod == "" { | |||||
| lst[i].Prod = METRIC | |||||
| } | |||||
| if lst[i].Cate == "" { | |||||
| lst[i].Cate = PROMETHEUS | |||||
| } | |||||
| err = lst[i].UpdateFieldsMap(ctx, map[string]interface{}{ | |||||
| "datasource_ids": lst[i].DatasourceIds, | |||||
| "rule_prod": lst[i].Prod, | |||||
| "cate": lst[i].Cate, | |||||
| }) | |||||
| if err != nil { | if err != nil { | ||||
| logger.Errorf("update alert rule:%d datasource ids failed, %v", lst[i].Id, err) | logger.Errorf("update alert rule:%d datasource ids failed, %v", lst[i].Id, err) | ||||
| } | } | ||||
| @@ -716,7 +716,21 @@ func AlertRuleUpgradeToV6(ctx *ctx.Context, dsm map[string]Datasource) error { | |||||
| lst[i].Annotations = string(b) | lst[i].Annotations = string(b) | ||||
| } | } | ||||
| err = lst[i].UpdateFieldsMap(ctx, map[string]interface{}{"datasource_ids": lst[i].DatasourceIds, "annotations": lst[i].Annotations, "rule_config": lst[i].RuleConfig}) | |||||
| if lst[i].Prod == "" { | |||||
| lst[i].Prod = METRIC | |||||
| } | |||||
| if lst[i].Cate == "" { | |||||
| lst[i].Cate = PROMETHEUS | |||||
| } | |||||
| err = lst[i].UpdateFieldsMap(ctx, map[string]interface{}{ | |||||
| "datasource_ids": lst[i].DatasourceIds, | |||||
| "annotations": lst[i].Annotations, | |||||
| "rule_config": lst[i].RuleConfig, | |||||
| "prod": lst[i].Prod, | |||||
| "cate": PROMETHEUS, | |||||
| }) | |||||
| if err != nil { | if err != nil { | ||||
| logger.Errorf("update alert rule:%d datasource ids failed, %v", lst[i].Id, err) | logger.Errorf("update alert rule:%d datasource ids failed, %v", lst[i].Id, err) | ||||
| } | } | ||||
| @@ -345,7 +345,19 @@ func AlertSubscribeUpgradeToV6(ctx *ctx.Context, dsm map[string]Datasource) erro | |||||
| } | } | ||||
| lst[i].DatasourceIds = string(b) | lst[i].DatasourceIds = string(b) | ||||
| err = lst[i].UpdateFieldsMap(ctx, map[string]interface{}{"datasource_ids": lst[i].DatasourceIds}) | |||||
| if lst[i].Prod == "" { | |||||
| lst[i].Prod = METRIC | |||||
| } | |||||
| if lst[i].Cate == "" { | |||||
| lst[i].Cate = PROMETHEUS | |||||
| } | |||||
| err = lst[i].UpdateFieldsMap(ctx, map[string]interface{}{ | |||||
| "datasource_ids": lst[i].DatasourceIds, | |||||
| "prod": lst[i].Prod, | |||||
| "cate": PROMETHEUS, | |||||
| }) | |||||
| if err != nil { | if err != nil { | ||||
| logger.Errorf("update alert rule:%d datasource ids failed, %v", lst[i].Id, err) | logger.Errorf("update alert rule:%d datasource ids failed, %v", lst[i].Id, err) | ||||
| } | } | ||||