Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions .github/workflows/sdk-spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ permissions:
jobs:
sdk-spec:
# Keep the ref in `uses:` and `spec-ref` in sync.
uses: Flagsmith/sdk-spec/.github/workflows/run-spec.yml@v0.9.0
uses: Flagsmith/sdk-spec/.github/workflows/run-spec.yml@v0.10.0
with:
spec-ref: v0.9.0
spec-ref: v0.10.0
language: elixir
tags: core events local-evaluation
32 changes: 14 additions & 18 deletions lib/flagsmith_engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -153,25 +153,21 @@

defp to_feature_states(feature_states, flags) do
Enum.map(feature_states, fn %{feature: %{name: name}} ->
case Map.fetch!(flags, name) do
%FlagResult{
enabled: enabled,
value: value,
%FlagResult{
enabled: enabled,
value: value,
variant: variant,
reason: reason,
metadata: %{feature_state: feature_state}
} = Map.fetch!(flags, name)

%{
feature_state
| enabled: enabled,
feature_state_value: value,
variant: variant,
metadata: %{feature_state: feature_state}
}
when enabled === feature_state.enabled and value === feature_state.feature_state_value and
variant === feature_state.variant ->
feature_state

%FlagResult{
enabled: enabled,
value: value,
variant: variant,
metadata: %{feature_state: feature_state}
} ->
%{feature_state | enabled: enabled, feature_state_value: value, variant: variant}
end
reason: reason
Comment thread
khvn26 marked this conversation as resolved.
Resolved
}
end)
end

Expand Down Expand Up @@ -525,7 +521,7 @@
end
rescue
Decimal.Error ->
Logger.warn(

Check warning on line 524 in lib/flagsmith_engine.ex

View workflow job for this annotation

GitHub Actions / Run benchmarks

Logger.warn/1 is deprecated. Use Logger.warning/2 instead

Check warning on line 524 in lib/flagsmith_engine.ex

View workflow job for this annotation

GitHub Actions / Build and test

Logger.warn/1 is deprecated. Use Logger.warning/2 instead

Check warning on line 524 in lib/flagsmith_engine.ex

View workflow job for this annotation

GitHub Actions / Build and test

Logger.warn/1 is deprecated. Use Logger.warning/2 instead
"invalid MODULO segment rule or trait value :: rule: #{inspect(condition_value)} :: value: #{inspect(value)}"
)

Expand Down
2 changes: 2 additions & 0 deletions lib/schemas/environment_feature_state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ defmodule Flagsmith.Schemas.Environment.FeatureState do

# The variant an identity is served, set when evaluating its flags.
field(:variant, :string, virtual: true)
# Why the flag evaluated to its value, set when evaluating flags.
field(:reason, :string, virtual: true)
end

@doc false
Expand Down
4 changes: 3 additions & 1 deletion lib/schemas/features/feature_state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ defmodule Flagsmith.Schemas.Features.FeatureState do
field(:id, :integer)
field(:identity, :integer)
field(:variant, :string)
field(:reason, :string)

embeds_one(:feature, Flagsmith.Schemas.Features.Feature)
embeds_one(:metadata, __MODULE__.Metadata)
Expand All @@ -38,7 +39,8 @@ defmodule Flagsmith.Schemas.Features.FeatureState do
:feature_segment,
:environment,
:enabled,
:variant
:variant,
:reason
],
empty_values: []
)
Expand Down
5 changes: 5 additions & 0 deletions lib/schemas/flag.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule Flagsmith.Schemas.Flag do
field(:feature_name, :string)
field(:value, :string)
field(:variant, :string)
field(:reason, :string)
embeds_one(:experiment, Experiment)
end

Expand All @@ -22,6 +23,7 @@ defmodule Flagsmith.Schemas.Flag do
enabled: enabled,
feature_state_value: value,
variant: variant,
reason: reason,
metadata: metadata,
feature: %Environment.Feature{
name: name,
Expand All @@ -34,6 +36,7 @@ defmodule Flagsmith.Schemas.Flag do
value: value,
feature_id: id,
variant: variant,
reason: reason,
experiment: metadata && metadata.experiment
}
end
Expand All @@ -42,6 +45,7 @@ defmodule Flagsmith.Schemas.Flag do
enabled: enabled,
feature_state_value: value,
variant: variant,
reason: reason,
metadata: metadata,
feature: %Features.Feature{
name: name,
Expand All @@ -54,6 +58,7 @@ defmodule Flagsmith.Schemas.Flag do
value: value,
feature_id: id,
variant: variant,
reason: reason,
experiment: metadata && metadata.experiment
}
end
Expand Down
7 changes: 6 additions & 1 deletion sdk-spec/steps.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ defmodule SDKSpec.Steps do
alias Flagsmith.Schemas

