Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(flags): add relative date operators and fix numeric ops #34

Merged
merged 6 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ gem 'concurrent-ruby', require: 'concurrent'
group :development, :test do
gem 'webmock'
gem 'prettier'
gem 'timecop'
end
99 changes: 82 additions & 17 deletions lib/posthog/feature_flags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,46 @@ def shutdown_poller()

# Class methods

def self.compare(lhs, rhs, operator)
if operator == "gt"
return lhs > rhs
elsif operator == "gte"
return lhs >= rhs
elsif operator == "lt"
return lhs < rhs
elsif operator == "lte"
return lhs <= rhs
else
raise "Invalid operator: #{operator}"
end
end

def self.relative_date_parse_for_feature_flag_matching(value)
match = /([0-9]+)([a-z])/.match(value)
parsed_dt = DateTime.now.new_offset(0)
if match
number = match[1].to_i
interval = match[2]
if interval == "h"
parsed_dt = parsed_dt - (number/24r)
elsif interval == "d"
parsed_dt = parsed_dt.prev_day(number)
elsif interval == "w"
parsed_dt = parsed_dt.prev_day(number*7)
elsif interval == "m"
parsed_dt = parsed_dt.prev_month(number)
elsif interval == "y"
parsed_dt = parsed_dt.prev_year(number)
else
nil
end
return parsed_dt
else
nil
end
end


def self.match_property(property, property_values)
# only looks for matches where key exists in property_values
# doesn't support operator is_not_set
Expand All @@ -225,10 +265,20 @@ def self.match_property(property, property_values)
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 'exact', 'is_not'
if value.is_a?(Array)
values_stringified = value.map { |val| val.to_s.downcase }
if operator == 'exact'
return values_stringified.any?(override_value.to_s.downcase)
else
return !values_stringified.any?(override_value.to_s.downcase)
end
end
if operator == 'exact'
value.to_s.downcase == override_value.to_s.downcase
else
value.to_s.downcase != override_value.to_s.downcase
end
when'is_set'
property_values.key?(key)
when 'icontains'
Expand All @@ -239,20 +289,35 @@ def self.match_property(property, property_values)
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
when 'gt', 'gte', 'lt', 'lte'
parsed_value = nil
begin
parsed_value = value.to_f
rescue StandardError => e
end
if !parsed_value.nil? && !override_value.nil?
if override_value.is_a?(String)
self.compare(override_value, value.to_s, operator)
else
self.compare(override_value, parsed_value, operator)
end
else
self.compare(override_value.to_s, value.to_s, operator)
end
when 'is_date_before', 'is_date_after', 'is_relative_date_before', 'is_relative_date_after'
if operator == 'is_relative_date_before' || operator == 'is_relative_date_after'
parsed_date = self.relative_date_parse_for_feature_flag_matching(value.to_s)
override_date = PostHog::Utils.convert_to_datetime(override_value.to_s)
else
parsed_date = PostHog::Utils.convert_to_datetime(value.to_s)
override_date = PostHog::Utils.convert_to_datetime(override_value.to_s)
end
if !parsed_date
raise InconclusiveMatchError.new("Invalid date format")
end
if operator == 'is_date_before' or operator == 'is_relative_date_before'
return override_date < parsed_date
elsif operator == 'is_date_after' or operator == 'is_relative_date_after'
return override_date > parsed_date
end
else
Expand Down
135 changes: 132 additions & 3 deletions spec/posthog/feature_flag_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'spec_helper'

require 'timecop'

class PostHog

Expand Down Expand Up @@ -1153,7 +1153,7 @@ class PostHog

expect(FeatureFlagsPoller.match_property(property_a, { 'key' => 0 })).to be false
expect(FeatureFlagsPoller.match_property(property_a, { 'key' => -1 })).to be false
expect(FeatureFlagsPoller.match_property(property_a, { 'key' => "23" })).to be false
expect(FeatureFlagsPoller.match_property(property_a, { 'key' => "23" })).to be true

