diff --git a/center/integration/init.go b/center/integration/init.go index 3ba4006e5..04bbc0a02 100644 --- a/center/integration/init.go +++ b/center/integration/init.go @@ -107,7 +107,23 @@ func Init(ctx *ctx.Context, builtinIntegrationsDir string) { } if exists { - continue + old, err := models.BuiltinPayloadGet(ctx, "type = ? AND component = ? AND name = ? AND cate = ?", builtinAlert.Type, builtinAlert.Component, builtinAlert.Name, builtinAlert.Cate) + if err != nil { + logger.Warning("get builtin alert fail ", builtinAlert, err) + continue + } + + if old.CreatedAt != old.UpdatedAt { + // 模板已经被修改过,不再更新 + continue + } + + // 先删除旧的 再添加新的 + err = models.BuiltinPayloadDels(ctx, []int64{old.ID}) + if err != nil { + logger.Warning("delete old builtin alert fail ", old, err) + continue + } } err = builtinAlert.Add(ctx, "system") @@ -159,7 +175,23 @@ func Init(ctx *ctx.Context, builtinIntegrationsDir string) { } if exists { - continue + old, err := models.BuiltinPayloadGet(ctx, "type = ? AND component = ? AND name = ? AND cate = ?", builtinDashboard.Type, builtinDashboard.Component, builtinDashboard.Name, builtinDashboard.Cate) + if err != nil { + logger.Warning("get builtin dashboard fail ", builtinDashboard, err) + continue + } + + if old.CreatedAt != old.UpdatedAt { + // 模板已经被修改过,不再更新 + continue + } + + // delete old + err = models.BuiltinPayloadDels(ctx, []int64{old.ID}) + if err != nil { + logger.Warning("delete old builtin dashboard fail ", old, err) + continue + } } err = builtinDashboard.Add(ctx, "system") @@ -196,9 +228,22 @@ func Init(ctx *ctx.Context, builtinIntegrationsDir string) { logger.Warning("check builtin metric exists fail", metric, err) continue } + if exists { - continue + old, err := models.BuiltinMetricGet(ctx, "lang = ? and collector = ? and typ = ? and name = ?", metric.Lang, metric.Collector, metric.Typ, metric.Name) + if err != nil { + logger.Warning("get builtin metric fail", metric, err) + continue + } + + // delete old + err = models.BuiltinMetricDels(ctx, []int64{old.ID}) + if err != nil { + logger.Warningf("delete old builtin metric fail %v %v", old, err) + continue + } } + err = metric.Add(ctx, "system") if err != nil { logger.Warning("add builtin metric fail", metric, err) diff --git a/models/builtin_metrics.go b/models/builtin_metrics.go index 1488ce9ca..95ac6a0b0 100644 --- a/models/builtin_metrics.go +++ b/models/builtin_metrics.go @@ -11,7 +11,7 @@ import ( // BuiltinMetric represents a metric along with its metadata. type BuiltinMetric struct { - ID uint64 `json:"id" gorm:"primaryKey;type:bigint;autoIncrement;comment:'unique identifier'"` + ID int64 `json:"id" gorm:"primaryKey;type:bigint;autoIncrement;comment:'unique identifier'"` Collector string `json:"collector" gorm:"type:varchar(191);not null;index:idx_collector,sort:asc;comment:'type of collector'"` // Type of collector (e.g., 'categraf', 'telegraf') Typ string `json:"typ" gorm:"type:varchar(191);not null;index:idx_typ,sort:asc;comment:'type of metric'"` // Type of metric (e.g., 'host', 'mysql', 'redis') Name string `json:"name" gorm:"type:varchar(191);not null;index:idx_name,sort:asc;comment:'name of metric'"` diff --git a/models/builtin_payload.go b/models/builtin_payload.go index fc70b1c48..2dc4a1d1d 100644 --- a/models/builtin_payload.go +++ b/models/builtin_payload.go @@ -9,7 +9,7 @@ import ( ) type BuiltinPayload struct { - ID uint64 `json:"id" gorm:"primaryKey;type:bigint;autoIncrement;comment:'unique identifier'"` + ID int64 `json:"id" gorm:"primaryKey;type:bigint;autoIncrement;comment:'unique identifier'"` Type string `json:"type" gorm:"type:varchar(191);not null;index:idx_type,sort:asc;comment:'type of payload'"` // Alert Dashboard Collet Component string `json:"component" gorm:"type:varchar(191);not null;index:idx_component,sort:asc;comment:'component of payload'"` // Host MySQL Redis Cate string `json:"cate" gorm:"type:varchar(191);not null;comment:'category of payload'"` // categraf_v1 telegraf_v1