File size: 8,411 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package alert

import (
	"context"
	"database/sql"
	"fmt"
	"strconv"
	"text/template"
	"time"

	"github.com/target/goalert/permission"
	"github.com/target/goalert/search"
	"github.com/target/goalert/util/sqlutil"
	"github.com/target/goalert/validation/validate"

	"github.com/pkg/errors"
)

// SortMode indicates the mode of sorting for alerts.
type SortMode int

const (
	// SortModeStatusID will sort by status priority (unacked, then acked, then closed) followed by ID (newest/highest first)
	SortModeStatusID SortMode = iota

	// SortModeDateID will sort alerts by date newest first, falling back to ID (newest/highest first)
	SortModeDateID

	// SortModeDateIDReverse will sort alerts by date oldest first, falling back to ID (oldest/lowest first)
	SortModeDateIDReverse
)

// SearchOptions contains criteria for filtering and sorting alerts.
type SearchOptions struct {
	// Search is matched case-insensitive against the alert summary, id and service name.
	Search string `json:"s,omitempty"`

	// Status, if specified, will restrict alerts to those with a matching status.
	Status []Status `json:"t,omitempty"`

	// ServiceFilter, if specified, will restrict alerts to those with a matching ServiceID on IDs, if valid.
	ServiceFilter IDFilter `json:"v,omitempty"`

	After SearchCursor `json:"a,omitempty"`

	// Omit specifies a list of alert IDs to exclude from the results.
	Omit []int `json:"o,omitempty"`

	// NotifiedUserID will include all alerts the specified user has been
	// notified for to the results.
	NotifiedUserID string `json:"e,omitempty"`

	// Limit restricts the maximum number of rows returned. Default is 50.
	// Note: Limit is applied AFTER AfterID is taken into account.
	Limit int `json:"-"`

	// Sort allows customizing the sort method.
	Sort SortMode `json:"z,omitempty"`

	// NotBefore will omit any alerts created any time before the provided time.
	NotBefore time.Time `json:"n,omitempty"`

	// Before will only include alerts that were created before the provided time.
	Before time.Time `json:"b,omitempty"`

	// serviceNameIDs is used internally to store IDs for services matching the query name.
	serviceNameIDs []string

	// ClosedBefore will only include alerts that were closed before the provided time.
	ClosedBefore time.Time `json:"c,omitempty"`

	// NotClosedBefore will omit any alerts closed any time before the provided time.
	NotClosedBefore time.Time `json:"nc,omitempty"`
}

type IDFilter struct {
	Valid bool     `json:"v,omitempty"`
	IDs   []string `json:"i,omitempty"`
}

type SearchCursor struct {
	ID      int       `json:"i,omitempty"`
	Status  Status    `json:"s,omitempty"`
	Created time.Time `json:"c,omitempty"`
}

var serviceSearchTemplate = template.Must(template.New("alert-search-services").Funcs(search.Helpers()).Parse(`
	SELECT id
	FROM services
	WHERE {{textSearch "search" "name"}}
`))

var searchTemplate = template.Must(template.New("alert-search").Funcs(search.Helpers()).Parse(`
	SELECT
		a.id,
		a.summary,
		a.details,
		a.service_id,
		a.source,
		a.status,
		created_at,
		a.dedup_key
	FROM alerts a
	WHERE true
	{{ if .Omit }}
		AND not a.id = any(:omit)
	{{ end }}
	{{ if .Search }}
		AND (
			a.id = :searchID OR {{textSearch "search" "a.summary"}} OR a.service_id = any(:svcNameMatchIDs)
		)
	{{ end }}
	{{ if .Status }}
		AND a.status = any(:status::enum_alert_status[])
	{{ end }}
	{{ if .ServiceFilter.Valid }}
		AND (a.service_id = any(:services)
			{{ if .NotifiedUserID }}
				OR a.id = any(select alert_id from alert_logs where event in ('notification_sent', 'no_notification_sent') and sub_user_id = :notifiedUserID)
			{{ end }}
		)
	{{ end }}
	{{ if not .Before.IsZero }}
		AND a.created_at < :beforeTime
	{{ end }}
	{{ if not .NotBefore.IsZero }}
		AND a.created_at >= :notBeforeTime
	{{ end }}
	{{ if .After.ID }}
		AND (
			{{ if eq .Sort 1 }}
				a.created_at < :afterCreated OR
				(a.created = :afterCreated AND a.id < :afterID)
			{{ else if eq .Sort 2}}
				a.created_at > :afterCreated OR
				(a.created_at = :afterCreated AND a.id > :afterID)
			{{ else }}
				a.status > :afterStatus::enum_alert_status OR
				(a.status = :afterStatus::enum_alert_status AND a.id < :afterID)
			{{ end }}
		)
	{{ end }}
	{{ if not .ClosedBefore.IsZero }}
		AND EXISTS (select 1 from alert_metrics where alert_id = a.id AND closed_at < :closedBeforeTime) 
	{{ end }}
	{{ if not .NotClosedBefore.IsZero }}
		AND EXISTS (select 1 from alert_metrics where alert_id = a.id AND closed_at > :notClosedBeforeTime) 
	{{ end }}
	ORDER BY {{.SortStr}}
	LIMIT {{.Limit}}
`))