property_b = { 'key' => 'key', 'value' => 1, 'operator' => 'lt' }
expect(FeatureFlagsPoller.match_property(property_b, { 'key' => 0 })).to be true
Expand All @@ -1171,15 +1171,29 @@ class PostHog
expect(FeatureFlagsPoller.match_property(property_c, { 'key' => 0 })).to be false
expect(FeatureFlagsPoller.match_property(property_c, { 'key' => -1 })).to be false
expect(FeatureFlagsPoller.match_property(property_c, { 'key' => -3 })).to be false
expect(FeatureFlagsPoller.match_property(property_c, { 'key' => "3" })).to be false
expect(FeatureFlagsPoller.match_property(property_c, { 'key' => "3" })).to be true

property_d = { 'key' => 'key', 'value' => '43', 'operator' => 'lte' }
expect(FeatureFlagsPoller.match_property(property_d, { 'key' => '43' })).to be true
expect(FeatureFlagsPoller.match_property(property_d, { 'key' => '42' })).to be true

expect(FeatureFlagsPoller.match_property(property_d, { 'key' => '44' })).to be false
expect(FeatureFlagsPoller.match_property(property_d, { 'key' => 44 })).to be false
expect(FeatureFlagsPoller.match_property(property_d, { 'key' => 42 })).to be true

property_e = { 'key' => 'key', 'value' => "30", 'operator' => 'lt' }
expect(FeatureFlagsPoller.match_property(property_e, { 'key' => '29' })).to be true

# depending on the type of override, we adjust type comparison
expect(FeatureFlagsPoller.match_property(property_e, { 'key' => '100' })).to be true
expect(FeatureFlagsPoller.match_property(property_e, { 'key' => 100 })).to be false

property_f = { 'key' => 'key', 'value' => "123aloha", 'operator' => 'gt' }
expect(FeatureFlagsPoller.match_property(property_f, { 'key' => '123' })).to be false
expect(FeatureFlagsPoller.match_property(property_f, { 'key' => 122 })).to be false

# this turns into a string comparison
expect(FeatureFlagsPoller.match_property(property_f, { 'key' => 129 })).to be true
end

it 'with date operators' do
Expand Down Expand Up @@ -1220,6 +1234,121 @@ class PostHog
expect(FeatureFlagsPoller.match_property(property_d, { 'key' => '2022-04-05 11:34:13 +00:00' })).to be false

end

it 'with relative date operators - is_relative_date_before' do
Timecop.travel(Time.zone.local(2022, 5, 1)) do
property_a = { 'key' => 'key', 'value' => '6h', 'operator' => 'is_relative_date_before'}
expect(FeatureFlagsPoller.match_property(property_a, { 'key' => '2022-03-01' })).to be true
expect(FeatureFlagsPoller.match_property(property_a, { 'key' => '2022-04-30' })).to be true
expect(FeatureFlagsPoller.match_property(property_a, { 'key' => DateTime.new(2022, 4, 30, 1, 2, 3) })).to be true
# false because date comparison, instead of datetime, so reduces to same date
# we converts this to datetime for appropriate comparison anyway
# expect(FeatureFlagsPoller.match_property(property_a, { 'key' => Time.new(2022, 4, 30) })).to be true

expect(FeatureFlagsPoller.match_property(property_a, { 'key' => DateTime.new(2022, 4, 30, 19, 2, 3) })).to be false
expect(FeatureFlagsPoller.match_property(property_a, { 'key' => DateTime.new(2022, 4, 30, 1, 2, 3, '+2') })).to be true
expect(FeatureFlagsPoller.match_property(property_a, { 'key' => DateTime.parse('2022-04-30') })).to be true
expect(FeatureFlagsPoller.match_property(property_a, { 'key' => '2022-05-30' })).to be false

