forked from ccfos/nightingale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalerting_engine.go
153 lines (129 loc) · 4.19 KB
/
alerting_engine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package models
import (
"fmt"
"time"
"github.com/ccfos/nightingale/v6/pkg/ctx"
)
type AlertingEngines struct {
Id int64 `json:"id" gorm:"primaryKey"`
Instance string `json:"instance"`
Cluster string `json:"cluster"`
DatasourceId int64 `json:"datasource_id"`
Clock int64 `json:"clock"`
}
func (e *AlertingEngines) TableName() string {
return "alerting_engines"
}
// UpdateCluster 页面上用户会给各个n9e-server分配要关联的目标集群是什么
func (e *AlertingEngines) UpdateDatasourceId(ctx *ctx.Context, id int64) error {
count, err := Count(DB(ctx).Model(&AlertingEngines{}).Where("id<>? and instance=? and datasource_id=?", e.Id, e.Instance, id))
if err != nil {
return err
}
if count > 0 {
return fmt.Errorf("instance %s and datasource_id %d already exists", e.Instance, id)
}
e.DatasourceId = id
return DB(ctx).Model(e).Select("datasource_id").Updates(e).Error
}
func AlertingEngineAdd(ctx *ctx.Context, instance string, datasourceId int64) error {
count, err := Count(DB(ctx).Model(&AlertingEngines{}).Where("instance=? and datasource_id=?", instance, datasourceId))
if err != nil {
return err
}
if count > 0 {
return fmt.Errorf("instance %s and datasource_id %d already exists", instance, datasourceId)
}
err = DB(ctx).Create(&AlertingEngines{
Instance: instance,
DatasourceId: datasourceId,
Clock: time.Now().Unix(),
}).Error
return err
}
func AlertingEngineDel(ctx *ctx.Context, ids []int64) error {
if len(ids) == 0 {
return nil
}
return DB(ctx).Where("id in ?", ids).Delete(new(AlertingEngines)).Error
}
func AlertingEngineGetDatasourceIds(ctx *ctx.Context, instance string) ([]int64, error) {
var objs []AlertingEngines
err := DB(ctx).Where("instance=?", instance).Find(&objs).Error
if err != nil {
return []int64{}, err
}
if len(objs) == 0 {
return []int64{}, nil
}
var ids []int64
for i := 0; i < len(objs); i++ {
ids = append(ids, objs[i].DatasourceId)
}
return ids, nil
}
// AlertingEngineGets 拉取列表数据,用户要在页面上看到所有 n9e-server 实例列表,然后为其分配 cluster
func AlertingEngineGets(ctx *ctx.Context, where string, args ...interface{}) ([]*AlertingEngines, error) {
var objs []*AlertingEngines
var err error
session := DB(ctx).Order("instance")
if where == "" {
err = session.Find(&objs).Error
} else {
err = session.Where(where, args...).Find(&objs).Error
}
return objs, err
}
func AlertingEngineGet(ctx *ctx.Context, where string, args ...interface{}) (*AlertingEngines, error) {
lst, err := AlertingEngineGets(ctx, where, args...)
if err != nil {
return nil, err
}
if len(lst) == 0 {
return nil, nil
}
return lst[0], nil
}
func AlertingEngineGetsClusters(ctx *ctx.Context, where string, args ...interface{}) ([]string, error) {
var arr []string
var err error
session := DB(ctx).Model(new(AlertingEngines)).Where("cluster != ''").Order("cluster").Distinct("cluster")
if where == "" {
err = session.Pluck("cluster", &arr).Error
} else {
err = session.Where(where, args...).Pluck("cluster", &arr).Error
}
return arr, err
}
func AlertingEngineGetsInstances(ctx *ctx.Context, where string, args ...interface{}) ([]string, error) {
var arr []string
var err error
session := DB(ctx).Model(new(AlertingEngines)).Order("instance")
if where == "" {
err = session.Pluck("instance", &arr).Error
} else {
err = session.Where(where, args...).Pluck("instance", &arr).Error
}
return arr, err
}
func AlertingEngineHeartbeatWithCluster(ctx *ctx.Context, instance, cluster string, datasourceId int64) error {
var total int64
err := DB(ctx).Model(new(AlertingEngines)).Where("instance=? and cluster = ? and datasource_id=?", instance, cluster, datasourceId).Count(&total).Error
if err != nil {
return err
}
if total == 0 {
// insert
err = DB(ctx).Create(&AlertingEngines{
Instance: instance,
DatasourceId: datasourceId,
Cluster: cluster,
Clock: time.Now().Unix(),
}).Error
} else {
// updates
fields := map[string]interface{}{"clock": time.Now().Unix()}
err = DB(ctx).Model(new(AlertingEngines)).Where("instance=? and cluster = ? and datasource_id=?", instance, cluster, datasourceId).Updates(fields).Error
}
return err
}