defp as_spec_flag(flag),
do: %{feature: flag.feature_name, enabled: flag.enabled, value: flag.value}
do: %{
feature: flag.feature_name,
enabled: flag.enabled,
value: flag.value,
reason: flag.reason
}

defp as_spec_flags(flags), do: Enum.map(Client.all_flags(flags), &as_spec_flag/1)

Expand Down
24 changes: 16 additions & 8 deletions test/flagsmith_client_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,29 @@ defmodule Flagsmith.Client.Test do
enabled: false,
feature_id: 13535,
feature_name: "body_size",
value: "18px"
value: "18px",
reason: "STATIC"
},
"header_size" => %Schemas.Flag{
enabled: false,
feature_id: 13534,
feature_name: "header_size",
value: "24px"
value: "24px",
reason: "STATIC"
},
"secret_button" => %Schemas.Flag{
enabled: true,
feature_id: 17985,
feature_name: "secret_button",
value: "{\"colour\": \"#ababab\"}"
value: "{\"colour\": \"#ababab\"}",
reason: "STATIC"
},
"test_identity" => %Schemas.Flag{
enabled: true,
feature_id: 18382,
feature_name: "test_identity",
value: "very_yes"
value: "very_yes",
reason: "STATIC"
}
}
}} = Flagsmith.Client.get_environment_flags(config)
Expand Down Expand Up @@ -288,25 +292,29 @@ defmodule Flagsmith.Client.Test do
enabled: false,
feature_id: 13535,
feature_name: "body_size",
value: "18px"
value: "18px",
reason: "STATIC"
},
"header_size" => %Schemas.Flag{
enabled: false,
feature_id: 13534,
feature_name: "header_size",
value: "34px"
value: "34px",
reason: "SPLIT; weight=60.0"
},
"secret_button" => %Schemas.Flag{
enabled: true,
feature_id: 17985,
feature_name: "secret_button",
value: ""
value: "",
reason: "TARGETING_MATCH; segment=test_segment"
},
"test_identity" => %Schemas.Flag{
enabled: true,
feature_id: 18382,
feature_name: "test_identity",
value: "very_no"
value: "very_no",
reason: "TARGETING_MATCH; segment=identity_overrides"
}
}
}
Expand Down
46 changes: 43 additions & 3 deletions test/flagsmith_engine_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ defmodule Flagsmith.EngineTest do
# note for non elixir devs the ^ (pin) operator on the left side of a match (=)
# forces the variable to be exactly the one that was pinned instead of doing
# re-assignement, it would be equivalent to doing var == something
assert ^feature_states = Flagsmith.Engine.get_environment_feature_states(env)
# evaluated without an identity, each comes with the reason it has its value
static_feature_states = Enum.map(feature_states, &%{&1 | reason: "STATIC"})
assert ^static_feature_states = Flagsmith.Engine.get_environment_feature_states(env)
end

test "get_environment_feature_states/1 when project hide_disabled is true", %{env: env} do
Expand All @@ -207,14 +209,16 @@ defmodule Flagsmith.EngineTest do

# replace the feature states with the new one that should have an additional enabled one
new_env = %{env | feature_states: new_feature_states}
expected_first_feature_state = %{new_first_feature_state | reason: "STATIC"}

# we should have 3 enabled, the 2 enabled originally + the last one we enabled
assert [^new_first_feature_state, _, _] =
assert [^expected_first_feature_state, _, _] =
Flagsmith.Engine.get_environment_feature_states(new_env)
end

test "get_environment_feature_state/2", %{env: env} do
[%{feature: %{name: name}} = first_feature_state | _] = env.feature_states
[%{feature: %{name: name}} = feature_state | _] = env.feature_states
first_feature_state = %{feature_state | reason: "STATIC"}

assert ^first_feature_state = Flagsmith.Engine.get_environment_feature_state(env, name)
end
Expand Down Expand Up @@ -249,6 +253,42 @@ defmodule Flagsmith.EngineTest do
] = Flagsmith.Engine.get_identity_feature_states(env, identity, [])
end

test "get_identity_feature_states/3 gives the reason each flag has its value", %{
env: env,
identity: identity
} do
# without the identity's overrides, which match as the `identity_overrides` segment
identity = %{identity | identity_features: []}

reasons = fn env, traits ->
env
|> Flagsmith.Engine.get_identity_feature_states(identity, traits)
|> Map.new(fn %{feature: %{name: name}, reason: reason} -> {name, reason} end)
end

# the identity's `show_popup` trait is false by default, matching `test_segment`
# which overrides `secret_button`, and it falls in the 60% split of `header_size`
assert %{
"header_size" => "SPLIT; weight=60.0",
"body_size" => "STATIC",
"secret_button" => "TARGETING_MATCH; segment=test_segment",
"test_identity" => "STATIC"
} = reasons.(env, [])

