/
incident.go
242 lines (222 loc) Β· 7.56 KB
/
incident.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
package db
import (
"database/sql"
"errors"
"github.com/coroot/coroot/model"
"github.com/coroot/coroot/timeseries"
"github.com/coroot/coroot/utils"
"k8s.io/klog"
)
type Incident model.ApplicationIncident
func (i *Incident) Migrate(m *Migrator) error {
return m.Exec(`
CREATE TABLE IF NOT EXISTS incident (
project_id TEXT NOT NULL REFERENCES project(id),
application_id TEXT NOT NULL,
key TEXT NOT NULL,
opened_at INT NOT NULL,
resolved_at INT NOT NULL DEFAULT 0,
severity INT NOT NULL,
PRIMARY KEY (project_id, application_id, opened_at)
);
CREATE UNIQUE INDEX IF NOT EXISTS incident_key ON incident (project_id, key);
`)
}
type IncidentNotification struct {
ProjectId ProjectId
ApplicationId model.ApplicationId
IncidentKey string
Status model.Status
Destination IntegrationType
Timestamp timeseries.Time
SentAt timeseries.Time
ExternalKey string
Details *IncidentNotificationDetails
}
func (n *IncidentNotification) Migrate(m *Migrator) error {
return m.Exec(`
CREATE TABLE IF NOT EXISTS incident_notification (
project_id TEXT NOT NULL REFERENCES project(id),
application_id TEXT NOT NULL,
incident_key TEXT NOT NULL,
status INT NOT NULL,
destination TEXT NOT NULL,
timestamp INT NOT NULL,
sent_at INT NOT NULL DEFAULT 0,
external_key TEXT NOT NULL DEFAULT '',
details TEXT
);
`)
}
type IncidentNotificationDetails struct {
Reports []IncidentNotificationDetailsReport `json:"reports"`
}
type IncidentNotificationDetailsReport struct {
Name model.AuditReportName `json:"name"`
Check string `json:"check"`
Message string `json:"message"`
}
func (db *DB) GetIncidentByKey(projectId ProjectId, key string) (*model.ApplicationIncident, error) {
i := &model.ApplicationIncident{Key: key}
err := db.db.QueryRow(
"SELECT opened_at, resolved_at, severity FROM incident WHERE project_id = $1 AND key = $2 LIMIT 1",
projectId, key).Scan(&i.OpenedAt, &i.ResolvedAt, &i.Severity)
return i, err
}
func (db *DB) GetApplicationIncidents(projectId ProjectId, from, to timeseries.Time) (map[model.ApplicationId][]*model.ApplicationIncident, error) {
rows, err := db.db.Query(
"SELECT application_id, key, opened_at, resolved_at, severity FROM incident WHERE project_id = $1 AND opened_at <= $2 AND (resolved_at = 0 OR resolved_at >= $3)",
projectId, to, from)
if err != nil {
return nil, err
}
defer func() {
_ = rows.Close()
}()
res := map[model.ApplicationId][]*model.ApplicationIncident{}
var appId model.ApplicationId
for rows.Next() {
var i model.ApplicationIncident
if err := rows.Scan(&appId, &i.Key, &i.OpenedAt, &i.ResolvedAt, &i.Severity); err != nil {
return nil, err
}
res[appId] = append(res[appId], &i)
}
return res, err
}
func (db *DB) CreateOrUpdateIncident(projectId ProjectId, appId model.ApplicationId, now timeseries.Time, severity model.Status) (*model.ApplicationIncident, error) {
appIdStr := appId.String()
var last model.ApplicationIncident
err := db.db.QueryRow(
"SELECT key, opened_at, resolved_at, severity FROM incident WHERE project_id = $1 AND application_id = $2 ORDER BY opened_at DESC LIMIT 1",
projectId, appIdStr).Scan(&last.Key, &last.OpenedAt, &last.ResolvedAt, &last.Severity)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return nil, err
}
if last.OpenedAt.IsZero() || last.Resolved() {
if severity > model.OK { // open
i := model.ApplicationIncident{Key: utils.NanoId(8), OpenedAt: now, Severity: severity}
_, err := db.db.Exec(
"INSERT INTO incident (project_id, application_id, key, opened_at, severity) VALUES ($1, $2, $3, $4, $5)",
projectId, appIdStr, i.Key, i.OpenedAt, i.Severity)
return &i, err
}
return nil, nil
}
if severity == model.OK { // close
last.ResolvedAt = now
_, err := db.db.Exec(
"UPDATE incident SET resolved_at = $1 WHERE project_id = $2 AND application_id = $3 AND opened_at = $4",
last.ResolvedAt, projectId, appIdStr, last.OpenedAt)
return &last, err
}
if severity != last.Severity { // update severity
last.Severity = severity
_, err := db.db.Exec(
"UPDATE incident SET severity = $1 WHERE project_id = $2 AND application_id = $3 AND opened_at = $4",
last.Severity, projectId, appIdStr, last.OpenedAt)
return &last, err
}
return nil, nil
}
func (db *DB) PutIncidentNotification(n IncidentNotification) {
details, err := marshal(n.Details)
if err != nil {
klog.Errorln(err)
return
}
_, err = db.db.Exec(
"INSERT INTO incident_notification (project_id, application_id, incident_key, status, destination, timestamp, external_key, details) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
n.ProjectId, n.ApplicationId, n.IncidentKey, n.Status, n.Destination, n.Timestamp, n.ExternalKey, details,
)
if err != nil {
klog.Errorln(err)
}
}
func (db *DB) UpdateIncidentNotification(n IncidentNotification) error {
_, err := db.db.Exec(
"UPDATE incident_notification SET sent_at = $1, external_key = $2 WHERE project_id = $3 AND application_id = $4 AND incident_key = $5 AND timestamp = $6 AND destination = $7",
n.SentAt, n.ExternalKey, n.ProjectId, n.ApplicationId, n.IncidentKey, n.Timestamp, n.Destination,
)
return err
}
func (db *DB) GetNotSentIncidentNotifications(from timeseries.Time) ([]IncidentNotification, error) {
rows, err := db.db.Query(`
SELECT project_id, application_id, incident_key, status, destination, timestamp, external_key, details
FROM incident_notification
WHERE timestamp >= $1 AND sent_at = 0
ORDER BY project_id, application_id, incident_key, timestamp
`, from)
if err != nil {
return nil, err
}
defer func() {
_ = rows.Close()
}()
var res []IncidentNotification
var details sql.NullString
for rows.Next() {
var n IncidentNotification
if err := rows.Scan(&n.ProjectId, &n.ApplicationId, &n.IncidentKey, &n.Status, &n.Destination, &n.Timestamp, &n.ExternalKey, &details); err != nil {
return nil, err
}
if details.String != "" {
if err := unmarshal(details.String, &n.Details); err != nil {
klog.Warningln(err)
}
}
res = append(res, n)
}
return res, nil
}
func (db *DB) GetPreviousIncidentNotifications(n IncidentNotification) ([]IncidentNotification, error) {
rows, err := db.db.Query(`
SELECT project_id, application_id, incident_key, status, destination, timestamp, external_key, details
FROM incident_notification
WHERE project_id = $1 AND application_id = $2 AND incident_key = $3 AND destination = $4 AND timestamp < $5
ORDER BY timestamp
`, n.ProjectId, n.ApplicationId, n.IncidentKey, n.Destination, n.Timestamp,
)
if err != nil {
return nil, err
}
defer func() {
_ = rows.Close()
}()
var res []IncidentNotification
var details sql.NullString
for rows.Next() {
var n IncidentNotification
if err := rows.Scan(&n.ProjectId, &n.ApplicationId, &n.IncidentKey, &n.Status, &n.Destination, &n.Timestamp, &n.ExternalKey, &details); err != nil {
return nil, err
}
if details.String != "" {
if err := unmarshal(details.String, &n.Details); err != nil {
klog.Warningln(err)
}
}
res = append(res, n)
}
return res, nil
}
func (db *DB) GetSentIncidentNotificationsStat(from timeseries.Time) map[IntegrationType]int {
rows, err := db.db.Query("SELECT destination, count(*) FROM incident_notification WHERE timestamp >= $1 AND sent_at > 0 GROUP BY destination", from)
if err != nil {
klog.Errorln(err)
return nil
}
defer func() {
_ = rows.Close()
}()
res := map[IntegrationType]int{}
var destination IntegrationType
var count int
for rows.Next() {
if err := rows.Scan(&destination, &count); err != nil {
klog.Errorln(err)
return nil
}
res[destination] = count
}
return res
}