-
Notifications
You must be signed in to change notification settings - Fork 217
/
rule.go
372 lines (301 loc) · 8.37 KB
/
rule.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package common
import (
"context"
"database/sql"
"errors"
sq "github.com/Masterminds/squirrel"
"github.com/gofrs/uuid"
proto "github.com/golang/protobuf/ptypes"
errs "github.com/markphelps/flipt/errors"
flipt "github.com/markphelps/flipt/rpc/flipt"
"github.com/markphelps/flipt/storage"
)
// GetRule gets an individual rule
func (s *Store) GetRule(ctx context.Context, id string) (*flipt.Rule, error) {
var (
createdAt timestamp
updatedAt timestamp
rule = &flipt.Rule{}
err = s.builder.Select("id, flag_key, segment_key, \"rank\", created_at, updated_at").
From("rules").
Where(sq.And{sq.Eq{"id": id}}).
QueryRowContext(ctx).
Scan(&rule.Id, &rule.FlagKey, &rule.SegmentKey, &rule.Rank, &createdAt, &updatedAt)
)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, errs.ErrNotFoundf("rule %q", id)
}
return nil, err
}
rule.CreatedAt = createdAt.Timestamp
rule.UpdatedAt = updatedAt.Timestamp
if err := s.distributions(ctx, rule); err != nil {
return nil, err
}
return rule, nil
}
// ListRules gets all rules for a flag
func (s *Store) ListRules(ctx context.Context, flagKey string, opts ...storage.QueryOption) ([]*flipt.Rule, error) {
var (
rules []*flipt.Rule
query = s.builder.Select("id, flag_key, segment_key, \"rank\", created_at, updated_at").
From("rules").
Where(sq.Eq{"flag_key": flagKey}).
OrderBy("\"rank\" ASC")
)
params := &storage.QueryParams{}
for _, opt := range opts {
opt(params)
}
if params.Limit > 0 {
query = query.Limit(params.Limit)
}
if params.Offset > 0 {
query = query.Offset(params.Offset)
}
rows, err := query.QueryContext(ctx)
if err != nil {
return nil, err
}
defer func() {
if cerr := rows.Close(); cerr != nil && err == nil {
err = cerr
}
}()
for rows.Next() {
var (
rule flipt.Rule
createdAt timestamp
updatedAt timestamp
)
if err := rows.Scan(
&rule.Id,
&rule.FlagKey,
&rule.SegmentKey,
&rule.Rank,
&createdAt,
&updatedAt); err != nil {
return nil, err
}
rule.CreatedAt = createdAt.Timestamp
rule.UpdatedAt = updatedAt.Timestamp
if err := s.distributions(ctx, &rule); err != nil {
return nil, err
}
rules = append(rules, &rule)
}
return rules, rows.Err()
}
// CreateRule creates a rule
func (s *Store) CreateRule(ctx context.Context, r *flipt.CreateRuleRequest) (*flipt.Rule, error) {
var (
now = proto.TimestampNow()
rule = &flipt.Rule{
Id: uuid.Must(uuid.NewV4()).String(),
FlagKey: r.FlagKey,
SegmentKey: r.SegmentKey,
Rank: r.Rank,
CreatedAt: now,
UpdatedAt: now,
}
)
if _, err := s.builder.
Insert("rules").
Columns("id", "flag_key", "segment_key", "\"rank\"", "created_at", "updated_at").
Values(rule.Id, rule.FlagKey, rule.SegmentKey, rule.Rank, ×tamp{rule.CreatedAt}, ×tamp{rule.UpdatedAt}).
ExecContext(ctx); err != nil {
return nil, err
}
return rule, nil
}
// UpdateRule updates an existing rule
func (s *Store) UpdateRule(ctx context.Context, r *flipt.UpdateRuleRequest) (*flipt.Rule, error) {
query := s.builder.Update("rules").
Set("segment_key", r.SegmentKey).
Set("updated_at", ×tamp{proto.TimestampNow()}).
Where(sq.And{sq.Eq{"id": r.Id}, sq.Eq{"flag_key": r.FlagKey}})
res, err := query.ExecContext(ctx)
if err != nil {
return nil, err
}
count, err := res.RowsAffected()
if err != nil {
return nil, err
}
if count != 1 {
return nil, errs.ErrNotFoundf("rule %q", r.Id)
}
return s.GetRule(ctx, r.Id)
}
// DeleteRule deletes a rule
func (s *Store) DeleteRule(ctx context.Context, r *flipt.DeleteRuleRequest) error {
tx, err := s.db.Begin()
if err != nil {
return err
}
// delete rule
//nolint
_, err = s.builder.Delete("rules").
RunWith(tx).
Where(sq.And{sq.Eq{"id": r.Id}, sq.Eq{"flag_key": r.FlagKey}}).
ExecContext(ctx)
// reorder existing rules after deletion
rows, err := s.builder.Select("id").
RunWith(tx).
From("rules").
Where(sq.Eq{"flag_key": r.FlagKey}).
OrderBy("\"rank\" ASC").
QueryContext(ctx)
if err != nil {
_ = tx.Rollback()
return err
}
defer func() {
if cerr := rows.Close(); cerr != nil && err == nil {
_ = tx.Rollback()
err = cerr
}
}()
var ruleIDs []string
for rows.Next() {
var ruleID string
if err := rows.Scan(&ruleID); err != nil {
_ = tx.Rollback()
return err
}
ruleIDs = append(ruleIDs, ruleID)
}
if err := rows.Err(); err != nil {
_ = tx.Rollback()
return err
}
if err := s.orderRules(ctx, tx, r.FlagKey, ruleIDs); err != nil {
_ = tx.Rollback()
return err
}
return tx.Commit()
}
// OrderRules orders rules
func (s *Store) OrderRules(ctx context.Context, r *flipt.OrderRulesRequest) error {
tx, err := s.db.Begin()
if err != nil {
return err
}
if err := s.orderRules(ctx, tx, r.FlagKey, r.RuleIds); err != nil {
_ = tx.Rollback()
return err
}
return tx.Commit()
}
func (s *Store) orderRules(ctx context.Context, runner sq.BaseRunner, flagKey string, ruleIDs []string) error {
updatedAt := proto.TimestampNow()
for i, id := range ruleIDs {
_, err := s.builder.Update("rules").
RunWith(runner).
Set("\"rank\"", i+1).
Set("updated_at", ×tamp{updatedAt}).
Where(sq.And{sq.Eq{"id": id}, sq.Eq{"flag_key": flagKey}}).
ExecContext(ctx)
if err != nil {
return err
}
}
return nil
}
// CreateDistribution creates a distribution
func (s *Store) CreateDistribution(ctx context.Context, r *flipt.CreateDistributionRequest) (*flipt.Distribution, error) {
var (
now = proto.TimestampNow()
d = &flipt.Distribution{
Id: uuid.Must(uuid.NewV4()).String(),
RuleId: r.RuleId,
VariantId: r.VariantId,
Rollout: r.Rollout,
CreatedAt: now,
UpdatedAt: now,
}
)
if _, err := s.builder.
Insert("distributions").
Columns("id", "rule_id", "variant_id", "rollout", "created_at", "updated_at").
Values(d.Id, d.RuleId, d.VariantId, d.Rollout, ×tamp{d.CreatedAt}, ×tamp{d.UpdatedAt}).
ExecContext(ctx); err != nil {
return nil, err
}
return d, nil
}
// UpdateDistribution updates an existing distribution
func (s *Store) UpdateDistribution(ctx context.Context, r *flipt.UpdateDistributionRequest) (*flipt.Distribution, error) {
query := s.builder.Update("distributions").
Set("rollout", r.Rollout).
Set("updated_at", ×tamp{proto.TimestampNow()}).
Where(sq.And{sq.Eq{"id": r.Id}, sq.Eq{"rule_id": r.RuleId}, sq.Eq{"variant_id": r.VariantId}})
res, err := query.ExecContext(ctx)
if err != nil {
return nil, err
}
count, err := res.RowsAffected()
if err != nil {
return nil, err
}
if count != 1 {
return nil, errs.ErrNotFoundf("distribution %q", r.Id)
}
var (
createdAt timestamp
updatedAt timestamp
distribution = &flipt.Distribution{}
)
if err := s.builder.Select("id, rule_id, variant_id, rollout, created_at, updated_at").
From("distributions").
Where(sq.And{sq.Eq{"id": r.Id}, sq.Eq{"rule_id": r.RuleId}, sq.Eq{"variant_id": r.VariantId}}).
QueryRowContext(ctx).
Scan(&distribution.Id, &distribution.RuleId, &distribution.VariantId, &distribution.Rollout, &createdAt, &updatedAt); err != nil {
return nil, err
}
distribution.CreatedAt = createdAt.Timestamp
distribution.UpdatedAt = updatedAt.Timestamp
return distribution, nil
}
// DeleteDistribution deletes a distribution
func (s *Store) DeleteDistribution(ctx context.Context, r *flipt.DeleteDistributionRequest) error {
_, err := s.builder.Delete("distributions").
Where(sq.And{sq.Eq{"id": r.Id}, sq.Eq{"rule_id": r.RuleId}, sq.Eq{"variant_id": r.VariantId}}).
ExecContext(ctx)
return err
}
func (s *Store) distributions(ctx context.Context, rule *flipt.Rule) (err error) {
query := s.builder.Select("id", "rule_id", "variant_id", "rollout", "created_at", "updated_at").
From("distributions").
Where(sq.Eq{"rule_id": rule.Id}).
OrderBy("created_at ASC")
rows, err := query.QueryContext(ctx)
if err != nil {
return err
}
defer func() {
if cerr := rows.Close(); cerr != nil && err == nil {
err = cerr
}
}()
for rows.Next() {
var (
distribution flipt.Distribution
createdAt, updatedAt timestamp
)
if err := rows.Scan(
&distribution.Id,
&distribution.RuleId,
&distribution.VariantId,
&distribution.Rollout,
&createdAt,
&updatedAt); err != nil {
return err
}
distribution.CreatedAt = createdAt.Timestamp
distribution.UpdatedAt = updatedAt.Timestamp
rule.Distributions = append(rule.Distributions, &distribution)
}
return rows.Err()
}