-
Notifications
You must be signed in to change notification settings - Fork 218
/
evaluation.go
147 lines (123 loc) · 4.04 KB
/
evaluation.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
package db
import (
"context"
"database/sql"
sq "github.com/Masterminds/squirrel"
flipt "github.com/markphelps/flipt/rpc"
"github.com/markphelps/flipt/storage"
)
var _ storage.EvaluationStore = &EvaluationStore{}
type optionalConstraint struct {
ID sql.NullString
Type sql.NullInt64
Property sql.NullString
Operator sql.NullString
Value sql.NullString
}
// EvaluationStore is a SQL EvaluationStore
type EvaluationStore struct {
builder sq.StatementBuilderType
}
// NewEvaluationStore creates an EvaluationStore
func NewEvaluationStore(builder sq.StatementBuilderType) *EvaluationStore {
return &EvaluationStore{
builder: builder,
}
}
func (s *EvaluationStore) GetEvaluationRules(ctx context.Context, flagKey string) ([]*storage.EvaluationRule, error) {
// get all rules for flag with their constraints if any
rows, err := s.builder.Select("r.id, r.flag_key, r.segment_key, s.match_type, r.rank, c.id, c.type, c.property, c.operator, c.value").
From("rules r").
Join("segments s on (r.segment_key = s.key)").
LeftJoin("constraints c ON (s.key = c.segment_key)").
Where(sq.Eq{"r.flag_key": flagKey}).
OrderBy("r.rank ASC").
GroupBy("r.id, c.id, s.match_type").
QueryContext(ctx)
if err != nil {
return nil, err
}
defer func() {
if cerr := rows.Close(); cerr != nil && err == nil {
err = cerr
}
}()
var (
seenRules = make(map[string]*storage.EvaluationRule)
rules = []*storage.EvaluationRule{}
)
for rows.Next() {
var (
tempRule storage.EvaluationRule
optionalConstraint optionalConstraint
)
if err := rows.Scan(&tempRule.ID, &tempRule.FlagKey, &tempRule.SegmentKey, &tempRule.SegmentMatchType, &tempRule.Rank, &optionalConstraint.ID, &optionalConstraint.Type, &optionalConstraint.Property, &optionalConstraint.Operator, &optionalConstraint.Value); err != nil {
return nil, err
}
if existingRule, ok := seenRules[tempRule.ID]; ok {
// current rule we know about
if optionalConstraint.ID.Valid {
constraint := storage.EvaluationConstraint{
ID: optionalConstraint.ID.String,
Type: flipt.ComparisonType(optionalConstraint.Type.Int64),
Property: optionalConstraint.Property.String,
Operator: optionalConstraint.Operator.String,
Value: optionalConstraint.Value.String,
}
existingRule.Constraints = append(existingRule.Constraints, constraint)
}
} else {
// haven't seen this rule before
newRule := &storage.EvaluationRule{
ID: tempRule.ID,
FlagKey: tempRule.FlagKey,
SegmentKey: tempRule.SegmentKey,
SegmentMatchType: tempRule.SegmentMatchType,
Rank: tempRule.Rank,
}
if optionalConstraint.ID.Valid {
constraint := storage.EvaluationConstraint{
ID: optionalConstraint.ID.String,
Type: flipt.ComparisonType(optionalConstraint.Type.Int64),
Property: optionalConstraint.Property.String,
Operator: optionalConstraint.Operator.String,
Value: optionalConstraint.Value.String,
}
newRule.Constraints = append(newRule.Constraints, constraint)
}
seenRules[newRule.ID] = newRule
rules = append(rules, newRule)
}
}
if err := rows.Err(); err != nil {
return nil, err
}
return rules, nil
}
func (s *EvaluationStore) GetEvaluationDistributions(ctx context.Context, ruleID string) ([]*storage.EvaluationDistribution, error) {
rows, err := s.builder.Select("d.id", "d.rule_id", "d.variant_id", "d.rollout", "v.key").
From("distributions d").
Join("variants v ON (d.variant_id = v.id)").
Where(sq.Eq{"d.rule_id": ruleID}).
QueryContext(ctx)
if err != nil {
return nil, err
}
defer func() {
if cerr := rows.Close(); cerr != nil && err == nil {
err = cerr
}
}()
var distributions []*storage.EvaluationDistribution
for rows.Next() {
var d storage.EvaluationDistribution
if err := rows.Scan(&d.ID, &d.RuleID, &d.VariantID, &d.Rollout, &d.VariantKey); err != nil {
return nil, err
}
distributions = append(distributions, &d)
}
if err := rows.Err(); err != nil {
return nil, err
}
return distributions, nil
}