# with `show_popup` true and only `test_segment` overriding `secret_button`,
# no segment overriding it matches
segments = Enum.filter(env.project.segments, &(&1.name == "test_segment"))
env = %{env | project: %{env.project | segments: segments}}

assert %{"secret_button" => "DEFAULT"} =
reasons.(env, [
%Traits.Trait{
trait_key: "show_popup",
trait_value: %Traits.Trait.Value{value: true, type: :boolean}
}
])
end

test "get_identity_segments/3", %{env: env, identity: identity} do
# Verify that the hash function is called with the composite key
# and return a hash that will evaluate as 99.9 (to ensure the identity
Expand Down
2 changes: 1 addition & 1 deletion test/support/generators.ex
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ defmodule Flagsmith.Engine.Test.Generators do
end

def json_identity() do
"{\"flags\":[{\"enabled\":false,\"environment\":11278,\"feature\":{\"created_date\":\"2021-10-24T13:40:02.805964Z\",\"default_enabled\":false,\"description\":\"Header Size\",\"id\":13534,\"initial_value\":\"24px\",\"name\":\"header_size\",\"type\":\"MULTIVARIATE\"},\"feature_segment\":null,\"feature_state_value\":\"34px\",\"id\":72267,\"identity\":null},{\"enabled\":false,\"environment\":11278,\"feature\":{\"created_date\":\"2021-10-24T13:41:35.650846Z\",\"default_enabled\":false,\"description\":null,\"id\":13535,\"initial_value\":\"18px\",\"name\":\"body_size\",\"type\":\"STANDARD\"},\"feature_segment\":null,\"feature_state_value\":\"18px\",\"id\":72269,\"identity\":null},{\"enabled\":true,\"environment\":11278,\"feature\":{\"created_date\":\"2022-02-07T19:54:48.630966Z\",\"default_enabled\":false,\"description\":null,\"id\":17985,\"initial_value\":null,\"name\":\"secret_button\",\"type\":\"STANDARD\"},\"feature_segment\":11493,\"feature_state_value\":\"\",\"id\":95631,\"identity\":null},{\"enabled\":true,\"environment\":11278,\"feature\":{\"created_date\":\"2022-02-13T16:14:18.546593Z\",\"default_enabled\":false,\"description\":null,\"id\":18382,\"initial_value\":\"very_yes\",\"name\":\"test_identity\",\"type\":\"STANDARD\"},\"feature_segment\":null,\"feature_state_value\":\"very_no\",\"id\":94429,\"identity\":24842744}],\"traits\":[]}"
"{\"flags\":[{\"enabled\":false,\"environment\":11278,\"feature\":{\"created_date\":\"2021-10-24T13:40:02.805964Z\",\"default_enabled\":false,\"description\":\"Header Size\",\"id\":13534,\"initial_value\":\"24px\",\"name\":\"header_size\",\"type\":\"MULTIVARIATE\"},\"feature_segment\":null,\"feature_state_value\":\"34px\",\"id\":72267,\"reason\":\"SPLIT; weight=60.0\",\"identity\":null},{\"enabled\":false,\"environment\":11278,\"feature\":{\"created_date\":\"2021-10-24T13:41:35.650846Z\",\"default_enabled\":false,\"description\":null,\"id\":13535,\"initial_value\":\"18px\",\"name\":\"body_size\",\"type\":\"STANDARD\"},\"feature_segment\":null,\"feature_state_value\":\"18px\",\"id\":72269,\"reason\":\"STATIC\",\"identity\":null},{\"enabled\":true,\"environment\":11278,\"feature\":{\"created_date\":\"2022-02-07T19:54:48.630966Z\",\"default_enabled\":false,\"description\":null,\"id\":17985,\"initial_value\":null,\"name\":\"secret_button\",\"type\":\"STANDARD\"},\"feature_segment\":11493,\"feature_state_value\":\"\",\"id\":95631,\"reason\":\"TARGETING_MATCH; segment=test_segment\",\"identity\":null},{\"enabled\":true,\"environment\":11278,\"feature\":{\"created_date\":\"2022-02-13T16:14:18.546593Z\",\"default_enabled\":false,\"description\":null,\"id\":18382,\"initial_value\":\"very_yes\",\"name\":\"test_identity\",\"type\":\"STANDARD\"},\"feature_segment\":null,\"feature_state_value\":\"very_no\",\"id\":94429,\"reason\":\"TARGETING_MATCH; segment=identity_overrides\",\"identity\":24842744}],\"traits\":[]}"
end

def map_identity(), do: Jason.decode!(json_identity())
Expand Down
Loading