Class: PostHog::FeatureFlagsPoller
- Inherits:
-
Object
- Object
- PostHog::FeatureFlagsPoller
show all
- Includes:
- Logging, Utils
- Defined in:
- lib/posthog/feature_flags.rb
Constant Summary
Constants included
from Utils
Utils::UTC_OFFSET_WITHOUT_COLON, Utils::UTC_OFFSET_WITH_COLON
Class Method Summary
collapse
Instance Method Summary
collapse
-
#_get_active_feature_variants(distinct_id, groups = {}, person_properties = {}, group_properties = {}) ⇒ Object
-
#get_all_flags(distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false) ⇒ Object
-
#get_all_flags_and_payloads(distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false) ⇒ Object
-
#get_decide(distinct_id, groups = {}, person_properties = {}, group_properties = {}) ⇒ Object
-
#get_feature_flag(key, distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false) ⇒ Object
-
#get_feature_flag_payload(key, distinct_id, match_value = nil, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false) ⇒ Object
-
#get_feature_payloads(distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false) ⇒ Object
-
#get_feature_variants(distinct_id, groups = {}, person_properties = {}, group_properties = {}) ⇒ Object
-
#initialize(polling_interval, personal_api_key, project_api_key, host) ⇒ FeatureFlagsPoller
constructor
A new instance of FeatureFlagsPoller.
-
#load_feature_flags(force_reload = false) ⇒ Object
-
#shutdown_poller ⇒ Object
Methods included from Utils
#convert_to_datetime, #date_in_iso8601, #datetime_in_iso8601, #formatted_offset, #is_valid_regex, #isoify_dates, #isoify_dates!, #seconds_to_utc_offset, #stringify_keys, #symbolize_keys, #symbolize_keys!, #time_in_iso8601, #uid
Methods included from Logging
included, #logger
Constructor Details
#initialize(polling_interval, personal_api_key, project_api_key, host) ⇒ FeatureFlagsPoller
Returns a new instance of FeatureFlagsPoller.
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
|
# File 'lib/posthog/feature_flags.rb', line 17
def initialize(polling_interval, personal_api_key, project_api_key, host)
@polling_interval = polling_interval || 30
@personal_api_key = personal_api_key
@project_api_key = project_api_key
@host = host
@feature_flags = Concurrent::Array.new
@group_type_mapping = Concurrent::Hash.new
@loaded_flags_successfully_once = Concurrent::AtomicBoolean.new
@feature_flags_by_key = nil
@task =
Concurrent::TimerTask.new(
execution_interval: polling_interval,
) { _load_feature_flags }
if @personal_api_key.nil?
logger.info "No personal API key provided, disabling local evaluation"
@loaded_flags_successfully_once.make_true
else
load_feature_flags
@task.execute
end
end
|
Class Method Details
.match_property(property, property_values) ⇒ Object
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
|
# File 'lib/posthog/feature_flags.rb', line 208
def self.match_property(property, property_values)
PostHog::Utils.symbolize_keys! property
PostHog::Utils.symbolize_keys! property_values
key = property[:key].to_sym
value = property[:value]
operator = property[:operator] || 'exact'
if !property_values.key?(key)
raise InconclusiveMatchError.new("Property #{key} not found in property_values")
elsif operator == 'is_not_set'
raise InconclusiveMatchError.new("Operator is_not_set not supported")
end
override_value = property_values[key]
case operator
when 'exact'
value.is_a?(Array) ? value.include?(override_value) : value == override_value
when 'is_not'
value.is_a?(Array) ? !value.include?(override_value) : value != override_value
when'is_set'
property_values.key?(key)
when 'icontains'
override_value.to_s.downcase.include?(value.to_s.downcase)
when 'not_icontains'
!override_value.to_s.downcase.include?(value.to_s.downcase)
when 'regex'
PostHog::Utils.is_valid_regex(value.to_s) && !Regexp.new(value.to_s).match(override_value.to_s).nil?
when 'not_regex'
PostHog::Utils.is_valid_regex(value.to_s) && Regexp.new(value.to_s).match(override_value.to_s).nil?
when 'gt'
override_value.class == value.class && override_value > value
when 'gte'
override_value.class == value.class && override_value >= value
when 'lt'
override_value.class == value.class && override_value < value
when 'lte'
override_value.class == value.class && override_value <= value
when 'is_date_before', 'is_date_after'
parsed_date = PostHog::Utils::convert_to_datetime(value)
override_date = PostHog::Utils::convert_to_datetime(override_value)
if operator == 'is_date_before'
return override_date < parsed_date
else
return override_date > parsed_date
end
else
logger.error "Unknown operator: #{operator}"
false
end
end
|
Instance Method Details
#_get_active_feature_variants(distinct_id, groups = {}, person_properties = {}, group_properties = {}) ⇒ Object
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/posthog/feature_flags.rb', line 58
def _get_active_feature_variants(distinct_id, groups={}, person_properties={}, group_properties={})
feature_variants = get_feature_variants(distinct_id, groups, person_properties, group_properties)
active_feature_variants = {}
feature_variants.each do |key, value|
if value != false
active_feature_variants[key] = value
end
end
active_feature_variants
end
|
#get_all_flags(distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false) ⇒ Object
140
141
142
143
144
|
# File 'lib/posthog/feature_flags.rb', line 140
def get_all_flags(distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false)
response = get_all_flags_and_payloads(distinct_id, groups, person_properties, group_properties, only_evaluate_locally)
flags = response[:featureFlags]
end
|
#get_all_flags_and_payloads(distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false) ⇒ Object
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
|
# File 'lib/posthog/feature_flags.rb', line 146
def get_all_flags_and_payloads(distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false)
load_feature_flags
flags = {}
payloads = {}
fallback_to_decide = @feature_flags.empty?
@feature_flags.each do |flag|
begin
match_value = _compute_flag_locally(flag, distinct_id, groups, person_properties, group_properties)
flags[flag[:key]] = match_value
match_payload = _compute_flag_payload_locally(flag[:key], match_value)
if match_payload
payloads[flag[:key]] = match_payload
end
rescue InconclusiveMatchError => e
fallback_to_decide = true
rescue StandardError => e
logger.error "Error computing flag locally: #{e}."
fallback_to_decide = true
end
end
if fallback_to_decide && !only_evaluate_locally
begin
flags_and_payloads = get_decide(distinct_id, groups, person_properties, group_properties)
flags = stringify_keys(flags_and_payloads[:featureFlags] || {})
payloads = stringify_keys(flags_and_payloads[:featureFlagPayloads] || {})
rescue StandardError => e
logger.error "Error computing flag remotely: #{e}"
end
end
{"featureFlags": flags, "featureFlagPayloads": payloads}
end
|
#get_decide(distinct_id, groups = {}, person_properties = {}, group_properties = {}) ⇒ Object
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/posthog/feature_flags.rb', line 78
def get_decide(distinct_id, groups={}, person_properties={}, group_properties={})
request_data = {
"distinct_id": distinct_id,
"groups": groups,
"person_properties": person_properties,
"group_properties": group_properties,
}
decide_data = _request_feature_flag_evaluation(request_data)
end
|
#get_feature_flag(key, distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false) ⇒ Object
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
|
# File 'lib/posthog/feature_flags.rb', line 89
def get_feature_flag(key, distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false)
load_feature_flags
symbolize_keys! groups
symbolize_keys! person_properties
symbolize_keys! group_properties
group_properties.each do |key, value|
symbolize_keys! value
end
response = nil
feature_flag = nil
@feature_flags.each do |flag|
if key == flag[:key]
feature_flag = flag
break
end
end
if !feature_flag.nil?
begin
response = _compute_flag_locally(feature_flag, distinct_id, groups, person_properties, group_properties)
logger.debug "Successfully computed flag locally: #{key} -> #{response}"
rescue InconclusiveMatchError => e
logger.debug "Failed to compute flag #{key} locally: #{e}"
rescue StandardError => e
logger.error "Error computing flag locally: #{e}. #{e.backtrace.join("\n")}"
end
end
flag_was_locally_evaluated = !response.nil?
if !flag_was_locally_evaluated && !only_evaluate_locally
begin
flags = get_feature_variants(distinct_id, groups, person_properties, group_properties)
response = flags[key]
if response.nil?
response = false
end
logger.debug "Successfully computed flag remotely: #{key} -> #{response}"
rescue StandardError => e
logger.error "Error computing flag remotely: #{e}. #{e.backtrace.join("\n")}"
end
end
[response, flag_was_locally_evaluated]
end
|
#get_feature_flag_payload(key, distinct_id, match_value = nil, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false) ⇒ Object
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/posthog/feature_flags.rb', line 180
def get_feature_flag_payload(key, distinct_id, match_value = nil, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false)
if match_value == nil
match_value = get_feature_flag(
key,
distinct_id,
groups,
person_properties,
group_properties,
true,
)[0]
end
response = nil
if match_value != nil
response = _compute_flag_payload_locally(key, match_value)
end
if response == nil and !only_evaluate_locally
decide_payloads = get_feature_payloads(distinct_id, groups, person_properties, group_properties)
response = decide_payloads[key.downcase] || nil
end
response
end
|
#get_feature_payloads(distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false) ⇒ Object
69
70
71
72
73
74
75
76
|
# File 'lib/posthog/feature_flags.rb', line 69
def get_feature_payloads(distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false)
decide_data = get_decide(distinct_id, groups, person_properties, group_properties)
if !decide_data.key?(:featureFlagPayloads)
logger.error "Missing feature flag payloads key: #{decide_data.to_json}"
else
stringify_keys(decide_data[:featureFlagPayloads] || {})
end
end
|
#get_feature_variants(distinct_id, groups = {}, person_properties = {}, group_properties = {}) ⇒ Object
49
50
51
52
53
54
55
56
|
# File 'lib/posthog/feature_flags.rb', line 49
def get_feature_variants(distinct_id, groups={}, person_properties={}, group_properties={})
decide_data = get_decide(distinct_id, groups, person_properties, group_properties)
if !decide_data.key?(:featureFlags)
logger.error "Missing feature flags key: #{decide_data.to_json}"
else
stringify_keys(decide_data[:featureFlags] || {})
end
end
|
#load_feature_flags(force_reload = false) ⇒ Object
43
44
45
46
47
|
# File 'lib/posthog/feature_flags.rb', line 43
def load_feature_flags(force_reload = false)
if @loaded_flags_successfully_once.false? || force_reload
_load_feature_flags
end
end
|
#shutdown_poller ⇒ Object
202
203
204
|
# File 'lib/posthog/feature_flags.rb', line 202
def shutdown_poller()
@task.shutdown
end
|