:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(experiments): Allow ordering experiments list by conclusion (#84039)
Juraj Majerikc48be54b42b6
diff --git a/products/experiments/backend/experiment_service.py b/products/experiments/backend/experiment_service.pyindex 14d56b81eca1..2204bd3f2fc7 100644--- a/products/experiments/backend/experiment_service.py+++ b/products/experiments/backend/experiment_service.py@@ -865,6 +865,8 @@ class ExperimentService:"-duration","status","-status",+ "conclusion",+ "-conclusion",}@classmethod@@ -4465,6 +4467,20 @@ class ExperimentService:output_field=CharField(),)).order_by(f"{prefix}created_by_display")+ elif order_value in ["conclusion", "-conclusion"]:+ # Match the frontend column's rank order: won → lost → inconclusive →+ # stopped_early → invalid, experiments without a conclusion last.+ prefix = "-" if order_value.startswith("-") else ""+ queryset = queryset.annotate(+ conclusion_sort_key=Case(+ When(conclusion="won", then=Value(1)),+ When(conclusion="lost", then=Value(2)),+ When(conclusion="inconclusive", then=Value(3)),+ When(conclusion="stopped_early", then=Value(4)),+ When(conclusion="invalid", then=Value(5)),+ default=Value(6),+ )+ ).order_by(f"{prefix}conclusion_sort_key")else:queryset = queryset.order_by(order_value)else:diff --git a/products/experiments/backend/test/test_experiment_service.py b/products/experiments/backend/test/test_experiment_service.pyindex d1166fb48361..8e022e59d313 100644--- a/products/experiments/backend/test/test_experiment_service.py+++ b/products/experiments/backend/test/test_experiment_service.py@@ -5293,6 +5293,30 @@ class TestExperimentService(APIBaseTest):assert list(queryset.values_list("name", flat=True)[:3]) == expected_order+ @parameterized.expand(+ [+ ("ascending", "conclusion", ["Won", "Lost", "No conclusion"]),+ ("descending", "-conclusion", ["No conclusion", "Lost", "Won"]),+ ]+ )+ def test_filter_experiments_queryset_orders_by_conclusion(+ self, _: str, order: str, expected_order: list[str]+ ) -> None:+ service = self._service()+ service.create_experiment(name="Won", feature_flag_key="order-conclusion-won")+ service.create_experiment(name="Lost", feature_flag_key="order-conclusion-lost")+ service.create_experiment(name="No conclusion", feature_flag_key="order-conclusion-none")+ Experiment.objects.filter(team=self.team, name="Won").update(conclusion="won")+ Experiment.objects.filter(team=self.team, name="Lost").update(conclusion="lost")++ queryset = service.filter_experiments_queryset(+ Experiment.objects.filter(team=self.team),+ action="list",+ query_params={"order": order},+ )++ assert list(queryset.values_list("name", flat=True)[:3]) == expected_order+def test_filter_experiments_queryset_validates_feature_flag_id(self) -> None:with self.assertRaises(ValidationError) as ctx:self._service().filter_experiments_queryset(