star_gates 0.3.1 → 0.4.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.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StarGates
4
- VERSION = '0.3.1'
4
+ VERSION = '0.4.0'
5
5
  end
data/lib/star_gates.rb CHANGED
@@ -3,6 +3,8 @@
3
3
  require 'zeitwerk'
4
4
  require 'active_support'
5
5
  require 'active_support/core_ext/numeric/time'
6
+ require 'deadline_machines'
7
+ require 'probabilistic_machines'
6
8
  require 'breaker_machines'
7
9
  require_relative 'star_gates/errors'
8
10
  require_relative 'star_gates/dsl'
@@ -30,6 +32,9 @@ loader.ignore("#{__dir__}/star_gates/hexagonal")
30
32
  loader.ignore("#{__dir__}/star_gates/projections")
31
33
  loader.ignore("#{__dir__}/star_gates/telemetry")
32
34
  loader.setup
35
+ # Defines StarGates.configuration and StarGates.configure, which no constant
36
+ # reference would autoload.
37
+ require_relative 'star_gates/configuration'
33
38
 
34
39
  module StarGates
35
40
  @registry = Registry.new
@@ -48,11 +53,20 @@ module StarGates
48
53
  context.instance_eval(&) if block_given?
49
54
  end
50
55
 
56
+ # KeyError for a name with no gate.
51
57
  def [](name)
52
- gate = registry[name]
53
- raise GateNotFoundError, "Gate #{name} not found" unless gate
58
+ registry.fetch(name)
59
+ end
60
+
61
+ # Monotonic Integer nanoseconds: every in-process instant and span.
62
+ def monotonic_time
63
+ Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
64
+ end
54
65
 
55
- gate
66
+ # Integer nanoseconds since the Unix epoch, for records that outlive the
67
+ # process (event-sourced events).
68
+ def wall_time
69
+ Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond)
56
70
  end
57
71
 
58
72
  def activate(name, context = {})
@@ -79,8 +93,12 @@ module StarGates
79
93
  @test_mode = false
80
94
  end
81
95
 
96
+ # Forgets every gate and the configured feature service and telemetry
97
+ # adapter, so the next caller starts from nothing.
82
98
  def reset!
83
99
  registry.clear
100
+ @feature_service = nil
101
+ @telemetry_adapter = nil
84
102
  end
85
103
 
86
104
  # Instrumentation for feature flag events
@@ -114,7 +132,7 @@ module StarGates
114
132
  gate_info = {
115
133
  name: name,
116
134
  feature: feature&.to_h,
117
- circuit: circuit ? circuit_info(circuit) : nil,
135
+ circuit: circuit&.to_h,
118
136
  status: gate.status
119
137
  }
120
138
 
@@ -141,7 +159,7 @@ module StarGates
141
159
  features_by_status: Hash.new(0),
142
160
  average_rollout: 0,
143
161
  rollout_distribution: {},
144
- generated_at: Time.now
162
+ generated_at: monotonic_time
145
163
  }
146
164
 
147
165
  rollout_percentages = []
@@ -170,7 +188,7 @@ module StarGates
170
188
  end
171
189
 
172
190
  stats[:average_rollout] =
173
- rollout_percentages.any? ? (rollout_percentages.sum / rollout_percentages.size).round(2) : 0
191
+ rollout_percentages.any? ? rollout_percentages.sum.fdiv(rollout_percentages.size).round(2) : 0
174
192
 
175
193
  stats
176
194
  end
@@ -197,46 +215,43 @@ module StarGates
197
215
  }
198
216
  end
199
217
 
200
- # Recent state transitions across all features
218
+ # Recent state transitions across all features, newest first, as
219
+ # `{ transitions:, total_found:, limit:, generated_at: }` in both modes;
220
+ # `generated_at` is the monotonic instant. Every transition is
221
+ # `{ feature:, from:, to:, event:, occurred_at: }` in both modes: `feature`
222
+ # the String feature id (the gate name without event sourcing), `from`,
223
+ # `to` and `event` Symbols, `occurred_at` wall-clock Integer nanoseconds.
224
+ # With event sourcing `total_found` counts the transitions the projection
225
+ # keeps; otherwise the registered features' transitions.
201
226
  def recent_transitions(limit: 10)
