Skip to content

Commit

Permalink
chore(environments): Update filtering of feature flags/surveys (#26677)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
Twixes and github-actions[bot] authored Dec 9, 2024
1 parent 4736958 commit d779154
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 69 deletions.
6 changes: 3 additions & 3 deletions posthog/api/early_access_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ def early_access_features(request: Request):
)

early_access_features = MinimalEarlyAccessFeatureSerializer(
EarlyAccessFeature.objects.filter(team_id=team.id, stage=EarlyAccessFeature.Stage.BETA).select_related(
"feature_flag"
),
EarlyAccessFeature.objects.filter(
team__project_id=team.project_id, stage=EarlyAccessFeature.Stage.BETA
).select_related("feature_flag"),
many=True,
).data

Expand Down
26 changes: 15 additions & 11 deletions posthog/api/feature_flag.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def create(self, validated_data: dict, *args: Any, **kwargs: Any) -> FeatureFlag

try:
FeatureFlag.objects.filter(
key=validated_data["key"], team_id=self.context["team_id"], deleted=True
key=validated_data["key"], team__project_id=self.context["project_id"], deleted=True
).delete()
except deletion.RestrictedError:
raise exceptions.ValidationError(
Expand Down Expand Up @@ -385,7 +385,9 @@ 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:
FeatureFlag.objects.filter(key=validated_key, team=instance.team, deleted=True).delete()
FeatureFlag.objects.filter(
key=validated_key, team__project_id=instance.team.project_id, deleted=True
).delete()
self._update_filters(validated_data)

analytics_dashboards = validated_data.pop("analytics_dashboards", None)
Expand Down Expand Up @@ -519,11 +521,11 @@ def safely_get_queryset(self, queryset) -> QuerySet:
.prefetch_related("surveys_linked_flag")
)

survey_targeting_flags = Survey.objects.filter(team=self.team, targeting_flag__isnull=False).values_list(
"targeting_flag_id", flat=True
)
survey_targeting_flags = Survey.objects.filter(
team__project_id=self.team.project_id, targeting_flag__isnull=False
).values_list("targeting_flag_id", flat=True)
survey_internal_targeting_flags = Survey.objects.filter(
team=self.team, internal_targeting_flag__isnull=False
team__project_id=self.team.project_id, internal_targeting_flag__isnull=False
).values_list("internal_targeting_flag_id", flat=True)
queryset = queryset.exclude(Q(id__in=survey_targeting_flags)).exclude(
Q(id__in=survey_internal_targeting_flags)
Expand Down Expand Up @@ -650,7 +652,9 @@ def my_flags(self, request: request.Request, **kwargs):
if not request.user.is_authenticated: # for mypy
raise exceptions.NotAuthenticated()

feature_flags = list(FeatureFlag.objects.filter(team=self.team, deleted=False).order_by("-created_at"))
feature_flags = list(
FeatureFlag.objects.filter(team__project_id=self.team.project_id, deleted=False).order_by("-created_at")
)

if not feature_flags:
return Response([])
Expand Down Expand Up @@ -774,9 +778,9 @@ def evaluation_reasons(self, request: request.Request, **kwargs):
"evaluation": reasons[flag_key],
}

disabled_flags = FeatureFlag.objects.filter(team_id=self.team_id, active=False, deleted=False).values_list(
"key", flat=True
)
disabled_flags = FeatureFlag.objects.filter(
team__project_id=self.project_id, active=False, deleted=False
).values_list("key", flat=True)

