standard_flags 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +229 -0
- data/README.md +837 -33
- data/lib/generators/standard_flags/admin/admin_generator.rb +115 -0
- data/lib/generators/standard_flags/admin/templates/controller.rb.tt +103 -0
- data/lib/generators/standard_flags/admin/templates/index.tsx.tt +64 -0
- data/lib/generators/standard_flags/admin/templates/show.tsx.tt +275 -0
- data/lib/generators/standard_flags/install/install_generator.rb +16 -1
- data/lib/generators/standard_flags/install/templates/flags.rb.tt +3 -2
- data/lib/generators/standard_flags/install/templates/initializer.rb.tt +10 -0
- data/lib/generators/standard_flags/migrations/migrations_generator.rb +57 -0
- data/lib/generators/standard_flags/migrations/templates/create_standard_flags_tables.rb.tt +78 -0
- data/lib/standard_flags/actor.rb +50 -0
- data/lib/standard_flags/audit_trail.rb +56 -0
- data/lib/standard_flags/config.rb +78 -0
- data/lib/standard_flags/context.rb +5 -1
- data/lib/standard_flags/definition.rb +85 -13
- data/lib/standard_flags/diagnostics.rb +35 -3
- data/lib/standard_flags/engine.rb +10 -2
- data/lib/standard_flags/env_pin.rb +15 -4
- data/lib/standard_flags/errors.rb +4 -0
- data/lib/standard_flags/evaluation.rb +5 -4
- data/lib/standard_flags/evaluator.rb +14 -1
- data/lib/standard_flags/events.rb +49 -0
- data/lib/standard_flags/health_check.rb +12 -2
- data/lib/standard_flags/health_contribution.rb +65 -0
- data/lib/standard_flags/mcp/cohorts.rb +65 -0
- data/lib/standard_flags/mcp/flags.rb +143 -0
- data/lib/standard_flags/mcp/members.rb +154 -0
- data/lib/standard_flags/mcp/writes.rb +98 -0
- data/lib/standard_flags/mcp.rb +238 -0
- data/lib/standard_flags/memo.rb +15 -1
- data/lib/standard_flags/notifiers/logger.rb +14 -0
- data/lib/standard_flags/open_feature/provider.rb +232 -0
- data/lib/standard_flags/open_feature.rb +26 -0
- data/lib/standard_flags/operations/audit.rb +218 -0
- data/lib/standard_flags/operations/base.rb +290 -0
- data/lib/standard_flags/operations/cohorts.rb +80 -0
- data/lib/standard_flags/operations/failure.rb +16 -0
- data/lib/standard_flags/operations/members.rb +164 -0
- data/lib/standard_flags/operations/restore.rb +330 -0
- data/lib/standard_flags/operations/restore_target.rb +197 -0
- data/lib/standard_flags/operations/rules.rb +233 -0
- data/lib/standard_flags/operations/validation.rb +154 -0
- data/lib/standard_flags/operations.rb +43 -0
- data/lib/standard_flags/parser.rb +16 -0
- data/lib/standard_flags/purge_job.rb +14 -0
- data/lib/standard_flags/registry.rb +38 -5
- data/lib/standard_flags/reporting.rb +51 -7
- data/lib/standard_flags/resolvers.rb +14 -7
- data/lib/standard_flags/result.rb +43 -0
- data/lib/standard_flags/rspec.rb +67 -13
- data/lib/standard_flags/runtime/active_record_store.rb +428 -0
- data/lib/standard_flags/runtime/cache.rb +98 -0
- data/lib/standard_flags/runtime/evaluation_tracker.rb +93 -0
- data/lib/standard_flags/runtime/memory_store.rb +408 -0
- data/lib/standard_flags/runtime/models.rb +43 -0
- data/lib/standard_flags/runtime/resolver.rb +256 -0
- data/lib/standard_flags/runtime/rule_snapshot.rb +60 -0
- data/lib/standard_flags/runtime/store.rb +161 -0
- data/lib/standard_flags/runtime/values.rb +181 -0
- data/lib/standard_flags/runtime.rb +266 -0
- data/lib/standard_flags/sensor.rb +12 -5
- data/lib/standard_flags/subscribers.rb +6 -0
- data/lib/standard_flags/testing.rb +100 -1
- data/lib/standard_flags/tooling/changes.rb +196 -0
- data/lib/standard_flags/tooling.rb +232 -19
- data/lib/standard_flags/version.rb +1 -1
- data/lib/standard_flags.rb +33 -5
- data/lib/tasks/standard_flags.rake +45 -1
- metadata +41 -1
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
require "set"
|
|
2
|
+
|
|
3
|
+
module StandardFlags
|
|
4
|
+
module Runtime
|
|
5
|
+
# Write-behind for standard_flags_rules.last_evaluated_at, which
|
|
6
|
+
# `flags:stale` reads to find rules nothing evaluates any more.
|
|
7
|
+
#
|
|
8
|
+
# Evaluating a flag that has a rule only adds its key to an in-memory
|
|
9
|
+
# set. Nothing is written during an evaluation. The write happens when a
|
|
10
|
+
# unit of work ends (the executor's `complete` hook, Memo::Hook, after
|
|
11
|
+
# the request or job has done its work), at most once per
|
|
12
|
+
# `config.evaluation_tracking_interval` (default an hour) per process,
|
|
13
|
+
# as one UPDATE of the rows not already marked within the interval. It
|
|
14
|
+
# never waits: rows a write holds are skipped, and so is the whole flush
|
|
15
|
+
# inside an open transaction (the keys wait for the next one). It is
|
|
16
|
+
# best-effort: a failed write is reported once and the keys are kept for
|
|
17
|
+
# the next try. `config.track_evaluations = false` turns it off.
|
|
18
|
+
#
|
|
19
|
+
# Fork-safe without a reset: a child keeps the keys it inherited and
|
|
20
|
+
# flushes them itself. The lock is never replaced (see Cache); Ruby
|
|
21
|
+
# releases a mutex held by a thread that doesn't survive the fork.
|
|
22
|
+
class EvaluationTracker
|
|
23
|
+
# Set while operator reads evaluate (see #untracked).
|
|
24
|
+
UNTRACKED = :standard_flags_untracked
|
|
25
|
+
|
|
26
|
+
def initialize
|
|
27
|
+
@mutex = Mutex.new
|
|
28
|
+
@keys = Set.new
|
|
29
|
+
@flushed_at = nil
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Whether evaluations on this thread or fiber are left untracked.
|
|
33
|
+
def self.untracked?
|
|
34
|
+
Thread.current[UNTRACKED] ? true : false
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def track(key)
|
|
38
|
+
return unless StandardFlags.config.track_evaluations
|
|
39
|
+
|
|
40
|
+
@mutex.synchronize { @keys << key }
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Called by Runtime.unit_of_work_complete! with its monotonic clock.
|
|
44
|
+
# The interval is checked and claimed under the lock, so threads that
|
|
45
|
+
# end their units of work together flush once. Never raises.
|
|
46
|
+
def flush(store, now)
|
|
47
|
+
config = StandardFlags.config
|
|
48
|
+
return unless config.track_evaluations
|
|
49
|
+
|
|
50
|
+
interval = config.evaluation_tracking_interval.to_f
|
|
51
|
+
keys = @mutex.synchronize do
|
|
52
|
+
next if @keys.empty? || (@flushed_at && now - @flushed_at < interval)
|
|
53
|
+
|
|
54
|
+
@flushed_at = now
|
|
55
|
+
@keys.to_a
|
|
56
|
+
end
|
|
57
|
+
return unless keys
|
|
58
|
+
|
|
59
|
+
if store.touch_evaluated(keys, before: Time.now.utc - interval).nil?
|
|
60
|
+
@mutex.synchronize { @flushed_at = nil }
|
|
61
|
+
else
|
|
62
|
+
@mutex.synchronize { @keys.subtract(keys) }
|
|
63
|
+
end
|
|
64
|
+
rescue StandardError => e
|
|
65
|
+
failed(e)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Evaluations inside the block (on this thread or fiber) are not
|
|
69
|
+
# tracked. Operator reads (the MCP tools, the admin page built on
|
|
70
|
+
# them) evaluate every flag they show, which is not the app using it,
|
|
71
|
+
# so they must not hide a dead flag from `flags:stale`.
|
|
72
|
+
def untracked
|
|
73
|
+
previous = Thread.current[UNTRACKED]
|
|
74
|
+
Thread.current[UNTRACKED] = true
|
|
75
|
+
yield
|
|
76
|
+
ensure
|
|
77
|
+
Thread.current[UNTRACKED] = previous
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def pending
|
|
81
|
+
@mutex.synchronize { @keys.to_a.sort }
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
private
|
|
85
|
+
|
|
86
|
+
def failed(error)
|
|
87
|
+
name = (error.cause_class if error.is_a?(StoreError)) || error.class.name
|
|
88
|
+
Reporting.once(:evaluation_tracking, name) { Reporting.report(error, runtime: "evaluation_tracking") }
|
|
89
|
+
Reporting.instrument(:evaluation_tracking_failed, error_class: name)
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
require "monitor"
|
|
2
|
+
|
|
3
|
+
module StandardFlags
|
|
4
|
+
module Runtime
|
|
5
|
+
# An in-process store, for specs and for apps without a database. Every
|
|
6
|
+
# write bumps the revision, so the cache picks it up at its next check.
|
|
7
|
+
# Thread-safe. `reads` counts calls per method, so specs can assert what
|
|
8
|
+
# evaluation cost without a database.
|
|
9
|
+
#
|
|
10
|
+
# store = StandardFlags::Runtime::MemoryStore.new
|
|
11
|
+
# store.put_rule(:new_checkout, status: :running, percentage_bps: 500)
|
|
12
|
+
# store.put_cohort(:staff, unit: :account)
|
|
13
|
+
# store.add_member(cohort: :staff, unit: :account, unit_key: "42", value: true)
|
|
14
|
+
# StandardFlags.configure { |c| c.runtime_rules = true; c.runtime_store = store }
|
|
15
|
+
#
|
|
16
|
+
# Values may be given as Ruby values (true, false, :sensor) or as their
|
|
17
|
+
# stored strings ("on", "off", "sensor").
|
|
18
|
+
#
|
|
19
|
+
# It also takes the write path (StandardFlags::Operations): `write` holds
|
|
20
|
+
# the store's lock for the whole block and restores the previous state
|
|
21
|
+
# if the block raises; after_commit blocks run once it returns, outside
|
|
22
|
+
# the lock. Removed memberships are kept, as in the database, until
|
|
23
|
+
# `purge`.
|
|
24
|
+
class MemoryStore < Store
|
|
25
|
+
RULE_ATTRIBUTES = %i[
|
|
26
|
+
status hash_version salt percentage_bps rollout_value variant_weights
|
|
27
|
+
fallthrough_value stop_value conditions cohorts full_since last_evaluated_at updated_at
|
|
28
|
+
].freeze
|
|
29
|
+
# A removed membership and when it was removed.
|
|
30
|
+
Removed = Data.define(:membership, :removed_at)
|
|
31
|
+
|
|
32
|
+
attr_reader :reads
|
|
33
|
+
|
|
34
|
+
def initialize
|
|
35
|
+
# Reentrant, so an event subscriber that evaluates a flag during a
|
|
36
|
+
# write can read the store.
|
|
37
|
+
@mutex = Monitor.new
|
|
38
|
+
@revision = 0
|
|
39
|
+
@sequence = 0 # like a database sequence: never rolled back
|
|
40
|
+
@next_id = 0
|
|
41
|
+
@rules = {}
|
|
42
|
+
@cohorts = {}
|
|
43
|
+
@memberships = []
|
|
44
|
+
@removed = []
|
|
45
|
+
@reads = Hash.new(0)
|
|
46
|
+
@pending = nil # after_commit blocks of the write in progress
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def revision
|
|
50
|
+
@mutex.synchronize do
|
|
51
|
+
@reads[:revision] += 1
|
|
52
|
+
@revision
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def load
|
|
57
|
+
@mutex.synchronize do
|
|
58
|
+
@reads[:load] += 1
|
|
59
|
+
Loaded.new(rules: @rules.values, cohorts: @cohorts.values)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def memberships(unit, unit_keys)
|
|
64
|
+
unit = unit.to_sym
|
|
65
|
+
keys = unit_keys.map(&:to_s)
|
|
66
|
+
@mutex.synchronize do
|
|
67
|
+
@reads[:memberships] += 1
|
|
68
|
+
@memberships.select { |m| m.unit == unit && keys.include?(m.unit_key) }
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# The stored rule for `key`, or nil. Not counted in `reads`.
|
|
73
|
+
def rule(key)
|
|
74
|
+
@mutex.synchronize { @rules[key.to_sym] }
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Creates or replaces the rule for `key`. Unset attributes keep their
|
|
78
|
+
# current value; the salt defaults to the key.
|
|
79
|
+
def put_rule(key, **attributes)
|
|
80
|
+
unknown = attributes.keys - RULE_ATTRIBUTES
|
|
81
|
+
raise ArgumentError, "unknown rule attribute(s): #{unknown.join(', ')}" unless unknown.empty?
|
|
82
|
+
|
|
83
|
+
key = key.to_sym
|
|
84
|
+
mutate do
|
|
85
|
+
current = @rules[key]&.to_h || { id: next_id, key: key, salt: key.to_s }
|
|
86
|
+
rule = Rule.build(**current.merge(normalize_rule(attributes), member_count: count_members(rule_id: current[:id])))
|
|
87
|
+
raise ArgumentError, "status must be one of #{Values::STATUSES.join('/')}" unless Values::STATUSES.include?(rule.status)
|
|
88
|
+
|
|
89
|
+
@rules[key] = rule
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def put_cohort(key, unit:, name: nil, archived_at: nil)
|
|
94
|
+
key = key.to_s
|
|
95
|
+
mutate do
|
|
96
|
+
id = @cohorts[key]&.id || next_id
|
|
97
|
+
@cohorts[key] = Cohort.build(id: id, key: key, name: name, unit: unit, archived_at: archived_at,
|
|
98
|
+
member_count: count_members(cohort_id: id))
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# Adds (or replaces) one membership on a rule or a cohort.
|
|
103
|
+
def add_member(unit:, unit_key:, value:, rule: nil, cohort: nil, expires_at: nil, created_at: Time.now)
|
|
104
|
+
mutate do
|
|
105
|
+
rule_id, cohort_id = owner_ids(rule, cohort)
|
|
106
|
+
unit = unit.to_sym
|
|
107
|
+
unit_key = unit_key.to_s
|
|
108
|
+
@memberships.reject! { |m| m.rule_id == rule_id && m.cohort_id == cohort_id && m.unit == unit && m.unit_key == unit_key }
|
|
109
|
+
@memberships << Membership.new(rule_id: rule_id, cohort_id: cohort_id, unit: unit, unit_key: unit_key,
|
|
110
|
+
value: Runtime.wire(value), expires_at: expires_at, created_at: created_at)
|
|
111
|
+
recount(rule_id, cohort_id)
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def remove_member(unit:, unit_key:, rule: nil, cohort: nil)
|
|
116
|
+
mutate do
|
|
117
|
+
rule_id, cohort_id = owner_ids(rule, cohort)
|
|
118
|
+
@memberships.reject! do |m|
|
|
119
|
+
m.rule_id == rule_id && m.cohort_id == cohort_id && m.unit == unit.to_sym && m.unit_key == unit_key.to_s
|
|
120
|
+
end
|
|
121
|
+
recount(rule_id, cohort_id)
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Runs the block as one write (see Store). Other threads wait until it
|
|
126
|
+
# ends.
|
|
127
|
+
def write
|
|
128
|
+
committed = nil
|
|
129
|
+
value = @mutex.synchronize do
|
|
130
|
+
outermost = @pending.nil?
|
|
131
|
+
@pending ||= []
|
|
132
|
+
begin
|
|
133
|
+
isolate { yield Transaction.new(self) }.tap { committed = @pending if outermost }
|
|
134
|
+
ensure
|
|
135
|
+
@pending = nil if outermost
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
committed&.each(&:call)
|
|
139
|
+
value
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def purge(before:)
|
|
143
|
+
@mutex.synchronize do
|
|
144
|
+
removed = @removed.count { |entry| entry.removed_at < before }
|
|
145
|
+
@removed.reject! { |entry| entry.removed_at < before }
|
|
146
|
+
expired, @memberships = @memberships.partition { |m| expired_before?(m, before) }
|
|
147
|
+
expired.map { |m| [ m.rule_id, m.cohort_id ] }.uniq.each { |ids| recount(*ids) }
|
|
148
|
+
next_revision! unless expired.empty?
|
|
149
|
+
{ removed: removed, expired: expired.size }
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def purgeable(before:)
|
|
154
|
+
@mutex.synchronize do
|
|
155
|
+
{ removed: @removed.count { |entry| entry.removed_at < before },
|
|
156
|
+
expired: @memberships.count { |m| expired_before?(m, before) } }
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Counted in `reads`, so specs can assert how often it ran.
|
|
161
|
+
def touch_evaluated(keys, before:)
|
|
162
|
+
now = Time.now
|
|
163
|
+
@mutex.synchronize do
|
|
164
|
+
@reads[:touch_evaluated] += 1
|
|
165
|
+
keys.map(&:to_sym).count do |key|
|
|
166
|
+
rule = @rules[key]
|
|
167
|
+
next false unless rule && (rule.last_evaluated_at.nil? || rule.last_evaluated_at < before)
|
|
168
|
+
|
|
169
|
+
@rules[key] = rule.with(last_evaluated_at: now)
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def members(rule: nil, cohort: nil, offset: 0, limit: nil)
|
|
175
|
+
@mutex.synchronize do
|
|
176
|
+
ids = owner_ids_or_nil(rule, cohort)
|
|
177
|
+
next MemberPage.new(memberships: [], total: 0) unless ids
|
|
178
|
+
|
|
179
|
+
live = @memberships.select { |m| [ m.rule_id, m.cohort_id ] == ids }
|
|
180
|
+
page = limit ? live.drop(offset).first(limit) : live.drop(offset)
|
|
181
|
+
MemberPage.new(memberships: page, total: live.size)
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def members_at(at, rule: nil, cohort: nil)
|
|
186
|
+
@mutex.synchronize do
|
|
187
|
+
ids = owner_ids_or_nil(rule, cohort)
|
|
188
|
+
next [] unless ids
|
|
189
|
+
|
|
190
|
+
live = @memberships.select { |m| [ m.rule_id, m.cohort_id ] == ids && existed_at?(m, at) }
|
|
191
|
+
gone = @removed.select do |entry|
|
|
192
|
+
[ entry.membership.rule_id, entry.membership.cohort_id ] == ids && entry.removed_at > at && existed_at?(entry.membership, at)
|
|
193
|
+
end
|
|
194
|
+
live + gone.map(&:membership)
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# Removed memberships, for specs. Not counted in `reads`.
|
|
199
|
+
def removed_memberships
|
|
200
|
+
@mutex.synchronize { @removed.map(&:membership) }
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
# One write. Methods are documented on Store.
|
|
204
|
+
class Transaction
|
|
205
|
+
def initialize(store)
|
|
206
|
+
@store = store
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
%i[lock_rule create_rule save_rule lock_cohort lock_cohorts create_cohort save_cohort rules_attached_to
|
|
210
|
+
upsert_members remove_members count_members lock_revision bump_revision].each do |name|
|
|
211
|
+
define_method(name) { |*args, **kwargs| @store.__send__(:"tx_#{name}", *args, **kwargs) }
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def isolated(&)
|
|
215
|
+
@store.__send__(:isolate, &)
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def after_commit(&block)
|
|
219
|
+
@store.__send__(:tx_after_commit, block)
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
private
|
|
224
|
+
|
|
225
|
+
# Restores the state (and drops after_commit blocks) the block added
|
|
226
|
+
# if it raises.
|
|
227
|
+
def isolate
|
|
228
|
+
saved = [ @rules.dup, @cohorts.dup, @memberships.dup, @removed.dup, @revision, @next_id, @pending.size ]
|
|
229
|
+
yield
|
|
230
|
+
rescue Exception # rubocop:disable Lint/RescueException -- any raise rolls the block back, then re-raises
|
|
231
|
+
@rules, @cohorts, @memberships, @removed, @revision, @next_id, pending = saved
|
|
232
|
+
@pending.slice!(pending..)
|
|
233
|
+
raise
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
def mutate
|
|
237
|
+
@mutex.synchronize do
|
|
238
|
+
result = yield
|
|
239
|
+
next_revision!
|
|
240
|
+
result
|
|
241
|
+
end
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def tx_lock_rule(key)
|
|
245
|
+
@rules[key.to_sym]
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def tx_create_rule(key, salt:, hash_version:)
|
|
249
|
+
key = key.to_sym
|
|
250
|
+
@rules[key] ||= Rule.build(id: next_id, key: key, salt: salt, hash_version: hash_version, updated_at: Time.now)
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
def tx_save_rule(rule, attributes)
|
|
254
|
+
current = @rules.fetch(rule.key)
|
|
255
|
+
@rules[rule.key] = Rule.build(**current.to_h, **attributes, lock_version: current.lock_version + 1, updated_at: Time.now)
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
def tx_lock_cohort(key)
|
|
259
|
+
@cohorts[key.to_s]
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def tx_lock_cohorts(keys)
|
|
263
|
+
keys.map(&:to_s).uniq.filter_map { |key| @cohorts[key] }.sort_by(&:id).to_h { |cohort| [ cohort.key, cohort ] }
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
def tx_create_cohort(key, unit:, name: nil, description: nil)
|
|
267
|
+
key = key.to_s
|
|
268
|
+
return if @cohorts.key?(key)
|
|
269
|
+
|
|
270
|
+
@cohorts[key] = Cohort.build(id: next_id, key: key, unit: unit, name: name, description: description)
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
def tx_after_commit(block)
|
|
274
|
+
@pending << block
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
def tx_save_cohort(cohort, attributes)
|
|
278
|
+
@cohorts[cohort.key] = Cohort.build(**@cohorts.fetch(cohort.key).to_h, **attributes)
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
def tx_rules_attached_to(cohort_key)
|
|
282
|
+
@rules.values.select { |rule| rule.cohort_attachments.any? { |key, _| key == cohort_key.to_s } }.map(&:key).sort
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
def tx_upsert_members(owner, unit:, unit_keys:, value:, expires_at:)
|
|
286
|
+
rule_id, cohort_id = owner_of(owner)
|
|
287
|
+
unit = unit.to_sym
|
|
288
|
+
counts = { added: 0, updated: 0 }
|
|
289
|
+
now = Time.now
|
|
290
|
+
unit_keys.each do |unit_key|
|
|
291
|
+
existing = @memberships.index { |m| m.rule_id == rule_id && m.cohort_id == cohort_id && m.unit == unit && m.unit_key == unit_key }
|
|
292
|
+
counts[existing ? :updated : :added] += 1
|
|
293
|
+
if existing
|
|
294
|
+
current = @memberships[existing]
|
|
295
|
+
next if current.value == value && current.expires_at == expires_at
|
|
296
|
+
|
|
297
|
+
# Versioned, as in the database: the old one is kept as removed.
|
|
298
|
+
@removed << Removed.new(membership: @memberships.delete_at(existing), removed_at: now)
|
|
299
|
+
end
|
|
300
|
+
@memberships << Membership.new(rule_id: rule_id, cohort_id: cohort_id, unit: unit, unit_key: unit_key,
|
|
301
|
+
value: value, expires_at: expires_at, created_at: now)
|
|
302
|
+
end
|
|
303
|
+
recount(rule_id, cohort_id)
|
|
304
|
+
counts
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
def tx_remove_members(owner, unit:, unit_keys:)
|
|
308
|
+
rule_id, cohort_id = owner_of(owner)
|
|
309
|
+
now = Time.now
|
|
310
|
+
gone, @memberships = @memberships.partition do |m|
|
|
311
|
+
m.rule_id == rule_id && m.cohort_id == cohort_id && m.unit == unit.to_sym && unit_keys.include?(m.unit_key)
|
|
312
|
+
end
|
|
313
|
+
@removed.concat(gone.map { |m| Removed.new(membership: m, removed_at: now) })
|
|
314
|
+
recount(rule_id, cohort_id)
|
|
315
|
+
gone.size
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
def tx_count_members(owner, now: nil)
|
|
319
|
+
rule_id, cohort_id = owner_of(owner)
|
|
320
|
+
@memberships.count { |m| m.rule_id == rule_id && m.cohort_id == cohort_id && (now.nil? || m.live?(now)) }
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
def tx_lock_revision
|
|
324
|
+
@revision
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
def tx_bump_revision
|
|
328
|
+
next_revision!
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
# A rolled-back write restores @revision but not @sequence, so its
|
|
332
|
+
# number is never handed out again.
|
|
333
|
+
def next_revision!
|
|
334
|
+
@revision = (@sequence += 1)
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
def owner_of(owner)
|
|
338
|
+
owner.is_a?(Rule) ? [ owner.id, nil ] : [ nil, owner.id ]
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
def expired_before?(membership, before)
|
|
342
|
+
!membership.expires_at.nil? && membership.expires_at < before
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
def next_id
|
|
346
|
+
@next_id += 1
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
def normalize_rule(attributes)
|
|
350
|
+
attributes.to_h do |name, value|
|
|
351
|
+
value =
|
|
352
|
+
case name
|
|
353
|
+
when :status then value.to_s
|
|
354
|
+
when :rollout_value, :fallthrough_value, :stop_value then Runtime.wire(value)
|
|
355
|
+
when :variant_weights then value.to_h { |variant, weight| [ Runtime.wire(variant), weight ] }
|
|
356
|
+
when :cohorts then normalize_attachments(value)
|
|
357
|
+
else value
|
|
358
|
+
end
|
|
359
|
+
[ name, value ]
|
|
360
|
+
end
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
# `{ staff: true }` or `[{ cohort: :staff, value: true }]`, in order.
|
|
364
|
+
def normalize_attachments(value)
|
|
365
|
+
pairs = value.is_a?(Hash) ? value.to_a : Array(value).map { |a| a.to_h.transform_keys(&:to_s).values_at("cohort", "value") }
|
|
366
|
+
pairs.map { |cohort, served| { "cohort" => cohort.to_s, "value" => Runtime.wire(served) } }
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
# Like owner_ids, but nil for an owner that doesn't exist.
|
|
370
|
+
def owner_ids_or_nil(rule, cohort)
|
|
371
|
+
raise ArgumentError, "pass exactly one of rule: or cohort:" unless rule.nil? ^ cohort.nil?
|
|
372
|
+
|
|
373
|
+
rule ? (id = @rules[rule.to_sym]&.id) && [ id, nil ] : (id = @cohorts[cohort.to_s]&.id) && [ nil, id ]
|
|
374
|
+
end
|
|
375
|
+
|
|
376
|
+
# Created by `at`, and not expired by then.
|
|
377
|
+
def existed_at?(membership, at)
|
|
378
|
+
(membership.created_at.nil? || membership.created_at <= at) && (membership.expires_at.nil? || membership.expires_at > at)
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
def owner_ids(rule, cohort)
|
|
382
|
+
raise ArgumentError, "pass exactly one of rule: or cohort:" unless rule.nil? ^ cohort.nil?
|
|
383
|
+
|
|
384
|
+
if rule
|
|
385
|
+
[ @rules.fetch(rule.to_sym) { raise ArgumentError, "no rule for :#{rule}; put_rule first" }.id, nil ]
|
|
386
|
+
else
|
|
387
|
+
[ nil, @cohorts.fetch(cohort.to_s) { raise ArgumentError, "no cohort #{cohort.inspect}; put_cohort first" }.id ]
|
|
388
|
+
end
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
def count_members(rule_id: nil, cohort_id: nil)
|
|
392
|
+
return 0 if rule_id.nil? && cohort_id.nil?
|
|
393
|
+
|
|
394
|
+
@memberships.count { |m| rule_id ? m.rule_id == rule_id : m.cohort_id == cohort_id }
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
def recount(rule_id, cohort_id)
|
|
398
|
+
if rule_id
|
|
399
|
+
key, rule = @rules.find { |_, r| r.id == rule_id }
|
|
400
|
+
@rules[key] = rule.with(member_count: count_members(rule_id: rule_id))
|
|
401
|
+
else
|
|
402
|
+
key, cohort = @cohorts.find { |_, c| c.id == cohort_id }
|
|
403
|
+
@cohorts[key] = cohort.with(member_count: count_members(cohort_id: cohort_id))
|
|
404
|
+
end
|
|
405
|
+
end
|
|
406
|
+
end
|
|
407
|
+
end
|
|
408
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
require "active_record"
|
|
2
|
+
|
|
3
|
+
module StandardFlags
|
|
4
|
+
module Runtime
|
|
5
|
+
# ActiveRecord models over the runtime tables. Required only by
|
|
6
|
+
# ActiveRecordStore, so a host with runtime rules off never loads
|
|
7
|
+
# ActiveRecord through this gem. They live under lib/, not app/models,
|
|
8
|
+
# so Rails never eager-loads them in a host without the tables.
|
|
9
|
+
#
|
|
10
|
+
# There are no associations: every read is one explicit query, which
|
|
11
|
+
# keeps strict_loading and N+1 detectors quiet.
|
|
12
|
+
#
|
|
13
|
+
# Unit keys can be phone numbers or emails. A model's filter_attributes
|
|
14
|
+
# only covers its own `inspect`; ActiveRecord filters SQL log binds
|
|
15
|
+
# through ActiveRecord::Base's, so loading these models adds `unit_key`
|
|
16
|
+
# there (Runtime.filter_unit_keys!). That is global, but the name is
|
|
17
|
+
# narrow. It can't help when prepared statements are off, because the
|
|
18
|
+
# values are then inlined into the SQL text; the health check warns.
|
|
19
|
+
module Models
|
|
20
|
+
class Record < ::ActiveRecord::Base
|
|
21
|
+
self.abstract_class = true
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
class Rule < Record
|
|
25
|
+
self.table_name = "standard_flags_rules"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
class Cohort < Record
|
|
29
|
+
self.table_name = "standard_flags_cohorts"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
class Membership < Record
|
|
33
|
+
self.table_name = "standard_flags_memberships"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
class State < Record
|
|
37
|
+
self.table_name = "standard_flags_state"
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
Runtime.filter_unit_keys!(::ActiveRecord::Base)
|
|
42
|
+
end
|
|
43
|
+
end
|