star_gates 0.3.2 → 0.5.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.
data/lib/star_gates.rb CHANGED
@@ -32,6 +32,9 @@ loader.ignore("#{__dir__}/star_gates/hexagonal")
32
32
  loader.ignore("#{__dir__}/star_gates/projections")
33
33
  loader.ignore("#{__dir__}/star_gates/telemetry")
34
34
  loader.setup
35
+ # Defines StarGates.configuration and StarGates.configure, which no constant
36
+ # reference would autoload.
37
+ require_relative 'star_gates/configuration'
35
38
 
36
39
  module StarGates
37
40
  @registry = Registry.new
@@ -50,11 +53,20 @@ module StarGates
50
53
  context.instance_eval(&) if block_given?
51
54
  end
52
55
 
56
+ # KeyError for a name with no gate.
53
57
  def [](name)
54
- gate = registry[name]
55
- 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
56
65
 
57
- 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)
58
70
  end
59
71
 
60
72
  def activate(name, context = {})
@@ -81,8 +93,12 @@ module StarGates
81
93
  @test_mode = false
82
94
  end
83
95
 
96
+ # Forgets every gate and the configured feature service and telemetry
97
+ # adapter, so the next caller starts from nothing.
84
98
  def reset!
85
99
  registry.clear
100
+ @feature_service = nil
101
+ @telemetry_adapter = nil
86
102
  end
87
103
 
88
104
  # Instrumentation for feature flag events
@@ -116,7 +132,7 @@ module StarGates
116
132
  gate_info = {
117
133
  name: name,
118
134
  feature: feature&.to_h,
119
- circuit: circuit ? circuit_info(circuit) : nil,
135
+ circuit: circuit&.to_h,
120
136
  status: gate.status
121
137
  }
122
138
 
@@ -143,7 +159,7 @@ module StarGates
143
159
  features_by_status: Hash.new(0),
144
160
  average_rollout: 0,
145
161
  rollout_distribution: {},
146
- generated_at: Time.now
162
+ generated_at: monotonic_time
147
163
  }
148
164
 
149
165
  rollout_percentages = []
@@ -172,7 +188,7 @@ module StarGates
172
188
  end
173
189
 
174
190
  stats[:average_rollout] =
175
- 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
176
192
 
177
193
  stats
178
194
  end
@@ -199,47 +215,44 @@ module StarGates
199
215
  }
200
216
  end
201
217
 
202
- # 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.
203
226
  def recent_transitions(limit: 10)
204
- if feature_service_enabled?
205
- feature_service.recent_transitions(limit: limit)
206
- else
207
- # Fallback to legacy implementation
208
- gates = registry.to_h
209
- all_transitions = []
210
-
211
- gates.each_value do |gate|
212
- feature = gate.feature
213
- next unless feature
214
-
215
- feature_transitions = feature.instance_variable_get(:@transition_history)
216
- 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
217
230
 
218
- all_transitions.concat(feature_transitions.to_a)
219
- 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
220
238
 
221
- # Sort by timestamp descending and take the most recent
222
- recent = all_transitions.sort_by { |t| -t[:timestamp].to_f }.first(limit)
223
-
224
- {
225
- transitions: recent.map do |transition|
226
- {
227
- timestamp: transition[:timestamp],
228
- feature_name: transition[:feature_name],
229
- feature_class: transition[:feature_class],
230
- from_state: transition[:from_state],
231
- to_state: transition[:to_state],
232
- event: transition[:event],
233
- duration_since: Time.now - transition[:timestamp]
234
- }
235
- end,
236
- total_found: all_transitions.size,
237
- limit: limit,
238
- generated_at: Time.now
239
- }
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] }
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])
240
247
  end
248
+ [recent, gates.sum { |gate| gate.feature.transition_count }]
241
249
  end
242
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 }
253
+ end
254
+ private :projected_transitions, :registered_transitions, :transition_entry
255
+
243
256
  # Configure the foundational architecture
244
257
  def configure_foundation(event_store: nil, adapter: nil, telemetry: nil)
245
258
  event_store ||= EventStore::EventStore.new
@@ -282,23 +295,6 @@ module StarGates
282
295
  def configure_telemetry(adapter)
283
296
  @telemetry_adapter = adapter
284
297
  end
285
-
286
- # Check if jsonrpc-rails is available
287
- def jsonrpc_rails_available?
288
- defined?(JSON_RPC) && defined?(ActionController::Base)
289
- end
290
-
291
- private
292
-
293
- def circuit_info(circuit)
294
- if circuit.respond_to?(:to_h)
295
- circuit.to_h
296
- elsif circuit.respond_to?(:stats)
297
- circuit.stats
298
- else
299
- { status: circuit.respond_to?(:status) ? circuit.status : 'unknown' }
300
- end
301
- end
302
298
  end
303
299
  end
304
300
 
@@ -308,12 +304,3 @@ class Numeric
308
304
  self
309
305
  end
310
306
  end
311
-
312
- # Optional jsonrpc-rails integration
313
- begin
314
- require 'rails'
315
- require 'jsonrpc-rails'
316
- require_relative 'star_gates/jsonrpc_rails_adapter'
317
- rescue LoadError
318
- # jsonrpc-rails or Rails not available, skip integration
319
- end