-
Notifications
You must be signed in to change notification settings - Fork 1
/
checks.go
351 lines (289 loc) · 11 KB
/
checks.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package models
import (
"fmt"
"time"
"github.com/flanksource/duty/types"
"github.com/google/uuid"
"github.com/samber/lo"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
)
type CheckHealthStatus string
const (
CheckStatusHealthy = "healthy"
CheckStatusUnhealthy = "unhealthy"
)
var CheckHealthStatuses = []CheckHealthStatus{
CheckStatusHealthy,
CheckStatusUnhealthy,
}
type Check struct {
ID uuid.UUID `json:"id" gorm:"default:generate_ulid()"`
CanaryID uuid.UUID `json:"canary_id"`
AgentID uuid.UUID `json:"agent_id,omitempty"`
Spec types.JSON `json:"-"`
Type string `json:"type"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Labels types.JSONStringMap `json:"labels" gorm:"type:jsonstringmap"`
Description string `json:"description,omitempty"`
Status CheckHealthStatus `json:"status,omitempty"`
Owner string `json:"owner,omitempty"`
Severity Severity `json:"severity,omitempty"`
Icon string `json:"icon,omitempty"`
Transformed bool `json:"transformed,omitempty"`
LastRuntime *time.Time `json:"last_runtime,omitempty"`
NextRuntime *time.Time `json:"next_runtime,omitempty"`
LastTransitionTime *time.Time `json:"last_transition_time,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty" gorm:"<-:create"`
UpdatedAt *time.Time `json:"updated_at,omitempty" gorm:"autoUpdateTime:false"`
DeletedAt *time.Time `json:"deleted_at,omitempty"`
SilencedAt *time.Time `json:"silenced_at,omitempty"`
// Auxiliary fields
CanaryName string `json:"canary_name,omitempty" gorm:"-"`
ComponentIDs []string `json:"components,omitempty" gorm:"-"` // Linked component ids
Uptime types.Uptime `json:"uptime,omitempty" gorm:"-"`
Latency types.Latency `json:"latency,omitempty" gorm:"-"`
Statuses []CheckStatus `json:"checkStatuses,omitempty" gorm:"-"`
DisplayType string `json:"display_type,omitempty" gorm:"-"`
// These are calculated for the selected date range
EarliestRuntime *time.Time `json:"earliestRuntime,omitempty" gorm:"-"`
LatestRuntime *time.Time `json:"latestRuntime,omitempty" gorm:"-"`
TotalRuns int `json:"totalRuns,omitempty" gorm:"-"`
}
func (t Check) Value() any {
return &t
}
func (t Check) PKCols() []clause.Column {
return []clause.Column{{Name: "id"}}
}
func (t Check) UpdateParentsIsPushed(db *gorm.DB, items []DBTable) error {
parentIDs := lo.Map(items, func(item DBTable, _ int) string {
return item.(Check).CanaryID.String()
})
return db.Model(&Canary{}).Where("id IN ?", parentIDs).Update("is_pushed", false).Error
}
func (t Check) GetUnpushed(db *gorm.DB) ([]DBTable, error) {
var items []Check
err := db.Where("is_pushed IS FALSE").Find(&items).Error
return lo.Map(items, func(i Check, _ int) DBTable { return i }), err
}
func (c Check) PK() string {
return c.ID.String()
}
func (c Check) TableName() string {
return "checks"
}
func (c Check) ToString() string {
return fmt.Sprintf("%s-%s-%s", c.Name, c.Type, c.Description)
}
func (c Check) GetDescription() string {
return c.Description
}
func (c Check) AsMap(removeFields ...string) map[string]any {
return asMap(c, removeFields...)
}
func (c Check) GetID() string {
return c.ID.String()
}
func (c Check) GetName() string {
return c.Name
}
func (c Check) GetNamespace() string {
return c.Namespace
}
func (c Check) GetType() string {
return c.Type
}
func (c Check) GetStatus() string {
return string(c.Status)
}
func (c Check) GetLabelsMatcher() labels.Labels {
return checkLabelsProvider{c}
}
func (c Check) GetFieldsMatcher() fields.Fields {
return checkFieldsProvider{c}
}
type checkLabelsProvider struct {
Check
}
func (c checkLabelsProvider) Get(key string) string {
return c.Labels[key]
}
func (c checkLabelsProvider) Has(key string) bool {
_, ok := c.Labels[key]
return ok
}
type checkFieldsProvider struct {
Check
}
func (c checkFieldsProvider) Get(key string) string {
return ""
}
func (c checkFieldsProvider) Has(key string) bool {
return false // field selector not applicalbe for checks
}
type Checks []*Check
func (c Checks) Len() int {
return len(c)
}
func (c Checks) Less(i, j int) bool {
return c[i].ToString() < c[j].ToString()
}
func (c Checks) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
func (c Checks) Find(key string) *Check {
for _, check := range c {
if check.Name == key {
return check
}
}
return nil
}
type CheckStatus struct {
CheckID uuid.UUID `json:"check_id" gorm:"primaryKey"`
Status bool `json:"status"`
Invalid bool `json:"invalid,omitempty"`
Time string `json:"time" gorm:"primaryKey"`
Duration int `json:"duration"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
Detail any `json:"-" gorm:"-"`
CreatedAt time.Time `json:"created_at,omitempty" gorm:"<-:create"`
// IsPushed when set to true indicates that the check status has been pushed to upstream.
IsPushed bool `json:"is_pushed,omitempty"`
}
func (t CheckStatus) Value() any {
return &t
}
func (t CheckStatus) UpdateParentsIsPushed(db *gorm.DB, items []DBTable) error {
parentIDs := lo.Map(items, func(item DBTable, _ int) string {
return item.(CheckStatus).CheckID.String()
})
return db.Model(&Check{}).Where("id IN ?", parentIDs).Update("is_pushed", false).Error
}
func (s CheckStatus) PKCols() []clause.Column {
return []clause.Column{{Name: "check_id"}, {Name: "time"}}
}
func (s CheckStatus) UpdateIsPushed(db *gorm.DB, items []DBTable) error {
ids := lo.Map(items, func(a DBTable, _ int) []any {
c := any(a).(CheckStatus)
return []any{c.CheckID, c.Time}
})
return db.Model(&CheckStatus{}).Where("(check_id, time) IN ?", ids).Update("is_pushed", true).Error
}
func (s CheckStatus) GetUnpushed(db *gorm.DB) ([]DBTable, error) {
var items []CheckStatus
err := db.Select("check_statuses.*").
Joins("LEFT JOIN checks ON checks.id = check_statuses.check_id").
Where("checks.agent_id = ?", uuid.Nil).
Where("check_statuses.is_pushed IS FALSE").
Find(&items).Error
return lo.Map(items, func(i CheckStatus, _ int) DBTable { return i }), err
}
func (s CheckStatus) PK() string {
return s.CheckID.String() + s.Time
}
func (s CheckStatus) GetTime() (time.Time, error) {
return time.Parse(time.DateTime, s.Time)
}
func (CheckStatus) TableName() string {
return "check_statuses"
}
func (s CheckStatus) AsMap(removeFields ...string) map[string]any {
return asMap(s, removeFields...)
}
// CheckStatusAggregate1h represents the `check_statuses_1h` table
type CheckStatusAggregate1h struct {
CheckID string `gorm:"column:check_id"`
CreatedAt time.Time `gorm:"column:created_at"`
Duration int `gorm:"column:duration"`
Total int `gorm:"column:total"`
Passed int `gorm:"column:passed"`
Failed int `gorm:"column:failed"`
}
func (CheckStatusAggregate1h) TableName() string {
return "check_statuses_1h"
}
// CheckStatusAggregate1d represents the `check_statuses_1d` table
type CheckStatusAggregate1d struct {
CheckID string `gorm:"column:check_id"`
CreatedAt time.Time `gorm:"column:created_at"`
Duration int `gorm:"column:duration"`
Total int `gorm:"column:total"`
Passed int `gorm:"column:passed"`
Failed int `gorm:"column:failed"`
}
func (CheckStatusAggregate1d) TableName() string {
return "check_statuses_1d"
}
// CheckSummary represents the `check_summary` view
type CheckSummary struct {
ID uuid.UUID `json:"id"`
CanaryID uuid.UUID `json:"canary_id"`
CanaryName string `json:"canary_name"`
CanaryNamespace string `json:"canary_namespace"`
Description string `json:"description,omitempty"`
Icon string `json:"icon,omitempty"`
Labels types.JSONStringMap `json:"labels"`
LastTransitionTime *time.Time `json:"last_transition_time,omitempty"`
Latency types.Latency `json:"latency,omitempty"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Owner string `json:"owner,omitempty"`
Severity string `json:"severity,omitempty"`
Status string `json:"status"`
Type string `json:"type"`
Uptime types.Uptime `json:"uptime,omitempty"`
LastRuntime *time.Time `json:"last_runtime,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at,omitempty"`
SilencedAt *time.Time `json:"silenced_at,omitempty"`
}
func (t *CheckSummary) TableName() string {
return "check_summary"
}
func (t CheckSummary) AsMap(removeFields ...string) map[string]any {
return asMap(t, removeFields...)
}
type CheckConfigRelationship struct {
ConfigID uuid.UUID `json:"config_id,omitempty"`
CheckID uuid.UUID `json:"check_id,omitempty"`
CanaryID uuid.UUID `json:"canary_id,omitempty"`
SelectorID string `json:"selector_id,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
DeletedAt *time.Time `json:"deleted_at,omitempty"`
}
func (s CheckConfigRelationship) UpdateIsPushed(db *gorm.DB, items []DBTable) error {
ids := lo.Map(items, func(a DBTable, _ int) []string {
c := any(a).(CheckConfigRelationship)
return []string{c.ConfigID.String(), c.CheckID.String(), c.CanaryID.String(), c.SelectorID}
})
return db.Model(&CheckConfigRelationship{}).Where("(config_id, check_id, canary_id, selector_id) IN ?", ids).Update("is_pushed", true).Error
}
func (c CheckConfigRelationship) GetUnpushed(db *gorm.DB) ([]DBTable, error) {
var items []CheckConfigRelationship
err := db.Select("check_config_relationships.*").
Joins("LEFT JOIN config_items ci ON check_config_relationships.config_id = ci.id").
Where("ci.agent_id = ?", uuid.Nil).
Where("check_config_relationships.is_pushed IS FALSE").
Find(&items).Error
return lo.Map(items, func(i CheckConfigRelationship, _ int) DBTable { return i }), err
}
func (c *CheckConfigRelationship) Save(db *gorm.DB) error {
return db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "canary_id"}, {Name: "check_id"}, {Name: "config_id"}, {Name: "selector_id"}},
UpdateAll: true,
}).Create(c).Error
}
func (c CheckConfigRelationship) PK() string {
return c.ConfigID.String() + "," + c.CheckID.String() + "," + c.CanaryID.String() + "," + c.SelectorID
}
func (CheckConfigRelationship) TableName() string {
return "check_config_relationships"
}