# can't be a number
expect{ FeatureFlagsPoller.match_property(property_a, { 'key' => 1}) }.to raise_error(InconclusiveMatchError)

# can't be invalid string
expect{ FeatureFlagsPoller.match_property(property_a, { 'key' => 'abcdef'}) }.to raise_error(InconclusiveMatchError)
end
end

it 'with relative date operators - is_relative_date_after' do
Timecop.travel(Time.zone.local(2022, 5, 1)) do
property_b = { 'key' => 'key', 'value' => '1h', 'operator' => 'is_relative_date_after'}
expect(FeatureFlagsPoller.match_property(property_b, { 'key' => '2022-05-02' })).to be true
expect(FeatureFlagsPoller.match_property(property_b, { 'key' => '2022-05-30' })).to be true
expect(FeatureFlagsPoller.match_property(property_b, { 'key' => DateTime.new(2022, 5, 30) })).to be true
expect(FeatureFlagsPoller.match_property(property_b, { 'key' => DateTime.parse('2022-05-30') })).to be true
expect(FeatureFlagsPoller.match_property(property_b, { 'key' => '2022-04-30' })).to be false

# can't be invalid string
expect{ FeatureFlagsPoller.match_property(property_b, { 'key' => 'abcdef'}) }.to raise_error(InconclusiveMatchError)
# invalid flag property
property_c = { 'key' => 'key', 'value' => 1234, 'operator' => 'is_relative_date_after'}
expect{ FeatureFlagsPoller.match_property(property_c, { 'key' => 1}) }.to raise_error(InconclusiveMatchError)
expect{ FeatureFlagsPoller.match_property(property_c, { 'key' => '2022-05-30'}) }.to raise_error(InconclusiveMatchError)
end
end

it 'with relative date operators - all possible relative dates' do
Timecop.travel(Time.zone.local(2022, 5, 1)) do
property_d = { 'key' => 'key', 'value' => '12d', 'operator' => 'is_relative_date_before'}
expect(FeatureFlagsPoller.match_property(property_d, { 'key' => '2022-05-30' })).to be false

expect(FeatureFlagsPoller.match_property(property_d, { 'key' => '2022-03-30' })).to be true
expect(FeatureFlagsPoller.match_property(property_d, { 'key' => '2022-04-05 12:34:11+01:00' })).to be true
expect(FeatureFlagsPoller.match_property(property_d, { 'key' => '2022-04-19 01:34:11+02:00' })).to be true
expect(FeatureFlagsPoller.match_property(property_d, { 'key' => '2022-04-19 02:00:01+02:00' })).to be false

# Try all possible relative dates
property_e = { 'key' => 'key', 'value' => '1h', 'operator' => 'is_relative_date_before'}
expect(FeatureFlagsPoller.match_property(property_e, { 'key' => '2022-05-01 00:00:00' })).to be false
expect(FeatureFlagsPoller.match_property(property_e, { 'key' => '2022-04-30 22:00:00' })).to be true

property_f = { 'key' => 'key', 'value' => '1d', 'operator' => 'is_relative_date_before'}
expect(FeatureFlagsPoller.match_property(property_f, { 'key' => '2022-04-29 23:59:00' })).to be true
expect(FeatureFlagsPoller.match_property(property_f, { 'key' => '2022-04-30 00:00:01' })).to be false

property_g = { 'key' => 'key', 'value' => '1w', 'operator' => 'is_relative_date_before'}
expect(FeatureFlagsPoller.match_property(property_g, { 'key' => '2022-04-23 00:00:00' })).to be true
expect(FeatureFlagsPoller.match_property(property_g, { 'key' => '2022-04-24 00:00:00' })).to be true
# true because in ruby two equivalent datetimes return true even in a < comparison
expect(FeatureFlagsPoller.match_property(property_g, { 'key' => '2022-04-24 00:00:01' })).to be false

