: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
refactor(customer-analytics): move GroupUsageMetricViewSet into customer_analytics (#128914)
Thomas Obermüller88e69166aceb
diff --git a/products/customer_analytics/backend/presentation/views/usage_metrics.py b/products/customer_analytics/backend/presentation/views/usage_metrics.pynew file mode 100644index 000000000000..ef440973c847--- /dev/null+++ b/products/customer_analytics/backend/presentation/views/usage_metrics.py@@ -0,0 +1,149 @@+from typing import Any, cast++from rest_framework import serializers, viewsets++from posthog.api.routing import TeamAndOrgViewSetMixin+from posthog.models import GroupUsageMetric++from products.access_control.backend.presentation.access_control import UserAccessControlSerializerMixin+from products.warehouse_sources.backend.facade.models import DataWarehouseTable++_DW_FILTER_REQUIRED_FIELDS = ("table_name", "timestamp_field", "key_field")+++class GroupUsageMetricSerializer(serializers.ModelSerializer, UserAccessControlSerializerMixin):+ name = serializers.CharField(+ max_length=255,+ help_text="Name of the usage metric. Must be unique per group type within the project.",+ )+ format = serializers.ChoiceField(+ choices=GroupUsageMetric.Format.choices,+ default=GroupUsageMetric.Format.NUMERIC,+ help_text="How the metric value is formatted in the UI. One of `numeric` or `currency`.",+ )+ interval = serializers.IntegerField(+ default=7,+ help_text="Rolling time window in days used to compute the metric. Defaults to 7.",+ )+ display = serializers.ChoiceField(+ choices=GroupUsageMetric.Display.choices,+ default=GroupUsageMetric.Display.NUMBER,+ help_text="Visual representation in the UI. One of `number` or `sparkline`.",+ )+ filters = serializers.DictField(+ help_text=(+ "Filter definition for the metric. Two shapes are accepted, discriminated by an optional "+ "`source` key.\n\n"+ '**Events** (default, when `source` is missing or `"events"`): HogFunction filter shape — '+ "`events: [...]`, optional `actions: [...]`, `properties: [...]`, `filter_test_accounts: bool`.\n\n"+ '**Data warehouse** (`source: "data_warehouse"`): `table_name` (synced DW table), '+ "`timestamp_field` (timestamp column or HogQL expression), `key_field` (column whose value "+ "matches the entity key). Currently DW metrics only render on group profiles — person profiles "+ "are not yet supported."+ ),+ )+ math = serializers.ChoiceField(+ choices=GroupUsageMetric.Math.choices,+ default=GroupUsageMetric.Math.COUNT,+ help_text=(+ "Aggregation function. `count` counts matching events; `sum` sums the value of `math_property` "+ "on matching events."+ ),+ )+ math_property = serializers.CharField(+ max_length=255,+ required=False,+ allow_null=True,+ allow_blank=True,+ help_text=(+ "Required when `math` is `sum`; must be empty when `math` is `count`. For events metrics this "+ "is an event property name. For data warehouse metrics this is the column name (or HogQL "+ "expression) to sum on the DW table."+ ),+ )++ class Meta:+ model = GroupUsageMetric+ fields = ("id", "name", "format", "interval", "display", "filters", "math", "math_property")++ def validate(self, data: dict[str, Any]) -> dict[str, Any]:+ data = super().validate(data)++ math = data.get("math", self.instance.math if self.instance else GroupUsageMetric.Math.COUNT)+ math_property = data.get("math_property", self.instance.math_property if self.instance else None)+ filters = data.get("filters", self.instance.filters if self.instance else None)++ source = (filters or {}).get("source") if isinstance(filters, dict) else None++ if source == GroupUsageMetric.Source.DATA_WAREHOUSE:+ self._validate_data_warehouse(cast(dict, filters), math, math_property)+ elif source in (None, GroupUsageMetric.Source.EVENTS):+ self._validate_events(math, math_property)+ else:+ raise serializers.ValidationError({"filters": f"Unknown source: {source!r}"})++ if "name" in data:+ self._validate_unique_name(data["name"])+ if source != GroupUsageMetric.Source.DATA_WAREHOUSE and "filters" in data:+ self._validate_cohort_filters(cast(dict, filters))++ return data++ def _validate_unique_name(self, name: str) -> None:+ group_type_index = self.instance.group_type_index if self.instance else self.context["group_type_index"]+ existing = GroupUsageMetric.objects.filter(+ team=self.context["get_team"](), group_type_index=group_type_index, name=name+ )+ if self.instance:+ existing = existing.exclude(pk=self.instance.pk)+ if existing.exists():+ raise serializers.ValidationError({"name": f"A usage metric named {name!r} already exists."})++ def _validate_cohort_filters(self, filters: dict) -> None:+ metric = GroupUsageMetric(team=self.context["get_team"](), filters=filters)+ reasons = metric.get_cohort_filter_errors()+ if reasons:+ raise serializers.ValidationError(+ {+ "filters": (+ f"Your internal and test account filters use cohorts that usage metrics can't use: "+ f"{'; '.join(reasons)}. Use a cohort with only person property filters, "+ f"or turn off the internal and test account filter for this metric."+ )+ }+ )++ def _validate_events(self, math: str, math_property: str | None) -> None:+ if math == GroupUsageMetric.Math.SUM and not math_property:+ raise serializers.ValidationError({"math_property": "math_property is required when math is 'sum'."})+ if math == GroupUsageMetric.Math.COUNT and math_property:+ raise serializers.ValidationError({"math_property": "math_property must be empty when math is 'count'."})++ def _validate_data_warehouse(self, filters: dict, math: str, math_property: str | None) -> None:+ missing = [field for field in _DW_FILTER_REQUIRED_FIELDS if not filters.get(field)]+ if missing:+ raise serializers.ValidationError(+ {"filters": f"Data warehouse metrics require {', '.join(_DW_FILTER_REQUIRED_FIELDS)}."}+ )++ if math == GroupUsageMetric.Math.SUM and not math_property:+ raise serializers.ValidationError(+ {"math_property": "math_property (column to sum) is required when math is 'sum'."}+ )+ if math == GroupUsageMetric.Math.COUNT and math_property:+ raise serializers.ValidationError({"math_property": "math_property must be empty when math is 'count'."})++ team = self.context["get_team"]()+ if not DataWarehouseTable.objects.filter(team=team, name=filters["table_name"]).exclude(deleted=True).exists():+ raise serializers.ValidationError(+ {"filters": f"Data warehouse table {filters['table_name']!r} does not exist."}+ )+++class GroupUsageMetricViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):+ scope_object = "usage_metric"+ queryset = GroupUsageMetric.objects.all()+ serializer_class = GroupUsageMetricSerializer++ def perform_create(self, serializer: serializers.BaseSerializer) -> None:+ serializer.save(team=self.team, group_type_index=self.parents_query_dict["group_type_index"])diff --git a/products/customer_analytics/backend/routes.py b/products/customer_analytics/backend/routes.pyindex 67342d04de31..bdf6df5c505a 100644--- a/products/customer_analytics/backend/routes.py+++ b/products/customer_analytics/backend/routes.py@@ -1,4 +1,5 @@from posthog.api.routing import RouterRegistry+from posthog.settings import EE_AVAILABLEfrom products.customer_analytics.backend.presentation.views import urls as external_api_urlsfrom products.customer_analytics.backend.presentation.views.account_persons_query import AccountPersonsViewSet@@ -8,6 +9,7 @@ from products.customer_analytics.backend.presentation.views.customer_tasks imporfrom products.customer_analytics.backend.presentation.views.organization_members import (OrganizationMembersForAccountViewSet,)+from products.customer_analytics.backend.presentation.views.usage_metrics import GroupUsageMetricViewSetfrom products.customer_analytics.backend.presentation.views.views import (AccountNotebookViewSet,AccountNotesViewSet,@@ -153,3 +155,13 @@ def register_routes(routers: RouterRegistry) -> None:"project_workflow_customer_tasks",["team_id"],)+ # Usage metrics keep the URL they had under the groups API, so they stay EE-only like it.+ # Products cannot nest onto another product's router, so the group type segment is spelled+ # out here with the `parent_lookup_` name the nested router would have given it.+ if EE_AVAILABLE:+ routers.projects.register(+ r"groups_types/(?P<parent_lookup_group_type_index>[^/.]+)/metrics",+ GroupUsageMetricViewSet,+ "project_groups_metrics",+ ["project_id"],+ )diff --git a/products/customer_analytics/backend/test/test_usage_metrics.py b/products/customer_analytics/backend/test/test_usage_metrics.pynew file mode 100644index 000000000000..1156645df86b--- /dev/null+++ b/products/customer_analytics/backend/test/test_usage_metrics.py@@ -0,0 +1,392 @@+from posthog.test.base import APIBaseTest++from parameterized import parameterized+from rest_framework import status++from posthog.constants import AvailableFeature+from posthog.models import GroupUsageMetric+from posthog.models.organization import Organization, OrganizationMembership+from posthog.models.team.team import Team+from posthog.test.persons import create_group_type_mapping++from products.access_control.backend.models.access_control import AccessControl+from products.warehouse_sources.backend.facade.models import DataWarehouseCredential, DataWarehouseTable+++class GroupUsageMetricViewSetTestCase(APIBaseTest):+ def setUp(self):+ super().setUp()+ self.group_type = create_group_type_mapping(+ team=self.team, project=self.project, group_type="organization", group_type_index=0+ )+ self.url = f"/api/projects/{self.team.id}/groups_types/{str(self.group_type.group_type_index)}/metrics"++ self.other_org = Organization.objects.create(name="other org")+ self.other_team = Team.objects.create(organization=self.other_org, name="other team")+ self.other_group_type = create_group_type_mapping(+ team=self.other_team, project_id=self.other_team.project_id, group_type="company", group_type_index=0+ )+ self.other_url = (+ f"/api/projects/{self.other_team.id}/groups_types/{str(self.other_group_type.group_type_index)}/metrics"+ )++ def assertFields(self, data, metric):+ self.assertEqual(data["id"], str(metric.id))+ self.assertEqual(data["name"], metric.name)+ self.assertEqual(data["format"], metric.format)+ self.assertEqual(data["interval"], metric.interval)+ self.assertEqual(data["display"], metric.display)+ self.assertEqual(data["filters"], metric.filters)++ def _create_metric(self, **kwargs):+ defaults = {+ "team": self.team,+ "group_type_index": self.group_type.group_type_index,+ "name": "Events",+ "filters": {"foo": "bar"},+ }+ defaults.update(kwargs)+ return GroupUsageMetric.objects.create(**defaults)++ def test_list(self):+ metric = self._create_metric()++ response = self.client.get(self.url)++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertFields(response.json()["results"][0], metric)++ def test_create(self):+ payload = {"name": "Events", "filters": {"foo": "bar"}}++ response = self.client.post(self.url, payload)++ self.assertEqual(response.status_code, status.HTTP_201_CREATED)+ metric = GroupUsageMetric.objects.get(id=response.json().get("id"))+ self.assertFields(response.json(), metric)+ self.assertEqual(metric.team, self.team, "Should set team automatically")+ self.assertEqual(+ metric.group_type_index, self.group_type.group_type_index, "Should set group_type_index automatically"+ )+ self.assertIsNotNone(metric.bytecode, "Should set bytecode automatically")++ def test_retrieve(self):+ metric = self._create_metric()+ url = f"{self.url}/{metric.id}"++ response = self.client.get(url)++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertFields(response.json(), metric)++ def test_update(self):+ metric = self._create_metric()+ url = f"{self.url}/{metric.id}"+ payload = {+ "name": "Updated Events",+ "format": "currency",+ "interval": 30,+ "display": "sparkline",+ "filters": {"updated": "value"},+ }++ response = self.client.put(url, payload)++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ metric.refresh_from_db()+ self.assertEqual(metric.name, "Updated Events")+ self.assertEqual(metric.format, "currency")+ self.assertEqual(metric.interval, 30)+ self.assertEqual(metric.display, "sparkline")+ self.assertEqual(metric.filters, {"updated": "value"})+ self.assertFields(response.json(), metric)++ def test_delete(self):+ metric = self._create_metric()+ url = f"{self.url}/{metric.id}"++ response = self.client.delete(url)++ self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)+ self.assertFalse(GroupUsageMetric.objects.filter(id=metric.id).exists())++ def test_partial_update(self):+ metric = self._create_metric()+ url = f"{self.url}/{metric.id}"+ payload = {"name": "Partially Updated Events"}++ response = self.client.patch(url, payload)++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ metric.refresh_from_db()+ self.assertEqual(metric.name, "Partially Updated Events")+ self.assertEqual(metric.format, "numeric", "Should remain unchanged")+ self.assertEqual(metric.interval, 7, "Should remain unchanged")+ self.assertEqual(metric.display, "number", "Should remain unchanged")++ def test_delete_nonexistent(self):+ fake_id = "00000000-0000-0000-0000-000000000000"+ url = f"{self.url}/{fake_id}"++ response = self.client.delete(url)++ self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)++ def test_update_nonexistent(self):+ fake_id = "00000000-0000-0000-0000-000000000000"+ url = f"{self.url}/{fake_id}"+ payload = {"name": "Updated Events"}++ response = self.client.put(url, payload)++ self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)++ def test_unauthenticated_access(self):+ self.client.logout()++ response = self.client.get(self.url)++ self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)++ def test_unauthorized_team_access(self):+ self._create_metric(group_type_index=self.other_group_type.group_type_index, team=self.other_team)++ response = self.client.get(self.other_url)++ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)+ self.assertEqual(response.json(), self.permission_denied_response("You don't have access to the project."))++ def test_unauthorized_metric_access(self):+ other_metric = self._create_metric(+ group_type_index=self.other_group_type.group_type_index, team=self.other_team+ )+ url = f"{self.other_url}/{other_metric.id}"++ response = self.client.get(url)++ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)+ self.assertEqual(response.json(), self.permission_denied_response("You don't have access to the project."))++ def test_unauthorized_metric_creation(self):+ payload = {"name": "Unauthorized Events"}++ response = self.client.post(self.other_url, payload)++ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)+ self.assertEqual(response.json(), self.permission_denied_response("You don't have access to the project."))++ def test_unauthorized_metric_modification(self):+ other_metric = self._create_metric(+ group_type_index=self.other_group_type.group_type_index, team=self.other_team+ )+ url = f"{self.other_url}/{other_metric.id}"+ payload = {"name": "Hacked Events"}++ response = self.client.put(url, payload)++ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)+ self.assertEqual(response.json(), self.permission_denied_response("You don't have access to the project."))++ def test_unauthorized_metric_deletion(self):+ other_metric = self._create_metric(+ group_type_index=self.other_group_type.group_type_index, team=self.other_team+ )+ url = f"{self.other_url}/{other_metric.id}"++ response = self.client.delete(url)++ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)+ self.assertEqual(response.json(), self.permission_denied_response("You don't have access to the project."))++ self.assertTrue(GroupUsageMetric.objects.filter(id=other_metric.id).exists())++ def test_non_admin_cannot_update_metric(self):+ metric = self._create_metric()+ url = f"{self.url}/{metric.id}"+ self.organization_membership.level = OrganizationMembership.Level.MEMBER+ self.organization_membership.save()+ self.organization.available_product_features = [+ {"key": AvailableFeature.ACCESS_CONTROL, "name": AvailableFeature.ACCESS_CONTROL},+ ]+ self.organization.save()+ # Grant member-level (not admin) project access so the request reaches the serializer+ AccessControl.objects.create(+ team=self.team,+ resource="project",+ resource_id=str(self.team.id),+ access_level="member",+ )++ response = self.client.patch(url, {"name": "Should not update"})++ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ metric.refresh_from_db()+ self.assertEqual(metric.name, "Events")++ def test_admin_can_update_metric(self):+ metric = self._create_metric()+ url = f"{self.url}/{metric.id}"+ self.organization_membership.level = OrganizationMembership.Level.ADMIN+ self.organization_membership.save()++ response = self.client.patch(url, {"name": "Updated by admin"})++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ metric.refresh_from_db()+ self.assertEqual(metric.name, "Updated by admin")++ def _create_dw_table(self, name: str = "stripe_charges"):+ credential = DataWarehouseCredential.objects.create(team=self.team, access_key="key", access_secret="secret")+ return DataWarehouseTable.objects.create(+ team=self.team,+ name=name,+ credential=credential,+ url_pattern="http://example.com/{name}",+ columns={+ "customer_id": {"clickhouse": "String", "hogql": "StringDatabaseField", "valid": True},+ "created": {"clickhouse": "DateTime64(3, 'UTC')", "hogql": "DateTimeDatabaseField", "valid": True},+ "amount": {"clickhouse": "Float64", "hogql": "FloatDatabaseField", "valid": True},+ },+ )++ def test_create_data_warehouse_metric(self):+ self._create_dw_table()+ payload = {+ "name": "DW signups",+ "math": "count",+ "filters": {+ "source": "data_warehouse",+ "table_name": "stripe_charges",+ "timestamp_field": "created",+ "key_field": "customer_id",+ },+ }++ response = self.client.post(self.url, payload, format="json")++ self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.json())+ metric = GroupUsageMetric.objects.get(id=response.json()["id"])+ self.assertEqual(metric.filters["source"], "data_warehouse")+ self.assertEqual(metric.filters["table_name"], "stripe_charges")++ def test_create_data_warehouse_metric_rejects_missing_fields(self):+ self._create_dw_table()+ payload = {+ "name": "DW signups",+ "filters": {"source": "data_warehouse", "table_name": "stripe_charges"},+ }++ response = self.client.post(self.url, payload, format="json")++ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ self.assertEqual(response.json()["attr"], "filters")++ def test_create_data_warehouse_metric_rejects_unknown_table(self):+ payload = {+ "name": "DW signups",+ "filters": {+ "source": "data_warehouse",+ "table_name": "no_such_table",+ "timestamp_field": "created",+ "key_field": "customer_id",+ },+ }++ response = self.client.post(self.url, payload, format="json")++ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ self.assertEqual(response.json()["attr"], "filters")++ @parameterized.expand(+ [+ ("duplicate_name_on_create", "post", {"name": "Events", "filters": {}}, 400, "name"),+ ("duplicate_name_on_rename", "patch", {"name": "Events"}, 400, "name"),+ ("own_name_on_update", "patch", {"name": "Signups"}, 200, None),+ (+ "test_account_cohort_filter",+ "post",+ {"name": "Pageviews", "filters": {"filter_test_accounts": True}},+ 400,+ "filters",+ ),+ ]+ )+ def test_save_rejects_invalid_input_with_400(self, _name, method, payload, expected_status, expected_attr):+ self._create_metric(name="Events")+ signups = self._create_metric(name="Signups")+ self.team.test_account_filters = [{"type": "cohort", "key": "id", "value": 999999}]+ self.team.save()+ url = self.url if method == "post" else f"{self.url}/{signups.id}"++ response = getattr(self.client, method)(url, payload, format="json")++ self.assertEqual(response.status_code, expected_status, response.json())+ self.assertEqual(response.json().get("attr"), expected_attr)++ def test_create_data_warehouse_metric_accepts_table_with_null_deleted(self):+ table = self._create_dw_table()+ table.deleted = None+ table.save()++ payload = {+ "name": "DW signups",+ "filters": {+ "source": "data_warehouse",+ "table_name": "stripe_charges",+ "timestamp_field": "created",+ "key_field": "customer_id",+ },+ }++ response = self.client.post(self.url, payload, format="json")++ self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.json())++ def test_create_data_warehouse_metric_rejects_soft_deleted_table(self):+ table = self._create_dw_table()+ table.deleted = True+ table.save()++ payload = {+ "name": "DW signups",+ "filters": {+ "source": "data_warehouse",+ "table_name": "stripe_charges",+ "timestamp_field": "created",+ "key_field": "customer_id",+ },+ }++ response = self.client.post(self.url, payload, format="json")++ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ self.assertEqual(response.json()["attr"], "filters")++ def test_create_data_warehouse_sum_requires_math_property(self):+ self._create_dw_table()+ payload = {+ "name": "DW revenue",+ "math": "sum",+ "filters": {+ "source": "data_warehouse",+ "table_name": "stripe_charges",+ "timestamp_field": "created",+ "key_field": "customer_id",+ },+ }++ response = self.client.post(self.url, payload, format="json")++ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ self.assertEqual(response.json()["attr"], "math_property")++ def test_create_metric_rejects_unknown_source(self):+ payload = {+ "name": "Bogus",+ "filters": {"source": "something_else"},+ }++ response = self.client.post(self.url, payload, format="json")++ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ self.assertEqual(response.json()["attr"], "filters")diff --git a/products/groups/backend/presentation/views.py b/products/groups/backend/presentation/views.pyindex 440f98d8d7dd..a9b6e6de8668 100644--- a/products/groups/backend/presentation/views.py+++ b/products/groups/backend/presentation/views.py@@ -30,7 +30,6 @@ from posthog.clickhouse.query_tagging import Feature, tag_queriesfrom posthog.constants import GroupTypeIndexfrom posthog.helpers.dashboard_templates import create_group_type_mapping_detail_dashboardfrom posthog.helpers.impersonation import is_impersonated-from posthog.models import GroupUsageMetricfrom posthog.models.activity_logging.activity_log import Change, Detail, load_activity, log_activityfrom posthog.models.activity_logging.activity_page import activity_page_response, parse_activity_page_paramsfrom posthog.models.group import Group@@ -70,7 +69,6 @@ from products.notebooks.backend.facade.content import (create_paragraph_with_text,create_text_content,)-from products.warehouse_sources.backend.facade.models import DataWarehouseTablelogger = structlog.get_logger(__name__)tracer = trace.get_tracer(__name__)@@ -961,145 +959,3 @@ class GroupsViewSet(TeamAndOrgViewSetMixin, mixins.ListModelMixin, mixins.Createconvert_notebook_content_to_markdown({"type": "doc", "content": template_nodes}))notebooks.create_group_notebook(self.team.id, group.id, title=notebook_title, content=notebook_content)---_DW_FILTER_REQUIRED_FIELDS = ("table_name", "timestamp_field", "key_field")---class GroupUsageMetricSerializer(serializers.ModelSerializer, UserAccessControlSerializerMixin):- name = serializers.CharField(- max_length=255,- help_text="Name of the usage metric. Must be unique per group type within the project.",- )- format = serializers.ChoiceField(- choices=GroupUsageMetric.Format.choices,- default=GroupUsageMetric.Format.NUMERIC,- help_text="How the metric value is formatted in the UI. One of `numeric` or `currency`.",- )- interval = serializers.IntegerField(- default=7,- help_text="Rolling time window in days used to compute the metric. Defaults to 7.",- )- display = serializers.ChoiceField(- choices=GroupUsageMetric.Display.choices,- default=GroupUsageMetric.Display.NUMBER,- help_text="Visual representation in the UI. One of `number` or `sparkline`.",- )- filters = serializers.DictField(- help_text=(- "Filter definition for the metric. Two shapes are accepted, discriminated by an optional "- "`source` key.\n\n"- '**Events** (default, when `source` is missing or `"events"`): HogFunction filter shape — '- "`events: [...]`, optional `actions: [...]`, `properties: [...]`, `filter_test_accounts: bool`.\n\n"- '**Data warehouse** (`source: "data_warehouse"`): `table_name` (synced DW table), '- "`timestamp_field` (timestamp column or HogQL expression), `key_field` (column whose value "- "matches the entity key). Currently DW metrics only render on group profiles — person profiles "- "are not yet supported."- ),- )- math = serializers.ChoiceField(- choices=GroupUsageMetric.Math.choices,- default=GroupUsageMetric.Math.COUNT,- help_text=(- "Aggregation function. `count` counts matching events; `sum` sums the value of `math_property` "- "on matching events."- ),- )- math_property = serializers.CharField(- max_length=255,- required=False,- allow_null=True,- allow_blank=True,- help_text=(- "Required when `math` is `sum`; must be empty when `math` is `count`. For events metrics this "- "is an event property name. For data warehouse metrics this is the column name (or HogQL "- "expression) to sum on the DW table."- ),- )-- class Meta:- model = GroupUsageMetric- fields = ("id", "name", "format", "interval", "display", "filters", "math", "math_property")-- def validate(self, data: dict[str, Any]) -> dict[str, Any]:- data = super().validate(data)-- math = data.get("math", self.instance.math if self.instance else GroupUsageMetric.Math.COUNT)- math_property = data.get("math_property", self.instance.math_property if self.instance else None)- filters = data.get("filters", self.instance.filters if self.instance else None)-- source = (filters or {}).get("source") if isinstance(filters, dict) else None-- if source == GroupUsageMetric.Source.DATA_WAREHOUSE:- self._validate_data_warehouse(cast(dict, filters), math, math_property)- elif source in (None, GroupUsageMetric.Source.EVENTS):- self._validate_events(math, math_property)- else:- raise serializers.ValidationError({"filters": f"Unknown source: {source!r}"})-- if "name" in data:- self._validate_unique_name(data["name"])- if source != GroupUsageMetric.Source.DATA_WAREHOUSE and "filters" in data:- self._validate_cohort_filters(cast(dict, filters))-- return data-- def _validate_unique_name(self, name: str) -> None:- group_type_index = self.instance.group_type_index if self.instance else self.context["group_type_index"]- existing = GroupUsageMetric.objects.filter(- team=self.context["get_team"](), group_type_index=group_type_index, name=name- )- if self.instance:- existing = existing.exclude(pk=self.instance.pk)- if existing.exists():- raise serializers.ValidationError({"name": f"A usage metric named {name!r} already exists."})-- def _validate_cohort_filters(self, filters: dict) -> None:- metric = GroupUsageMetric(team=self.context["get_team"](), filters=filters)- reasons = metric.get_cohort_filter_errors()- if reasons:- raise serializers.ValidationError(- {- "filters": (- f"Your internal and test account filters use cohorts that usage metrics can't use: "- f"{'; '.join(reasons)}. Use a cohort with only person property filters, "- f"or turn off the internal and test account filter for this metric."- )- }- )-- def _validate_events(self, math: str, math_property: str | None) -> None:- if math == GroupUsageMetric.Math.SUM and not math_property:- raise serializers.ValidationError({"math_property": "math_property is required when math is 'sum'."})- if math == GroupUsageMetric.Math.COUNT and math_property:- raise serializers.ValidationError({"math_property": "math_property must be empty when math is 'count'."})-- def _validate_data_warehouse(self, filters: dict, math: str, math_property: str | None) -> None:- missing = [field for field in _DW_FILTER_REQUIRED_FIELDS if not filters.get(field)]- if missing:- raise serializers.ValidationError(- {"filters": f"Data warehouse metrics require {', '.join(_DW_FILTER_REQUIRED_FIELDS)}."}- )-- if math == GroupUsageMetric.Math.SUM and not math_property:- raise serializers.ValidationError(- {"math_property": "math_property (column to sum) is required when math is 'sum'."}- )- if math == GroupUsageMetric.Math.COUNT and math_property:- raise serializers.ValidationError({"math_property": "math_property must be empty when math is 'count'."})-- team = self.context["get_team"]()- if not DataWarehouseTable.objects.filter(team=team, name=filters["table_name"]).exclude(deleted=True).exists():- raise serializers.ValidationError(- {"filters": f"Data warehouse table {filters['table_name']!r} does not exist."}- )---@extend_schema(extensions={"x-product": "customer_analytics"})-class GroupUsageMetricViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):- scope_object = "usage_metric"- queryset = GroupUsageMetric.objects.all()- serializer_class = GroupUsageMetricSerializer-- def perform_create(self, serializer: serializers.BaseSerializer) -> None:- serializer.save(team=self.team, group_type_index=self.parents_query_dict["group_type_index"])diff --git a/products/groups/backend/routes.py b/products/groups/backend/routes.pyindex 093578235438..47c1bb1d1b69 100644--- a/products/groups/backend/routes.py+++ b/products/groups/backend/routes.py@@ -1,7 +1,7 @@from posthog.api.routing import RouterRegistryfrom posthog.settings import EE_AVAILABLE-from products.groups.backend.presentation.views import GroupsTypesViewSet, GroupsViewSet, GroupUsageMetricViewSet+from products.groups.backend.presentation.views import GroupsTypesViewSet, GroupsViewSetdef register_routes(routers: RouterRegistry) -> None:@@ -10,9 +10,4 @@ def register_routes(routers: RouterRegistry) -> None:returnrouters.projects.register(r"groups", GroupsViewSet, "project_groups", ["team_id"])- group_types_router = routers.projects.register(- r"groups_types", GroupsTypesViewSet, "project_groups_types", ["project_id"]- )- group_types_router.register(- r"metrics", GroupUsageMetricViewSet, "project_groups_metrics", ["project_id", "group_type_index"]- )+ routers.projects.register(r"groups_types", GroupsTypesViewSet, "project_groups_types", ["project_id"])diff --git a/products/groups/backend/tests/test_clickhouse_groups.py b/products/groups/backend/tests/test_clickhouse_groups.pyindex b8f5bfd85095..1845c04c1b44 100644--- a/products/groups/backend/tests/test_clickhouse_groups.py+++ b/products/groups/backend/tests/test_clickhouse_groups.py@@ -30,7 +30,7 @@ from posthog.hogql.query import execute_hogql_queryfrom posthog.constants import AvailableFeature, GroupTypeIndexfrom posthog.helpers.dashboard_templates import create_group_type_mapping_detail_dashboard-from posthog.models import GroupUsageMetric, PropertyDefinition+from posthog.models import PropertyDefinitionfrom posthog.models.group.group import Groupfrom posthog.models.group.util import ListGroupsResult, create_group, list_groups, raw_create_group_chfrom posthog.models.group_type_mapping import (@@ -51,7 +51,6 @@ from products.access_control.backend.models.access_control import AccessControlfrom products.groups.backend.presentation.views import _decode_groups_cursor, _encode_groups_cursorfrom products.notebooks.backend.facade.content import convert_notebook_content_to_markdown, is_markdown_notebook_contentfrom products.notebooks.backend.models import Notebook, ResourceNotebook-from products.warehouse_sources.backend.facade.models import DataWarehouseCredential, DataWarehouseTablePATH = "products.groups.backend.presentation.views"@@ -1923,385 +1922,6 @@ class GroupsTypesViewSetTestCase(APIBaseTest):self.assertEqual(group_type.name_plural, "Orgs")-class GroupUsageMetricViewSetTestCase(APIBaseTest):- def setUp(self):- super().setUp()- self.group_type = create_group_type_mapping(- team=self.team, project=self.project, group_type="organization", group_type_index=0- )- self.url = f"/api/projects/{self.team.id}/groups_types/{str(self.group_type.group_type_index)}/metrics"-- self.other_org = Organization.objects.create(name="other org")- self.other_team = Team.objects.create(organization=self.other_org, name="other team")- self.other_group_type = create_group_type_mapping(- team=self.other_team, project_id=self.other_team.project_id, group_type="company", group_type_index=0- )- self.other_url = (- f"/api/projects/{self.other_team.id}/groups_types/{str(self.other_group_type.group_type_index)}/metrics"- )-- def assertFields(self, data, metric):- self.assertEqual(data["id"], str(metric.id))- self.assertEqual(data["name"], metric.name)- self.assertEqual(data["format"], metric.format)- self.assertEqual(data["interval"], metric.interval)- self.assertEqual(data["display"], metric.display)- self.assertEqual(data["filters"], metric.filters)-- def _create_metric(self, **kwargs):- defaults = {- "team": self.team,- "group_type_index": self.group_type.group_type_index,- "name": "Events",- "filters": {"foo": "bar"},- }- defaults.update(kwargs)- return GroupUsageMetric.objects.create(**defaults)-- def test_list(self):- metric = self._create_metric()-- response = self.client.get(self.url)-- self.assertEqual(response.status_code, status.HTTP_200_OK)- self.assertFields(response.json()["results"][0], metric)-- def test_create(self):- payload = {"name": "Events", "filters": {"foo": "bar"}}-- response = self.client.post(self.url, payload)-- self.assertEqual(response.status_code, status.HTTP_201_CREATED)- metric = GroupUsageMetric.objects.get(id=response.json().get("id"))- self.assertFields(response.json(), metric)- self.assertEqual(metric.team, self.team, "Should set team automatically")- self.assertEqual(- metric.group_type_index, self.group_type.group_type_index, "Should set group_type_index automatically"- )- self.assertIsNotNone(metric.bytecode, "Should set bytecode automatically")-- def test_retrieve(self):- metric = self._create_metric()- url = f"{self.url}/{metric.id}"-- response = self.client.get(url)-- self.assertEqual(response.status_code, status.HTTP_200_OK)- self.assertFields(response.json(), metric)-- def test_update(self):- metric = self._create_metric()- url = f"{self.url}/{metric.id}"- payload = {- "name": "Updated Events",- "format": "currency",- "interval": 30,- "display": "sparkline",- "filters": {"updated": "value"},- }-- response = self.client.put(url, payload)-- self.assertEqual(response.status_code, status.HTTP_200_OK)- metric.refresh_from_db()- self.assertEqual(metric.name, "Updated Events")- self.assertEqual(metric.format, "currency")- self.assertEqual(metric.interval, 30)- self.assertEqual(metric.display, "sparkline")- self.assertEqual(metric.filters, {"updated": "value"})- self.assertFields(response.json(), metric)-- def test_delete(self):- metric = self._create_metric()- url = f"{self.url}/{metric.id}"-- response = self.client.delete(url)-- self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)- self.assertFalse(GroupUsageMetric.objects.filter(id=metric.id).exists())-- def test_partial_update(self):- metric = self._create_metric()- url = f"{self.url}/{metric.id}"- payload = {"name": "Partially Updated Events"}-- response = self.client.patch(url, payload)-- self.assertEqual(response.status_code, status.HTTP_200_OK)- metric.refresh_from_db()- self.assertEqual(metric.name, "Partially Updated Events")- self.assertEqual(metric.format, "numeric", "Should remain unchanged")- self.assertEqual(metric.interval, 7, "Should remain unchanged")- self.assertEqual(metric.display, "number", "Should remain unchanged")-- def test_delete_nonexistent(self):- fake_id = "00000000-0000-0000-0000-000000000000"- url = f"{self.url}/{fake_id}"-- response = self.client.delete(url)-- self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)-- def test_update_nonexistent(self):- fake_id = "00000000-0000-0000-0000-000000000000"- url = f"{self.url}/{fake_id}"- payload = {"name": "Updated Events"}-- response = self.client.put(url, payload)-- self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)-- def test_unauthenticated_access(self):- self.client.logout()-- response = self.client.get(self.url)-- self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)-- def test_unauthorized_team_access(self):- self._create_metric(group_type_index=self.other_group_type.group_type_index, team=self.other_team)-- response = self.client.get(self.other_url)-- self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)- self.assertEqual(response.json(), self.permission_denied_response("You don't have access to the project."))-- def test_unauthorized_metric_access(self):- other_metric = self._create_metric(- group_type_index=self.other_group_type.group_type_index, team=self.other_team- )- url = f"{self.other_url}/{other_metric.id}"-- response = self.client.get(url)-- self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)- self.assertEqual(response.json(), self.permission_denied_response("You don't have access to the project."))-- def test_unauthorized_metric_creation(self):- payload = {"name": "Unauthorized Events"}-- response = self.client.post(self.other_url, payload)-- self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)- self.assertEqual(response.json(), self.permission_denied_response("You don't have access to the project."))-- def test_unauthorized_metric_modification(self):- other_metric = self._create_metric(- group_type_index=self.other_group_type.group_type_index, team=self.other_team- )- url = f"{self.other_url}/{other_metric.id}"- payload = {"name": "Hacked Events"}-- response = self.client.put(url, payload)-- self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)- self.assertEqual(response.json(), self.permission_denied_response("You don't have access to the project."))-- def test_unauthorized_metric_deletion(self):- other_metric = self._create_metric(- group_type_index=self.other_group_type.group_type_index, team=self.other_team- )- url = f"{self.other_url}/{other_metric.id}"-- response = self.client.delete(url)-- self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)- self.assertEqual(response.json(), self.permission_denied_response("You don't have access to the project."))-- self.assertTrue(GroupUsageMetric.objects.filter(id=other_metric.id).exists())-- def test_non_admin_cannot_update_metric(self):- metric = self._create_metric()- url = f"{self.url}/{metric.id}"- self.organization_membership.level = OrganizationMembership.Level.MEMBER- self.organization_membership.save()- self.organization.available_product_features = [- {"key": AvailableFeature.ACCESS_CONTROL, "name": AvailableFeature.ACCESS_CONTROL},- ]- self.organization.save()- # Grant member-level (not admin) project access so the request reaches the serializer- AccessControl.objects.create(- team=self.team,- resource="project",- resource_id=str(self.team.id),- access_level="member",- )-- response = self.client.patch(url, {"name": "Should not update"})-- self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)- metric.refresh_from_db()- self.assertEqual(metric.name, "Events")-- def test_admin_can_update_metric(self):- metric = self._create_metric()- url = f"{self.url}/{metric.id}"- self.organization_membership.level = OrganizationMembership.Level.ADMIN- self.organization_membership.save()-- response = self.client.patch(url, {"name": "Updated by admin"})-- self.assertEqual(response.status_code, status.HTTP_200_OK)- metric.refresh_from_db()- self.assertEqual(metric.name, "Updated by admin")-- def _create_dw_table(self, name: str = "stripe_charges"):- credential = DataWarehouseCredential.objects.create(team=self.team, access_key="key", access_secret="secret")- return DataWarehouseTable.objects.create(- team=self.team,- name=name,- credential=credential,- url_pattern="http://example.com/{name}",- columns={- "customer_id": {"clickhouse": "String", "hogql": "StringDatabaseField", "valid": True},- "created": {"clickhouse": "DateTime64(3, 'UTC')", "hogql": "DateTimeDatabaseField", "valid": True},- "amount": {"clickhouse": "Float64", "hogql": "FloatDatabaseField", "valid": True},- },- )-- def test_create_data_warehouse_metric(self):- self._create_dw_table()- payload = {- "name": "DW signups",- "math": "count",- "filters": {- "source": "data_warehouse",- "table_name": "stripe_charges",- "timestamp_field": "created",- "key_field": "customer_id",- },- }-- response = self.client.post(self.url, payload, format="json")-- self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.json())- metric = GroupUsageMetric.objects.get(id=response.json()["id"])- self.assertEqual(metric.filters["source"], "data_warehouse")- self.assertEqual(metric.filters["table_name"], "stripe_charges")-- def test_create_data_warehouse_metric_rejects_missing_fields(self):- self._create_dw_table()- payload = {- "name": "DW signups",- "filters": {"source": "data_warehouse", "table_name": "stripe_charges"},- }-- response = self.client.post(self.url, payload, format="json")-- self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)- self.assertEqual(response.json()["attr"], "filters")-- def test_create_data_warehouse_metric_rejects_unknown_table(self):- payload = {- "name": "DW signups",- "filters": {- "source": "data_warehouse",- "table_name": "no_such_table",- "timestamp_field": "created",- "key_field": "customer_id",- },- }-- response = self.client.post(self.url, payload, format="json")-- self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)- self.assertEqual(response.json()["attr"], "filters")-- @parameterized.expand(- [- ("duplicate_name_on_create", "post", {"name": "Events", "filters": {}}, 400, "name"),- ("duplicate_name_on_rename", "patch", {"name": "Events"}, 400, "name"),- ("own_name_on_update", "patch", {"name": "Signups"}, 200, None),- (- "test_account_cohort_filter",- "post",- {"name": "Pageviews", "filters": {"filter_test_accounts": True}},- 400,- "filters",- ),- ]- )- def test_save_rejects_invalid_input_with_400(self, _name, method, payload, expected_status, expected_attr):- self._create_metric(name="Events")- signups = self._create_metric(name="Signups")- self.team.test_account_filters = [{"type": "cohort", "key": "id", "value": 999999}]- self.team.save()- url = self.url if method == "post" else f"{self.url}/{signups.id}"-- response = getattr(self.client, method)(url, payload, format="json")-- self.assertEqual(response.status_code, expected_status, response.json())- self.assertEqual(response.json().get("attr"), expected_attr)-- def test_create_data_warehouse_metric_accepts_table_with_null_deleted(self):- table = self._create_dw_table()- table.deleted = None- table.save()-- payload = {- "name": "DW signups",- "filters": {- "source": "data_warehouse",- "table_name": "stripe_charges",- "timestamp_field": "created",- "key_field": "customer_id",- },- }-- response = self.client.post(self.url, payload, format="json")-- self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.json())-- def test_create_data_warehouse_metric_rejects_soft_deleted_table(self):- table = self._create_dw_table()- table.deleted = True- table.save()-- payload = {- "name": "DW signups",- "filters": {- "source": "data_warehouse",- "table_name": "stripe_charges",- "timestamp_field": "created",- "key_field": "customer_id",- },- }-- response = self.client.post(self.url, payload, format="json")-- self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)- self.assertEqual(response.json()["attr"], "filters")-- def test_create_data_warehouse_sum_requires_math_property(self):- self._create_dw_table()- payload = {- "name": "DW revenue",- "math": "sum",- "filters": {- "source": "data_warehouse",- "table_name": "stripe_charges",- "timestamp_field": "created",- "key_field": "customer_id",- },- }-- response = self.client.post(self.url, payload, format="json")-- self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)- self.assertEqual(response.json()["attr"], "math_property")-- def test_create_metric_rejects_unknown_source(self):- payload = {- "name": "Bogus",- "filters": {"source": "something_else"},- }-- response = self.client.post(self.url, payload, format="json")-- self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)- self.assertEqual(response.json()["attr"], "filters")--class GroupPropertyDefinitionsTestCase(ClickhouseTestMixin, APIBaseTest):def setUp(self):super().setUp()