From 1180f1fcfd61ca91834bfea7d6ef95e1bf67f00a Mon Sep 17 00:00:00 2001 From: ning <710leo@gmail.com> Date: Thu, 9 Mar 2023 23:28:55 +0800 Subject: [PATCH] update upgrade cli --- models/alert_cur_event.go | 19 +++++++++++++++++-- models/alert_his_event.go | 18 ++++++++++++++++-- models/alert_mute.go | 14 +++++++++++++- models/alert_rule.go | 16 +++++++++++++++- models/alert_subscribe.go | 14 +++++++++++++- 5 files changed, 74 insertions(+), 7 deletions(-) diff --git a/models/alert_cur_event.go b/models/alert_cur_event.go index 61f7d72b..a837cbe3 100644 --- a/models/alert_cur_event.go +++ b/models/alert_cur_event.go @@ -7,6 +7,7 @@ import ( "strconv" "strings" "text/template" + "time" "github.com/ccfos/nightingale/v6/pkg/ctx" "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 { 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 { return err } @@ -515,7 +516,21 @@ func AlertCurEventUpgradeToV6(ctx *ctx.Context, dsm map[string]Datasource) error b, _ := json.Marshal(ruleConfig) 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 { logger.Errorf("update alert rule:%d datasource ids failed, %v", lst[i].Id, err) } diff --git a/models/alert_his_event.go b/models/alert_his_event.go index 063535e1..9464d976 100644 --- a/models/alert_his_event.go +++ b/models/alert_his_event.go @@ -4,6 +4,7 @@ import ( "encoding/json" "strconv" "strings" + "time" "github.com/ccfos/nightingale/v6/pkg/ctx" "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 { 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 { return err } @@ -238,7 +239,20 @@ func AlertHisEventUpgradeToV6(ctx *ctx.Context, dsm map[string]Datasource) error b, _ := json.Marshal(ruleConfig) 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 { logger.Errorf("update alert rule:%d datasource ids failed, %v", lst[i].Id, err) } diff --git a/models/alert_mute.go b/models/alert_mute.go index a4aa1691..06cd8747 100644 --- a/models/alert_mute.go +++ b/models/alert_mute.go @@ -281,7 +281,19 @@ func AlertMuteUpgradeToV6(ctx *ctx.Context, dsm map[string]Datasource) error { } 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 { logger.Errorf("update alert rule:%d datasource ids failed, %v", lst[i].Id, err) } diff --git a/models/alert_rule.go b/models/alert_rule.go index 17db3873..f583271f 100644 --- a/models/alert_rule.go +++ b/models/alert_rule.go @@ -716,7 +716,21 @@ func AlertRuleUpgradeToV6(ctx *ctx.Context, dsm map[string]Datasource) error { 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 { logger.Errorf("update alert rule:%d datasource ids failed, %v", lst[i].Id, err) } diff --git a/models/alert_subscribe.go b/models/alert_subscribe.go index b9fc5005..f98efe20 100644 --- a/models/alert_subscribe.go +++ b/models/alert_subscribe.go @@ -345,7 +345,19 @@ func AlertSubscribeUpgradeToV6(ctx *ctx.Context, dsm map[string]Datasource) erro } 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 { logger.Errorf("update alert rule:%d datasource ids failed, %v", lst[i].Id, err) }