Skip to content

Commit

Permalink
fix(flags): Update feature flag insights when feature flag key changes (
Browse files Browse the repository at this point in the history
#27340)

Co-authored-by: Dylan Martin <[email protected]>
  • Loading branch information
haacked and dmarticus authored Jan 9, 2025
1 parent 2f4fe25 commit a529a8d
Show file tree
Hide file tree
Showing 3 changed files with 522 additions and 5 deletions.
15 changes: 15 additions & 0 deletions posthog/api/feature_flag.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ def update(self, instance: FeatureFlag, validated_data: dict, *args: Any, **kwar
request = self.context["request"]
validated_key = validated_data.get("key", None)
if validated_key:
# Delete any soft deleted feature flags with the same key to prevent conflicts
FeatureFlag.objects.filter(
key=validated_key, team__project_id=instance.team.project_id, deleted=True
).delete()
Expand All @@ -396,6 +397,8 @@ def update(self, instance: FeatureFlag, validated_data: dict, *args: Any, **kwar
for dashboard in analytics_dashboards:
FeatureFlagDashboards.objects.get_or_create(dashboard=dashboard, feature_flag=instance)

old_key = instance.key

instance = super().update(instance, validated_data)

# Propagate the new variants and aggregation group type index to the linked experiments
Expand All @@ -415,6 +418,9 @@ def update(self, instance: FeatureFlag, validated_data: dict, *args: Any, **kwar
experiment.parameters.pop("aggregation_group_type_index", None)
experiment.save()

if old_key != instance.key:
_update_feature_flag_dashboard(instance, old_key)

report_user_action(request.user, "feature flag updated", instance.get_analytics_metadata())

return instance
Expand Down Expand Up @@ -446,6 +452,15 @@ def _create_usage_dashboard(feature_flag: FeatureFlag, user):
return usage_dashboard


def _update_feature_flag_dashboard(feature_flag: FeatureFlag, old_key: str) -> None:
from posthog.helpers.dashboard_templates import update_feature_flag_dashboard

if not old_key:
return

update_feature_flag_dashboard(feature_flag, old_key)


class MinimalFeatureFlagSerializer(serializers.ModelSerializer):
filters = serializers.DictField(source="get_filters", required=False)

Expand Down
Loading

0 comments on commit a529a8d

Please sign in to comment.