202
- if feature_service_enabled?
203
- feature_service.recent_transitions(limit: limit)
204
- else
205
- # Fallback to legacy implementation
206
- gates = registry.to_h
207
- all_transitions = []
208
-
209
- gates.each_value do |gate|
210
- feature = gate.feature
211
- next unless feature
212
-
213
- feature_transitions = feature.instance_variable_get(:@transition_history)
214
- next unless feature_transitions
227
+ recent, total = feature_service_enabled? ? projected_transitions(limit) : registered_transitions(limit)
228
+ { transitions: recent, total_found: total, limit: limit, generated_at: monotonic_time }
229
+ end
215
230
 
216
- all_transitions.concat(feature_transitions.to_a)
217
- end
231
+ def projected_transitions(limit)
232
+ recent = feature_service.recent_transitions(limit: limit).map do |transition|
233
+ transition_entry(transition[:feature_id], transition[:from_state], transition[:to_state],
234
+ transition[:event_name], transition[:occurred_at])
235
+ end
236
+ [recent, feature_service.transition_count]
237
+ end
218
238
 
219
- # Sort by timestamp descending and take the most recent
220
- recent = all_transitions.sort_by { |t| -t[:timestamp].to_f }.first(limit)
221
-
222
- {
223
- transitions: recent.map do |transition|
224
- {
225
- timestamp: transition[:timestamp],
226
- feature_name: transition[:feature_name],
227
- feature_class: transition[:feature_class],
228
- from_state: transition[:from_state],
229
- to_state: transition[:to_state],
230
- event: transition[:event],
231
- duration_since: Time.now - transition[:timestamp]
232
- }
233
- end,
234
- total_found: all_transitions.size,
235
- limit: limit,
236
- generated_at: Time.now
237
- }
239
+ def registered_transitions(limit)
240
+ gates = registry.values.select { |gate| gate.feature.respond_to?(:transition_history) }
241
+ recent = gates.flat_map do |gate|
242
+ gate.feature.transition_history(limit: limit)[:transitions].map { |transition| [gate.name, transition] }
238
243
  end
244
+ recent = recent.sort_by { |_, transition| -transition[:at] }.first(limit).map do |name, transition|
245
+ transition_entry(name, transition[:from_state], transition[:to_state], transition[:event],
246
+ transition[:occurred_at])
247
+ end
248
+ [recent, gates.sum { |gate| gate.feature.transition_count }]
249
+ end
250
+
251
+ def transition_entry(feature, from, to, event, occurred_at)
252
+ { feature: feature.to_s, from: from, to: to, event: event, occurred_at: occurred_at }
239
253
  end
254
+ private :projected_transitions, :registered_transitions, :transition_entry
240
255
 
241
256
  # Configure the foundational architecture
242
257
  def configure_foundation(event_store: nil, adapter: nil, telemetry: nil)
@@ -280,23 +295,6 @@ module StarGates
280
295
  def configure_telemetry(adapter)
281
296
  @telemetry_adapter = adapter
282
297
  end
283
-
284
- # Check if jsonrpc-rails is available
285
- def jsonrpc_rails_available?
286
- defined?(JSON_RPC) && defined?(ActionController::Base)
287
- end
288
-
289
- private
290
-
291
- def circuit_info(circuit)
292
- if circuit.respond_to?(:to_h)
293
- circuit.to_h
294
- elsif circuit.respond_to?(:stats)
295
- circuit.stats
296
- else
297
- { status: circuit.respond_to?(:status) ? circuit.status : 'unknown' }
298
- end
299
- end
300
298
  end
301
299
  end
302
300