Skip to content

Commit

Permalink
refactor: integration init
Browse files Browse the repository at this point in the history
  • Loading branch information
710leo committed Jun 3, 2024
1 parent 4424a6b commit e345332
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
51 changes: 48 additions & 3 deletions center/integration/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion models/builtin_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'"`
Expand Down
2 changes: 1 addition & 1 deletion models/builtin_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e345332

Please sign in to comment.