File size: 6,879 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
package service

import (
	"context"
	"database/sql"
	"strings"
	"text/template"

	"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"
)

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

	// FavoritesUserID specifies the UserID whose favorite services want to be displayed.
	FavoritesUserID string `json:"u,omitempty"`

	// FavoritesOnly controls filtering the results to those marked as favorites by FavoritesUserID.
	FavoritesOnly bool `json:"o,omitempty"`

	// Omit specifies a list of service IDs to exclude from the results.
	Omit []string `json:"m,omitempty"`

	// Only lookup the service IDs present in the request.
	Only []string `json:"n,omitempty"`

	// FavoritesFirst indicates that services marked as favorite (by FavoritesUserID) should be returned first (before any non-favorites).
	FavoritesFirst bool `json:"f,omitempty"`

	// Limit will limit the number of results.
	Limit int `json:"-"`

	After SearchCursor `json:"a,omitempty"`
}

type SearchCursor struct {
	Name       string `json:"n"`
	IsFavorite bool   `json:"f"`
}

var searchTemplate = template.Must(template.New("search").Funcs(search.Helpers()).Parse(`
	SELECT{{if .LabelKey}} DISTINCT ON ({{ .OrderBy }}){{end}}
		svc.id,
		svc.name,
		svc.description,
		svc.escalation_policy_id,
		fav IS DISTINCT FROM NULL,
		svc.maintenance_expires_at
	FROM services svc
	{{if not .FavoritesOnly }}LEFT {{end}}JOIN user_favorites fav ON svc.id = fav.tgt_service_id AND {{if .FavoritesUserID}}fav.user_id = :favUserID{{else}}false{{end}}
	{{if and .IntegrationKey}}
		JOIN integration_keys intKey ON
			intKey.service_id = svc.id AND
			intKey.id = :integrationKey
	{{end}}
	{{if and .LabelKey (not .LabelNegate)}}
		JOIN labels l ON
			l.tgt_service_id = svc.id AND
			l.key = :labelKey
			{{if ne .LabelValue "*"}} AND value = :labelValue{{end}}
	{{end}}
	WHERE true
	{{if .Omit}}
		AND not svc.id = any(:omit)
	{{end}}
	{{if .Only}}
		AND svc.id = any(:only)
	{{end}}
	{{- if and .LabelKey .LabelNegate}}
		AND svc.id NOT IN (
			SELECT tgt_service_id
			FROM labels
			WHERE
				tgt_service_id NOTNULL AND
				key = :labelKey
				{{if ne .LabelValue "*"}} AND value = :labelValue{{end}}
		)
	{{end}}
	{{- if and .Search (not .LabelKey) (not .IntegrationKey)}}
		AND ({{orderedPrefixSearch "search" "svc.name"}} OR {{contains "search" "svc.description"}} OR {{contains "search" "svc.name"}})
	{{- end}}
	{{- if .After.Name}}
		AND
		{{if not .FavoritesFirst}}
			lower(svc.name) > lower(:afterName)
		{{else if .After.IsFavorite}}
			((fav IS DISTINCT FROM NULL AND lower(svc.name) > lower(:afterName)) OR fav isnull)
		{{else}}
			(fav isnull AND lower(svc.name) > lower(:afterName))
		{{end}}
	{{- end}}
	ORDER BY {{ .OrderBy }}
	LIMIT {{.Limit}}
`))

type renderData SearchOptions

func (opts renderData) OrderBy() string {
	if opts.FavoritesFirst {
		return "fav isnull, lower(svc.name)"
	}

	return "lower(svc.name)"
}

func (opts renderData) IntegrationKey() string {
	if !strings.Contains(opts.Search, "token=") {
		return ""
	}
	return opts.Search[6:42]
}

func (opts renderData) LabelKey() string {
	searchStr := opts.Search
	if strings.Contains(opts.Search, "token=") {
		// strip token string
		searchStr = opts.Search[42:]
		searchStr = strings.TrimSpace(searchStr)
	}
	idx := strings.IndexByte(searchStr, '=')
	if idx == -1 {
		return ""
	}
	return strings.TrimSuffix(searchStr[:idx], "!") // if `!=`` is used
}
func (opts renderData) LabelValue() string {
	searchStr := opts.Search
	if strings.Contains(opts.Search, "token=") {
		// strip token string
		searchStr = opts.Search[42:]
		searchStr = strings.TrimSpace(searchStr)
	}
	idx := strings.IndexByte(searchStr, '=')
	if idx == -1 {
		return ""
	}
	val := searchStr[idx+1:]
	if val == "" {
		return "*"
	}
	return val
}
func (opts renderData) LabelNegate() bool {
	idx := strings.IndexByte(opts.Search, '=')
	if idx < 1 {
		return false
	}

	return opts.Search[idx-1] == '!'
}

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, search.MaxResults),
		validate.ManyUUID("Omit", opts.Omit, 50),
		validate.ManyUUID("Only", opts.Only, 50),
	)
	if opts.After.Name != "" {
		err = validate.Many(err, validate.IDName("After.Name", opts.After.Name))
	}
	if opts.FavoritesOnly || opts.FavoritesFirst || opts.FavoritesUserID != "" {
		err = validate.Many(err, validate.UUID("FavoritesUserID", opts.FavoritesUserID))
	}
	if err != nil {
		return nil, err
	}
	if opts.IntegrationKey() != "" {
		err = validate.Search("IntegrationKey", opts.IntegrationKey())
	}
	if opts.LabelKey() != "" {
		err = validate.Search("LabelKey", opts.LabelKey())
		if opts.LabelValue() != "*" {
			err = validate.Many(err,
				validate.LabelValue("LabelValue", opts.LabelValue()),
			)
		}
	}
	if err != nil {
		return nil, err
	}

	return &opts, nil
}

func (opts renderData) QueryArgs() []sql.NamedArg {
	return []sql.NamedArg{
		sql.Named("favUserID", opts.FavoritesUserID),
		sql.Named("integrationKey", opts.IntegrationKey()),
		sql.Named("labelKey", opts.LabelKey()),
		sql.Named("labelValue", opts.LabelValue()),
		sql.Named("labelNegate", opts.LabelNegate()),
		sql.Named("search", opts.Search),
		sql.Named("afterName", opts.After.Name),
		sql.Named("omit", sqlutil.UUIDArray(opts.Omit)),
		sql.Named("only", sqlutil.UUIDArray(opts.Only)),
	}
}

// Search will return a list of matching services and the total number of matches available.
func (s *Store) Search(ctx context.Context, opts *SearchOptions) ([]Service, error) {
	if opts == nil {
		opts = &SearchOptions{}
	}

	userCheck := permission.User
	if opts.FavoritesUserID != "" {
		userCheck = permission.MatchUser(opts.FavoritesUserID)
	}

	err := permission.LimitCheckAny(ctx, permission.System, userCheck)
	if err != nil {
		return nil, err
	}

	data, err := (*renderData)(opts).Normalize()
	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, err
	}
	defer rows.Close()

	var result []Service
	for rows.Next() {
		var s Service
		var maintExpiresAt sql.NullTime
		err = rows.Scan(&s.ID, &s.Name, &s.Description, &s.EscalationPolicyID, &s.isUserFavorite, &maintExpiresAt)
		if err != nil {
			return nil, err
		}
		s.MaintenanceExpiresAt = maintExpiresAt.Time

		result = append(result, s)
	}

	return result, nil
}