Skip to content

Commit

Permalink
chore: Optimize my_flags performance (#20166)
Browse files Browse the repository at this point in the history
  • Loading branch information
webjunkie authored Feb 13, 2024
1 parent 7726ed7 commit 66b5f17
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
37 changes: 15 additions & 22 deletions posthog/api/feature_flag.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from typing import Any, Dict, List, Optional, cast
from typing import Any, Dict, Optional, cast
from datetime import datetime

from django.db.models import QuerySet, Q, deletion
Expand Down Expand Up @@ -473,33 +473,26 @@ def my_flags(self, request: request.Request, **kwargs):
if not request.user.is_authenticated: # for mypy
raise exceptions.NotAuthenticated()

feature_flags = (
FeatureFlag.objects.filter(team=self.team, deleted=False)
.prefetch_related("experiment_set")
.prefetch_related("features")
.prefetch_related("analytics_dashboards")
.prefetch_related("surveys_linked_flag")
.select_related("created_by")
.order_by("-created_at")
)
groups = json.loads(request.GET.get("groups", "{}"))
flags: List[dict] = []
feature_flags = list(FeatureFlag.objects.filter(team=self.team, deleted=False).order_by("-created_at"))

feature_flag_list = list(feature_flags)
if not feature_flags:
return Response([])

if not feature_flag_list:
return Response(flags)
groups = json.loads(request.GET.get("groups", "{}"))
matches, *_ = get_all_feature_flags(self.team_id, request.user.distinct_id, groups)

matches, _, _, _ = get_all_feature_flags(self.team_id, request.user.distinct_id, groups)
for feature_flag in feature_flags:
flags.append(
all_serialized_flags = MinimalFeatureFlagSerializer(
feature_flags, many=True, context=self.get_serializer_context()
).data
return Response(
(
{
"feature_flag": FeatureFlagSerializer(feature_flag, context=self.get_serializer_context()).data,
"value": matches.get(feature_flag.key, False),
"feature_flag": feature_flag,
"value": matches.get(feature_flag["key"], False),
}
for feature_flag in all_serialized_flags
)

return Response(flags)
)

@action(methods=["GET"], detail=False, throttle_classes=[FeatureFlagThrottle])
def local_evaluation(self, request: request.Request, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions posthog/api/test/test_feature_flag.py
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ def test_my_flags_is_not_nplus1(self) -> None:
format="json",
).json()

with self.assertNumQueries(FuzzyInt(10, 11)):
with self.assertNumQueries(FuzzyInt(5, 6)):
response = self.client.get(f"/api/projects/{self.team.id}/feature_flags/my_flags")
self.assertEqual(response.status_code, status.HTTP_200_OK)

Expand All @@ -1141,7 +1141,7 @@ def test_my_flags_is_not_nplus1(self) -> None:
format="json",
).json()

with self.assertNumQueries(FuzzyInt(10, 11)):
with self.assertNumQueries(FuzzyInt(5, 6)):
response = self.client.get(f"/api/projects/{self.team.id}/feature_flags/my_flags")
self.assertEqual(response.status_code, status.HTTP_200_OK)

Expand Down

0 comments on commit 66b5f17

Please sign in to comment.