type renderData SearchOptions

func (opts renderData) SortStr() string {
	switch opts.Sort {
	case SortModeDateID:
		return "created_at DESC, id DESC"
	case SortModeDateIDReverse:
		return "created_at, id"
	}

	// SortModeStatusID
	return "status, id DESC"
}

func (opts renderData) Normalize() (*renderData, error) {
	if opts.Limit == 0 {
		opts.Limit = search.DefaultMaxResults
	}

	err := validate.Many(
		validate.Search("Search", opts.Search),
		validate.Range("Limit", opts.Limit, 0, 1001),
		validate.Range("Status", len(opts.Status), 0, 3),
		validate.ManyUUID("Services", opts.ServiceFilter.IDs, 50),
		validate.Range("Omit", len(opts.Omit), 0, 50),
		validate.OneOf("Sort", opts.Sort, SortModeStatusID, SortModeDateID, SortModeDateIDReverse),
	)
	if opts.After.Status != "" {
		err = validate.Many(err, validate.OneOf("After.Status", opts.After.Status, StatusTriggered, StatusActive, StatusClosed))
	}
	if err != nil {
		return nil, err
	}

	for i, stat := range opts.Status {
		err = validate.OneOf("Status["+strconv.Itoa(i)+"]", stat, StatusTriggered, StatusActive, StatusClosed)
		if err != nil {
			return nil, err
		}
	}

	return &opts, err
}

func (opts renderData) QueryArgs() []sql.NamedArg {
	var searchID sql.NullInt64
	if i, err := strconv.ParseInt(opts.Search, 10, 64); err == nil {
		searchID.Valid = true
		searchID.Int64 = i
	}

	stat := make(sqlutil.StringArray, len(opts.Status))
	for i := range opts.Status {
		stat[i] = string(opts.Status[i])
	}

	return []sql.NamedArg{
		sql.Named("search", opts.Search),
		sql.Named("searchID", searchID),
		sql.Named("status", stat),
		sql.Named("services", sqlutil.UUIDArray(opts.ServiceFilter.IDs)),
		sql.Named("svcNameMatchIDs", sqlutil.UUIDArray(opts.serviceNameIDs)),
		sql.Named("afterID", opts.After.ID),
		sql.Named("afterStatus", opts.After.Status),
		sql.Named("afterCreated", opts.After.Created),
		sql.Named("omit", sqlutil.IntArray(opts.Omit)),
		sql.Named("notifiedUserID", opts.NotifiedUserID),
		sql.Named("beforeTime", opts.Before),
		sql.Named("notBeforeTime", opts.NotBefore),
		sql.Named("closedBeforeTime", opts.ClosedBefore),
		sql.Named("notClosedBeforeTime", opts.NotClosedBefore),
	}
}

func (s *Store) serviceNameSearch(ctx context.Context, data *renderData) error {
	if data.Search == "" {
		data.serviceNameIDs = nil
		return nil
	}

	query, args, err := search.RenderQuery(ctx, serviceSearchTemplate, data)
	if err != nil {
		return fmt.Errorf("render service-search query: %w", err)
	}

	rows, err := s.db.QueryContext(ctx, query, args...)
	if errors.Is(err, sql.ErrNoRows) {
		data.serviceNameIDs = nil
		return nil
	}
	if err != nil {
		return fmt.Errorf("search for services with '%s': %w", data.Search, err)
	}
	defer rows.Close()

	var ids []string
	for rows.Next() {
		var id string
		if err := rows.Scan(&id); err != nil {
			return fmt.Errorf("scan service-search query: %w", err)
		}
		ids = append(ids, id)
	}

	data.serviceNameIDs = ids

	return nil
}

func (s *Store) Search(ctx context.Context, opts *SearchOptions) ([]Alert, error) {
	err := permission.LimitCheckAny(ctx, permission.System, permission.User)
	if err != nil {
		return nil, err
	}
	if opts == nil {
		opts = new(SearchOptions)
	}

	data, err := (*renderData)(opts).Normalize()
	if err != nil {
		return nil, err
	}

	err = s.serviceNameSearch(ctx, data)
	if err != nil {
		return nil, err
	}

	query, args, err := search.RenderQuery(ctx, searchTemplate, data)
	if err != nil {
		return nil, errors.Wrap(err, "render query")
	}

	rows, err := s.db.QueryContext(ctx, query, args...)
	if err != nil {
		return nil, errors.Wrap(err, "query")
	}
	defer rows.Close()

	alerts := make([]Alert, 0, opts.Limit)

	for rows.Next() {
		var a Alert
		err = errors.Wrap(a.scanFrom(rows.Scan), "scan")
		if err != nil {
			return nil, err
		}
		alerts = append(alerts, a)
	}

	return alerts, nil
}