-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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(experiments): Create experiment with existing feature flag #28004
Merged
+527
−145
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
0d5382d
First pass at attaching to existing feature flag
danielbachhuber 44411d5
Update test
danielbachhuber 7331356
Update query snapshots
github-actions[bot] 7050098
Merge branch 'master' into experiments/attach-existing-ff
danielbachhuber bce1143
Restore `<LemonBanner>`
danielbachhuber e71389c
Fix merge conflict
danielbachhuber 817e552
UX tweaks
danielbachhuber 5579d81
Send event when used
danielbachhuber 6766efd
Tweaks
danielbachhuber b35101c
Validation
danielbachhuber fc65a5a
Move feature flag validation entirely async
danielbachhuber 2a68769
Merge branch 'master' into experiments/attach-existing-ff
danielbachhuber 216ccb3
Drop stale test
danielbachhuber 915cfbf
Allow experiments to be created with soft-deleted feature flags
danielbachhuber f37aeff
Remnant from 915cfbf22379494f77158c7e65d78ccf645712a0
danielbachhuber 5b148dc
Merge branch 'master' into experiments/attach-existing-ff
danielbachhuber 962bd18
Persist manual validation when the field is blurred
danielbachhuber a302b2a
Expects `undefined` for no error, not empty string
danielbachhuber 7f13464
Add a heading and a link to the banner
danielbachhuber 800b047
Fix type issue
danielbachhuber e433afa
Merge branch 'master' into experiments/attach-existing-ff
danielbachhuber 05b38a4
Remove Holdout field from the experiment form
danielbachhuber 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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -30,6 +30,7 @@ | |||||
from posthog.clickhouse.query_tagging import tag_queries | ||||||
from posthog.constants import INSIGHT_TRENDS | ||||||
from posthog.models.experiment import Experiment, ExperimentHoldout, ExperimentSavedMetric | ||||||
from posthog.models.feature_flag.feature_flag import FeatureFlag | ||||||
from posthog.models.filters.filter import Filter | ||||||
from posthog.utils import generate_cache_key, get_safe_cache | ||||||
|
||||||
|
@@ -252,6 +253,19 @@ def validate_parameters(self, value): | |||||
|
||||||
return value | ||||||
|
||||||
def validate_existing_feature_flag_for_experiment(self, feature_flag: FeatureFlag): | ||||||
if feature_flag.experiment_set.exists(): | ||||||
raise ValidationError("Feature flag is already associated with an experiment.") | ||||||
|
||||||
variants = feature_flag.filters.get("multivariate", {}).get("variants", []) | ||||||
|
||||||
if len(variants) and len(variants) > 1: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: The condition
Suggested change
|
||||||
if variants[0].get("key") != "control": | ||||||
raise ValidationError("Feature flag must have control as the first variant.") | ||||||
return True | ||||||
|
||||||
raise ValidationError("Feature flag is not eligible for experiments.") | ||||||
|
||||||
def create(self, validated_data: dict, *args: Any, **kwargs: Any) -> Experiment: | ||||||
is_draft = "start_date" not in validated_data or validated_data["start_date"] is None | ||||||
|
||||||
|
@@ -271,35 +285,42 @@ def create(self, validated_data: dict, *args: Any, **kwargs: Any) -> Experiment: | |||||
|
||||||
feature_flag_key = validated_data.pop("get_feature_flag_key") | ||||||
|
||||||
holdout_groups = None | ||||||
if validated_data.get("holdout"): | ||||||
holdout_groups = validated_data["holdout"].filters | ||||||
|
||||||
default_variants = [ | ||||||
{"key": "control", "name": "Control Group", "rollout_percentage": 50}, | ||||||
{"key": "test", "name": "Test Variant", "rollout_percentage": 50}, | ||||||
] | ||||||
|
||||||
feature_flag_filters = { | ||||||
"groups": [{"properties": [], "rollout_percentage": 100}], | ||||||
"multivariate": {"variants": variants or default_variants}, | ||||||
"aggregation_group_type_index": aggregation_group_type_index, | ||||||
"holdout_groups": holdout_groups, | ||||||
} | ||||||
existing_feature_flag = FeatureFlag.objects.filter( | ||||||
key=feature_flag_key, team_id=self.context["team_id"], deleted=False | ||||||
).first() | ||||||
if existing_feature_flag: | ||||||
self.validate_existing_feature_flag_for_experiment(existing_feature_flag) | ||||||
feature_flag = existing_feature_flag | ||||||
else: | ||||||
holdout_groups = None | ||||||
if validated_data.get("holdout"): | ||||||
holdout_groups = validated_data["holdout"].filters | ||||||
|
||||||
default_variants = [ | ||||||
{"key": "control", "name": "Control Group", "rollout_percentage": 50}, | ||||||
{"key": "test", "name": "Test Variant", "rollout_percentage": 50}, | ||||||
] | ||||||
|
||||||
feature_flag_filters = { | ||||||
"groups": [{"properties": [], "rollout_percentage": 100}], | ||||||
"multivariate": {"variants": variants or default_variants}, | ||||||
"aggregation_group_type_index": aggregation_group_type_index, | ||||||
"holdout_groups": holdout_groups, | ||||||
} | ||||||
|
||||||
feature_flag_serializer = FeatureFlagSerializer( | ||||||
data={ | ||||||
"key": feature_flag_key, | ||||||
"name": f'Feature Flag for Experiment {validated_data["name"]}', | ||||||
"filters": feature_flag_filters, | ||||||
"active": not is_draft, | ||||||
"creation_context": "experiments", | ||||||
}, | ||||||
context=self.context, | ||||||
) | ||||||
feature_flag_serializer = FeatureFlagSerializer( | ||||||
data={ | ||||||
"key": feature_flag_key, | ||||||
"name": f'Feature Flag for Experiment {validated_data["name"]}', | ||||||
"filters": feature_flag_filters, | ||||||
"active": not is_draft, | ||||||
"creation_context": "experiments", | ||||||
}, | ||||||
context=self.context, | ||||||
) | ||||||
|
||||||
feature_flag_serializer.is_valid(raise_exception=True) | ||||||
feature_flag = feature_flag_serializer.save() | ||||||
feature_flag_serializer.is_valid(raise_exception=True) | ||||||
feature_flag = feature_flag_serializer.save() | ||||||
|
||||||
if not validated_data.get("stats_config"): | ||||||
validated_data["stats_config"] = {"version": 2} | ||||||
|
This file contains 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 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel reasonably confident in this validation logic but would love a second pair of eyes on it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good to me 👍