-
Notifications
You must be signed in to change notification settings - Fork 6
feat: add exposure service #75
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9bd6223
feat: add exposure service
zhukaihan acc7762
fix: access, add tests
zhukaihan 7dd7529
fix: check options
zhukaihan 509a63d
fix: rubocop lint
zhukaihan 0f3642d
fix: remove flags from options
zhukaihan 607a957
fix: redundant code
zhukaihan 804993c
feat: require api key
zhukaihan 832261c
test: fix flag keys
zhukaihan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| module AmplitudeExperiment | ||
| # Options for evaluating variants for a user. | ||
| class EvaluateOptions | ||
| attr_accessor :tracks_exposure | ||
|
|
||
| def initialize(tracks_exposure: nil) | ||
| @tracks_exposure = tracks_exposure | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| module AmplitudeExperiment | ||
| # Exposure is a class that represents a user's exposure to a set of flags. | ||
| class Exposure | ||
| attr_accessor :user, :results, :timestamp | ||
|
|
||
| def initialize(user, results) | ||
| @user = user | ||
| @results = results | ||
| @timestamp = (Time.now.to_f * 1000).to_i | ||
| end | ||
|
|
||
| def canonicalize | ||
| sb = "#{@user&.user_id&.strip} #{@user&.device_id&.strip} " | ||
| results.sort.to_h.each do |key, value| | ||
| next unless value.key | ||
|
|
||
| sb += "#{key.strip} #{value.key&.strip} " | ||
| end | ||
| sb | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| module AmplitudeExperiment | ||
| # ExposureConfig | ||
| class ExposureConfig < AmplitudeAnalytics::Config | ||
| attr_accessor :api_key, :cache_capacity | ||
|
|
||
| def initialize(api_key = nil, cache_capacity = 65_536, **kwargs) | ||
| super(**kwargs) | ||
| @api_key = api_key | ||
| @cache_capacity = cache_capacity | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| module AmplitudeExperiment | ||
| # ExposureFilter | ||
| class ExposureFilter | ||
| attr_accessor :ttl_millis | ||
|
|
||
| def initialize(size, ttl_millis = DAY_MILLIS) | ||
| @cache = LRUCache.new(size, ttl_millis) | ||
| @ttl_millis = ttl_millis | ||
| end | ||
|
|
||
| def should_track(exposure) | ||
| return false if exposure.results.empty? | ||
|
|
||
| canonical_exposure = exposure.canonicalize | ||
| track = @cache.get(canonical_exposure).nil? | ||
| @cache.put(canonical_exposure, 0) if track | ||
| track | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| require_relative '../../../amplitude' | ||
| module AmplitudeExperiment | ||
| # ExposureService | ||
| class ExposureService | ||
| def initialize(amplitude, exposure_filter) | ||
| @amplitude = amplitude | ||
| @exposure_filter = exposure_filter | ||
| end | ||
|
|
||
| def track(exposure) | ||
| return unless @exposure_filter.should_track(exposure) | ||
|
|
||
| events = ExposureService.to_exposure_events(exposure, @exposure_filter.ttl_millis) | ||
| events.each do |event| | ||
| @amplitude.track(event) | ||
| end | ||
| end | ||
|
|
||
| def self.to_exposure_events(exposure, ttl_millis) | ||
| events = [] | ||
| canonicalized = exposure.canonicalize | ||
| exposure.results.each do |flag_key, variant| | ||
| track_exposure = variant.metadata ? variant.metadata.fetch('trackExposure', true) : true | ||
| next unless track_exposure | ||
|
|
||
| # Skip default variant exposures | ||
| is_default = variant.metadata ? variant.metadata.fetch('default', false) : false | ||
| next if is_default | ||
|
|
||
| # Determine user properties to set and unset. | ||
| set_props = {} | ||
| unset_props = {} | ||
| flag_type = variant.metadata['flagType'] if variant.metadata | ||
| if flag_type != 'mutual-exclusion-group' | ||
| if variant.key | ||
| set_props["[Experiment] #{flag_key}"] = variant.key | ||
| elsif variant.value | ||
| set_props["[Experiment] #{flag_key}"] = variant.value | ||
| end | ||
| end | ||
|
|
||
| # Build event properties. | ||
| event_properties = {} | ||
| event_properties['[Experiment] Flag Key'] = flag_key | ||
| if variant.key | ||
| event_properties['[Experiment] Variant'] = variant.key | ||
| elsif variant.value | ||
| event_properties['[Experiment] Variant'] = variant.value | ||
| end | ||
| event_properties['metadata'] = variant.metadata if variant.metadata | ||
|
|
||
| # Build event. | ||
| event = AmplitudeAnalytics::BaseEvent.new( | ||
| '[Experiment] Exposure', | ||
| user_id: exposure.user.user_id, | ||
| device_id: exposure.user.device_id, | ||
| event_properties: event_properties, | ||
| user_properties: { | ||
| '$set' => set_props, | ||
| '$unset' => unset_props | ||
| }, | ||
| insert_id: "#{exposure.user.user_id} #{exposure.user.device_id} #{AmplitudeExperiment.hash_code("#{flag_key} #{canonicalized}")} #{exposure.timestamp / ttl_millis}" | ||
| ) | ||
| event.groups = exposure.user.groups if exposure.user.groups | ||
|
|
||
| events << event | ||
| end | ||
| events | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.