property_h = { 'key' => 'key', 'value' => '1m', 'operator' => 'is_relative_date_before'}
expect(FeatureFlagsPoller.match_property(property_h, { 'key' => '2022-03-01 00:00:00' })).to be true
expect(FeatureFlagsPoller.match_property(property_h, { 'key' => '2022-04-05 00:00:00' })).to be false

property_i = { 'key' => 'key', 'value' => '1y', 'operator' => 'is_relative_date_before'}
expect(FeatureFlagsPoller.match_property(property_i, { 'key' => '2021-04-28 00:00:00' })).to be true
expect(FeatureFlagsPoller.match_property(property_i, { 'key' => '2021-05-01 00:00:01' })).to be false

property_j = { 'key' => 'key', 'value' => '122h', 'operator' => 'is_relative_date_after'}
expect(FeatureFlagsPoller.match_property(property_j, { 'key' => '2022-05-01 00:00:00' })).to be true
expect(FeatureFlagsPoller.match_property(property_j, { 'key' => '2022-04-23 01:00:00' })).to be false

property_k = { 'key' => 'key', 'value' => '2d', 'operator' => 'is_relative_date_after'}
expect(FeatureFlagsPoller.match_property(property_k, { 'key' => '2022-05-01 00:00:00' })).to be true
expect(FeatureFlagsPoller.match_property(property_k, { 'key' => '2022-04-29 00:00:01' })).to be true
expect(FeatureFlagsPoller.match_property(property_k, { 'key' => '2022-04-29 00:00:00' })).to be false

property_l = { 'key' => 'key', 'value' => '02w', 'operator' => 'is_relative_date_after'}
expect(FeatureFlagsPoller.match_property(property_l, { 'key' => '2022-05-01 00:00:00' })).to be true
expect(FeatureFlagsPoller.match_property(property_l, { 'key' => '2022-04-16 00:00:00' })).to be false

property_m = { 'key' => 'key', 'value' => '1m', 'operator' => 'is_relative_date_after'}
expect(FeatureFlagsPoller.match_property(property_m, { 'key' => '2022-04-01 00:00:01' })).to be true
expect(FeatureFlagsPoller.match_property(property_m, { 'key' => '2022-04-01 00:00:00' })).to be false

property_n = { 'key' => 'key', 'value' => '1y', 'operator' => 'is_relative_date_after'}
expect(FeatureFlagsPoller.match_property(property_n, { 'key' => '2022-05-01 00:00:00' })).to be true
expect(FeatureFlagsPoller.match_property(property_n, { 'key' => '2021-05-01 00:00:01' })).to be true
expect(FeatureFlagsPoller.match_property(property_n, { 'key' => '2021-05-01 00:00:00' })).to be false
expect(FeatureFlagsPoller.match_property(property_n, { 'key' => '2021-04-30 00:00:00' })).to be false
expect(FeatureFlagsPoller.match_property(property_n, { 'key' => '2021-03-01 12:13:00' })).to be false

end
end

it 'nil property value with all operators' do
neilkakkar marked this conversation as resolved.
Show resolved Hide resolved
property_a = { 'key' => 'key', 'value' => 'nil', 'operator' => 'is_not' }
expect(FeatureFlagsPoller.match_property(property_a, { 'key' => nil })).to be true # nil to string here is an empty string, not "nil" so it's true because it doesn't match
expect(FeatureFlagsPoller.match_property(property_a, { 'key' => 'non' })).to be true

property_b = { 'key' => 'key', 'value' => nil, 'operator' => 'is_set' }
expect(FeatureFlagsPoller.match_property(property_b, { 'key' => nil })).to be true

# i don't think these none test cases make sense for ruby's nil equivalent?
property_c = { 'key' => 'key', 'value' => 'no', 'operator' => 'icontains' }
# expect(FeatureFlagsPoller.match_property(property_c, { 'key' => nil })).to be true
expect(FeatureFlagsPoller.match_property(property_c, { 'key' => 'smh' })).to be false
end
end


Expand Down
Loading