-
Notifications
You must be signed in to change notification settings - Fork 6
feat!: evaluation context mappers #87
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 1 commit
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
a67d321
fix: main-mappers-and-tests
Zaimwa9 dc19da6
fix: added-metadata
Zaimwa9 d536c81
fix: renaming
Zaimwa9 79b3176
fix: comments
Zaimwa9 45b926e
fix: remoevd-redundant-test
Zaimwa9 9ea952d
feat: reviewed-engine-test-todos
Zaimwa9 b9d14a8
Update lib/flagsmith/engine/evaluation/mappers.rb
Zaimwa9 cb3558b
feat: use-overrides-key-hash
Zaimwa9 58bfe8b
feat: removed-identifiers-join
Zaimwa9 3d7fa00
feat: flagsmith-id-in-snake-case
Zaimwa9 075a8a4
feat: run-lint
Zaimwa9 73b841c
feat: renamed-module-evaluation
Zaimwa9 51894ad
feat: fixed-priority-0-being-skipped
Zaimwa9 e1eabd2
feat: reverted-to-hash
Zaimwa9 0386836
feat: linter
Zaimwa9 d86ba90
feat: split-functions-and-module-for-linting
Zaimwa9 f8ea862
feat: get-rid-of-extra-map-nested-rule-function
Zaimwa9 b2c3d30
feat: renaming-identity-methods
Zaimwa9 7de46c2
feat: fixed-forgotten-func-renaming
Zaimwa9 10b256c
Update lib/flagsmith/engine/evaluation/mappers/environment.rb
Zaimwa9 6c8dc0b
feat: added-name-env-model-and-fixture
Zaimwa9 1a908fe
feat: moved-mappers-to-engine-namespace
Zaimwa9 0c9650a
feat: removed-feature-key
Zaimwa9 4b106a7
feat: linter
Zaimwa9 f1026a2
feat: get evaluation get result (#88)
Zaimwa9 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Flagsmith | ||
| module Engine | ||
| module Evaluation | ||
| module Mappers | ||
| # Handles environment and feature mapping | ||
| module Environment | ||
| def self.build_environment_context(environment) | ||
| { | ||
| key: environment.api_key, | ||
| name: environment.project.name | ||
Zaimwa9 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| end | ||
|
|
||
| def self.build_features_context(feature_states) | ||
| features = {} | ||
| feature_states.each do |feature_state| | ||
| features[feature_state.feature.name] = build_feature_hash(feature_state) | ||
| end | ||
| features | ||
| end | ||
|
|
||
| def self.build_feature_hash(feature_state) # rubocop:disable Metrics/MethodLength | ||
| feature_hash = { | ||
| key: feature_state.django_id&.to_s || feature_state.uuid, | ||
| feature_key: feature_state.feature.id.to_s, | ||
| name: feature_state.feature.name, | ||
| enabled: feature_state.enabled, | ||
| value: feature_state.get_value, | ||
| metadata: { flagsmith_id: feature_state.feature.id } | ||
| } | ||
| add_variants_to_feature(feature_hash, feature_state) | ||
| add_priority_to_feature(feature_hash, feature_state) | ||
| feature_hash | ||
| end | ||
|
|
||
| def self.add_variants_to_feature(feature_hash, feature_state) | ||
| return unless feature_state.multivariate_feature_state_values&.any? | ||
|
|
||
| feature_hash[:variants] = feature_state.multivariate_feature_state_values.map do |mv| | ||
| { | ||
| value: mv.multivariate_feature_option.value, | ||
| weight: mv.percentage_allocation, | ||
| priority: mv.id || uuid_to_big_int(mv.mv_fs_value_uuid) | ||
| } | ||
| end | ||
| end | ||
|
|
||
| def self.uuid_to_big_int(uuid) | ||
| uuid.gsub('-', '').to_i(16) | ||
| end | ||
|
|
||
| def self.add_priority_to_feature(feature_hash, feature_state) | ||
| priority = feature_state.feature_segment&.priority | ||
| feature_hash[:priority] = priority unless priority.nil? | ||
| end | ||
| end | ||
| end | ||
| 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,100 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Flagsmith | ||
| module Engine | ||
| module Evaluation | ||
| module Mappers | ||
| # Handles identity and override mapping | ||
| module Identity | ||
| def self.build_environment_context(identity, override_traits = nil) | ||
| traits = override_traits || identity.identity_traits | ||
|
|
||
| { | ||
| identifier: identity.identifier, | ||
| key: identity.django_id&.to_s || identity.composite_key, | ||
| traits: build_traits_hash(traits) | ||
| } | ||
| end | ||
|
|
||
| def self.build_traits_hash(traits) | ||
| traits_hash = {} | ||
| traits.each do |trait| | ||
| traits_hash[trait.trait_key] = trait.trait_value | ||
| end | ||
| traits_hash | ||
| end | ||
|
|
||
| def self.map_overrides_to_segments(identity_overrides) | ||
| features_to_identifiers = group_by_overrides(identity_overrides) | ||
| build_segments(features_to_identifiers) | ||
| end | ||
|
|
||
| def self.group_by_overrides(identity_overrides) | ||
| features_to_identifiers = {} | ||
|
|
||
| identity_overrides.each do |identity| | ||
| next if identity.identity_features.nil? || identity.identity_features.none? | ||
|
|
||
| overrides_key = build_overrides_key(identity.identity_features) | ||
| overrides_hash = overrides_key.hash | ||
|
|
||
| features_to_identifiers[overrides_hash] ||= { identifiers: [], overrides: overrides_key } | ||
| features_to_identifiers[overrides_hash][:identifiers] << identity.identifier | ||
| end | ||
|
|
||
| features_to_identifiers | ||
| end | ||
|
|
||
| def self.build_overrides_key(identity_features) # rubocop:disable Metrics/MethodLength | ||
Zaimwa9 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| sorted_features = identity_features.to_a.sort_by { |fs| fs.feature.name } | ||
| sorted_features.map do |feature_state| | ||
| { | ||
| feature_key: feature_state.feature.id.to_s, | ||
| name: feature_state.feature.name, | ||
| enabled: feature_state.enabled, | ||
| value: feature_state.get_value, | ||
| priority: Mappers::STRONGEST_PRIORITY, | ||
| metadata: { flagsmith_id: feature_state.feature.id } | ||
| } | ||
| end | ||
| end | ||
|
|
||
| def self.build_segments(features_to_identifiers) | ||
| segments = {} | ||
|
|
||
| features_to_identifiers.each do |overrides_hash, data| | ||
| segment_key = "identity_override_#{overrides_hash}" | ||
| segments[segment_key] = build_segment(segment_key, data) | ||
| end | ||
|
|
||
| segments | ||
| end | ||
|
|
||
| def self.build_segment(segment_key, data) | ||
| { | ||
| key: segment_key, | ||
| name: 'identity_override', | ||
| rules: [build_rule(data[:identifiers])], | ||
| metadata: { source: 'identity_override' }, | ||
| overrides: data[:overrides] | ||
| } | ||
| end | ||
|
|
||
| def self.build_rule(identifiers) # rubocop:disable Metrics/MethodLength | ||
| { | ||
| type: 'ALL', | ||
| conditions: [ | ||
| { | ||
| property: '$.identity.identifier', | ||
| operator: 'IN', | ||
| value: identifiers | ||
| } | ||
| ], | ||
| rules: [] | ||
| } | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
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.