for flag_key in disabled_flags:
flags_with_evaluation_reasons[flag_key] = {
Expand Down Expand Up @@ -859,7 +863,7 @@ def activity(self, request: request.Request, **kwargs):
page = int(request.query_params.get("page", "1"))

item_id = kwargs["pk"]
if not FeatureFlag.objects.filter(id=item_id, team_id=self.team_id).exists():
if not FeatureFlag.objects.filter(id=item_id, team__project_id=self.project_id).exists():
return Response("", status=status.HTTP_404_NOT_FOUND)

activity_page = load_activity(
Expand Down
14 changes: 6 additions & 8 deletions posthog/api/organization_feature_flag.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.core.exceptions import ObjectDoesNotExist
from rest_framework.response import Response
from posthog.api.utils import action
from rest_framework import (
Expand Down Expand Up @@ -66,7 +65,7 @@ def copy_flags(self, request, *args, **kwargs):

# Fetch the flag to copy
try:
flag_to_copy = FeatureFlag.objects.get(key=feature_flag_key, team_id=from_project)
flag_to_copy = FeatureFlag.objects.get(key=feature_flag_key, team__project_id=from_project)
except FeatureFlag.DoesNotExist:
return Response({"error": "Feature flag to copy does not exist."}, status=status.HTTP_400_BAD_REQUEST)

Expand All @@ -82,9 +81,8 @@ def copy_flags(self, request, *args, **kwargs):

for target_project_id in target_project_ids:
# Target project does not exist
try:
target_project = Team.objects.get(id=target_project_id)
except ObjectDoesNotExist:
target_team = Team.objects.filter(project_id=target_project_id).first()
if target_team is None:
failed_projects.append(
{
"project_id": target_project_id,
Expand Down Expand Up @@ -134,7 +132,7 @@ def copy_flags(self, request, *args, **kwargs):

destination_cohort_serializer = CohortSerializer(
data={
"team": target_project,
"team": target_team,
"name": original_cohort.name,
"groups": [],
"filters": {"properties": prop_group.to_dict()},
Expand All @@ -143,7 +141,7 @@ def copy_flags(self, request, *args, **kwargs):
},
context={
"request": request,
"team_id": target_project.id,
"team_id": target_team.id,
},
)
destination_cohort_serializer.is_valid(raise_exception=True)
Expand Down Expand Up @@ -183,7 +181,7 @@ def copy_flags(self, request, *args, **kwargs):
}

existing_flag = FeatureFlag.objects.filter(
key=feature_flag_key, team_id=target_project_id, deleted=False
key=feature_flag_key, team__project_id=target_project_id, deleted=False
).first()
# Update existing flag
if existing_flag:
Expand Down
16 changes: 8 additions & 8 deletions posthog/api/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def validate(self, data):

if (
self.context["request"].method == "POST"
and Survey.objects.filter(name=data.get("name"), team_id=self.context["team_id"]).exists()
and Survey.objects.filter(name=data.get("name"), team__project_id=self.context["project_id"]).exists()
):
raise serializers.ValidationError("There is already a survey with this name.", code="unique")

Expand All @@ -300,7 +300,7 @@ def validate(self, data):
if (
existing_survey
and existing_survey.name != data.get("name")
and Survey.objects.filter(name=data.get("name"), team_id=self.context["team_id"])
and Survey.objects.filter(name=data.get("name"), team__project_id=self.context["project_id"])
.exclude(id=existing_survey.id)
.exists()
):
Expand Down Expand Up @@ -686,9 +686,9 @@ def destroy(self, request: Request, *args: Any, **kwargs: Any) -> Response:

@action(methods=["GET"], detail=False, required_scopes=["survey:read"])
def responses_count(self, request: request.Request, **kwargs):
earliest_survey_start_date = Survey.objects.filter(team_id=self.team_id).aggregate(Min("start_date"))[
"start_date__min"
]
earliest_survey_start_date = Survey.objects.filter(team__project_id=self.project_id).aggregate(
Min("start_date")
)["start_date__min"]
data = sync_execute(
f"""
SELECT JSONExtractString(properties, '$survey_id') as survey_id, count()
Expand Down Expand Up @@ -721,7 +721,7 @@ def activity(self, request: request.Request, **kwargs):

item_id = kwargs["pk"]

if not Survey.objects.filter(id=item_id, team_id=self.team_id).exists():
if not Survey.objects.filter(id=item_id, team__project_id=self.project_id).exists():
return Response(status=status.HTTP_404_NOT_FOUND)

activity_page = load_activity(
Expand All @@ -742,7 +742,7 @@ def summarize_responses(self, request: request.Request, **kwargs):

survey_id = kwargs["pk"]

if not Survey.objects.filter(id=survey_id, team_id=self.team_id).exists():
if not Survey.objects.filter(id=survey_id, team__project_id=self.project_id).exists():
return Response(status=status.HTTP_404_NOT_FOUND)

survey = self.get_object()
Expand Down Expand Up @@ -893,7 +893,7 @@ def surveys(request: Request):
)

surveys = SurveyAPISerializer(
Survey.objects.filter(team_id=team.id)
Survey.objects.filter(team__project_id=team.project_id)
.exclude(archived=True)
.select_related("linked_flag", "targeting_flag", "internal_targeting_flag")
.prefetch_related("actions"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,10 @@
"posthog_featureflag"."usage_dashboard_id",
"posthog_featureflag"."has_enriched_analytics"
FROM "posthog_earlyaccessfeature"
INNER JOIN "posthog_team" ON ("posthog_earlyaccessfeature"."team_id" = "posthog_team"."id")
LEFT OUTER JOIN "posthog_featureflag" ON ("posthog_earlyaccessfeature"."feature_flag_id" = "posthog_featureflag"."id")
WHERE ("posthog_earlyaccessfeature"."stage" = 'beta'
AND "posthog_earlyaccessfeature"."team_id" = 99999)
AND "posthog_team"."project_id" = 99999)
'''
# ---
# name: TestPreviewList.test_early_access_features.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -865,8 +865,9 @@
"posthog_featureflag"."usage_dashboard_id",
"posthog_featureflag"."has_enriched_analytics"
FROM "posthog_featureflag"
INNER JOIN "posthog_team" ON ("posthog_featureflag"."team_id" = "posthog_team"."id")
WHERE ("posthog_featureflag"."key" = 'copied-flag-key'
AND "posthog_featureflag"."team_id" = 99999)
AND "posthog_team"."project_id" = 99999)
LIMIT 21
'''
# ---
Expand Down Expand Up @@ -1696,8 +1697,9 @@
"posthog_team"."external_data_workspace_id",
"posthog_team"."external_data_workspace_last_synced_at"
FROM "posthog_team"
WHERE "posthog_team"."id" = 99999
LIMIT 21
WHERE "posthog_team"."project_id" = 99999
ORDER BY "posthog_team"."id" ASC
LIMIT 1
'''
# ---
# name: TestOrganizationFeatureFlagCopy.test_copy_feature_flag_create_new.50
Expand Down Expand Up @@ -1926,9 +1928,10 @@
"posthog_featureflag"."usage_dashboard_id",
"posthog_featureflag"."has_enriched_analytics"
FROM "posthog_featureflag"
INNER JOIN "posthog_team" ON ("posthog_featureflag"."team_id" = "posthog_team"."id")
WHERE (NOT "posthog_featureflag"."deleted"
AND "posthog_featureflag"."key" = 'copied-flag-key'
AND "posthog_featureflag"."team_id" = 99999)
AND "posthog_team"."project_id" = 99999)
ORDER BY "posthog_featureflag"."id" ASC
LIMIT 1
'''
Expand Down Expand Up @@ -2280,9 +2283,10 @@
"posthog_featureflag"."usage_dashboard_id",
"posthog_featureflag"."has_enriched_analytics"
FROM "posthog_featureflag"
INNER JOIN "posthog_team" ON ("posthog_featureflag"."team_id" = "posthog_team"."id")
WHERE ("posthog_featureflag"."deleted"
AND "posthog_featureflag"."key" = 'copied-flag-key'
AND "posthog_featureflag"."team_id" = 99999)
AND "posthog_team"."project_id" = 99999)
'''
# ---
# name: TestOrganizationFeatureFlagCopy.test_copy_feature_flag_create_new.9
Expand Down
45 changes: 24 additions & 21 deletions posthog/api/test/__snapshots__/test_survey.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
'''
SELECT COUNT(*) AS "__count"
FROM "posthog_survey"
INNER JOIN "posthog_team" ON ("posthog_survey"."team_id" = "posthog_team"."id")
WHERE (NOT "posthog_survey"."archived"
AND "posthog_survey"."end_date" IS NULL
AND "posthog_survey"."start_date" IS NOT NULL
AND "posthog_survey"."team_id" = 99999
AND "posthog_team"."project_id" = 99999
AND NOT ("posthog_survey"."type" = 'api'))
'''
# ---
Expand Down Expand Up @@ -482,10 +483,11 @@
'''
SELECT COUNT(*) AS "__count"
FROM "posthog_survey"
INNER JOIN "posthog_team" ON ("posthog_survey"."team_id" = "posthog_team"."id")
WHERE (NOT "posthog_survey"."archived"
AND "posthog_survey"."end_date" IS NULL
AND "posthog_survey"."start_date" IS NOT NULL
AND "posthog_survey"."team_id" = 99999
AND "posthog_team"."project_id" = 99999
AND NOT ("posthog_survey"."type" = 'api'))
'''
# ---
Expand Down Expand Up @@ -630,21 +632,6 @@
"posthog_featureflag"."ensure_experience_continuity",
"posthog_featureflag"."usage_dashboard_id",
"posthog_featureflag"."has_enriched_analytics",
T4."id",
T4."key",
T4."name",
T4."filters",
T4."rollout_percentage",
T4."team_id",
T4."created_by_id",
T4."created_at",
T4."deleted",
T4."active",
T4."rollback_conditions",
T4."performed_rollback",
T4."ensure_experience_continuity",
T4."usage_dashboard_id",
T4."has_enriched_analytics",
T5."id",
T5."key",
T5."name",
Expand All @@ -659,12 +646,28 @@
T5."performed_rollback",
T5."ensure_experience_continuity",
T5."usage_dashboard_id",
T5."has_enriched_analytics"
T5."has_enriched_analytics",
T6."id",
T6."key",
T6."name",
T6."filters",
T6."rollout_percentage",
T6."team_id",
T6."created_by_id",
T6."created_at",
T6."deleted",
T6."active",
T6."rollback_conditions",
T6."performed_rollback",
T6."ensure_experience_continuity",
T6."usage_dashboard_id",
T6."has_enriched_analytics"
FROM "posthog_survey"
INNER JOIN "posthog_team" ON ("posthog_survey"."team_id" = "posthog_team"."id")
LEFT OUTER JOIN "posthog_featureflag" ON ("posthog_survey"."linked_flag_id" = "posthog_featureflag"."id")
LEFT OUTER JOIN "posthog_featureflag" T4 ON ("posthog_survey"."targeting_flag_id" = T4."id")
LEFT OUTER JOIN "posthog_featureflag" T5 ON ("posthog_survey"."internal_targeting_flag_id" = T5."id")
WHERE ("posthog_survey"."team_id" = 99999
LEFT OUTER JOIN "posthog_featureflag" T5 ON ("posthog_survey"."targeting_flag_id" = T5."id")
LEFT OUTER JOIN "posthog_featureflag" T6 ON ("posthog_survey"."internal_targeting_flag_id" = T6."id")
WHERE ("posthog_team"."project_id" = 99999
AND NOT ("posthog_survey"."archived"))
'''
# ---
Expand Down
Loading

0 comments on commit d779154

Please sign in to comment.