From 8b7c3485fe3c50c3db91fee924552af0998203fb Mon Sep 17 00:00:00 2001 From: Michael Matloka Date: Fri, 6 Dec 2024 09:59:42 +0100 Subject: [PATCH] chore(environments): Update filtering of group types all over (#26672) Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> --- ee/clickhouse/queries/related_actors_query.py | 2 +- ee/clickhouse/views/groups.py | 4 +- ee/hogai/schema_generator/nodes.py | 2 +- ee/hogai/taxonomy_agent/nodes.py | 2 +- ee/hogai/taxonomy_agent/toolkit.py | 2 +- posthog/api/feature_flag.py | 2 +- posthog/api/project.py | 2 +- posthog/api/team.py | 2 +- .../api/test/__snapshots__/test_decide.ambr | 2 +- posthog/api/test/test_team.py | 21 ++++++++ posthog/demo/matrix/manager.py | 8 ++-- posthog/hogql/database/database.py | 2 +- .../management/commands/generate_demo_data.py | 19 ++++---- .../test_session_recordings.ambr | 48 +++++++++---------- 14 files changed, 71 insertions(+), 47 deletions(-) diff --git a/ee/clickhouse/queries/related_actors_query.py b/ee/clickhouse/queries/related_actors_query.py index 4b6198c222710..99817998d7119 100644 --- a/ee/clickhouse/queries/related_actors_query.py +++ b/ee/clickhouse/queries/related_actors_query.py @@ -41,7 +41,7 @@ def __init__( def run(self) -> list[SerializedActor]: results: list[SerializedActor] = [] results.extend(self._query_related_people()) - for group_type_mapping in GroupTypeMapping.objects.filter(team_id=self.team.pk): + for group_type_mapping in GroupTypeMapping.objects.filter(project_id=self.team.project_id): results.extend(self._query_related_groups(group_type_mapping.group_type_index)) return results diff --git a/ee/clickhouse/views/groups.py b/ee/clickhouse/views/groups.py index 4970a770854a2..be692dc597525 100644 --- a/ee/clickhouse/views/groups.py +++ b/ee/clickhouse/views/groups.py @@ -35,7 +35,9 @@ class GroupsTypesViewSet(TeamAndOrgViewSetMixin, mixins.ListModelMixin, viewsets @action(detail=False, methods=["PATCH"], name="Update group types metadata") def update_metadata(self, request: request.Request, *args, **kwargs): for row in cast(list[dict], request.data): - instance = GroupTypeMapping.objects.get(team=self.team, group_type_index=row["group_type_index"]) + instance = GroupTypeMapping.objects.get( + project_id=self.team.project_id, group_type_index=row["group_type_index"] + ) serializer = self.get_serializer(instance, data=row) serializer.is_valid(raise_exception=True) serializer.save() diff --git a/ee/hogai/schema_generator/nodes.py b/ee/hogai/schema_generator/nodes.py index c5e7ffbba85c4..f2d383d5c1e30 100644 --- a/ee/hogai/schema_generator/nodes.py +++ b/ee/hogai/schema_generator/nodes.py @@ -113,7 +113,7 @@ def router(self, state: AssistantState): @cached_property def _group_mapping_prompt(self) -> str: - groups = GroupTypeMapping.objects.filter(team=self._team).order_by("group_type_index") + groups = GroupTypeMapping.objects.filter(project_id=self._team.project_id).order_by("group_type_index") if not groups: return "The user has not defined any groups." diff --git a/ee/hogai/taxonomy_agent/nodes.py b/ee/hogai/taxonomy_agent/nodes.py index d499269a8ca95..025058a51eec1 100644 --- a/ee/hogai/taxonomy_agent/nodes.py +++ b/ee/hogai/taxonomy_agent/nodes.py @@ -179,7 +179,7 @@ def _events_prompt(self) -> str: @cached_property def _team_group_types(self) -> list[str]: return list( - GroupTypeMapping.objects.filter(team=self._team) + GroupTypeMapping.objects.filter(project_id=self._team.project_id) .order_by("group_type_index") .values_list("group_type", flat=True) ) diff --git a/ee/hogai/taxonomy_agent/toolkit.py b/ee/hogai/taxonomy_agent/toolkit.py index 2af39253b9e68..dc8a0e092c2e6 100644 --- a/ee/hogai/taxonomy_agent/toolkit.py +++ b/ee/hogai/taxonomy_agent/toolkit.py @@ -169,7 +169,7 @@ def render_text_description(self) -> str: @property def _groups(self): - return GroupTypeMapping.objects.filter(team=self._team).order_by("group_type_index") + return GroupTypeMapping.objects.filter(project_id=self._team.project_id).order_by("group_type_index") @cached_property def _entity_names(self) -> list[str]: diff --git a/posthog/api/feature_flag.py b/posthog/api/feature_flag.py index 435ccbe1cf27f..25c71d898950c 100644 --- a/posthog/api/feature_flag.py +++ b/posthog/api/feature_flag.py @@ -746,7 +746,7 @@ def local_evaluation(self, request: request.Request, **kwargs): "group_type_mapping": { str(row.group_type_index): row.group_type for row in GroupTypeMapping.objects.db_manager(DATABASE_FOR_LOCAL_EVALUATION).filter( - team_id=self.team_id + project_id=self.project_id ) }, "cohorts": cohorts, diff --git a/posthog/api/project.py b/posthog/api/project.py index 9bc0d91cec45f..b3b808f520430 100644 --- a/posthog/api/project.py +++ b/posthog/api/project.py @@ -191,7 +191,7 @@ def get_effective_membership_level(self, project: Project) -> Optional[Organizat return self.user_permissions.team(team).effective_membership_level def get_has_group_types(self, project: Project) -> bool: - return GroupTypeMapping.objects.filter(team_id=project.id).exists() + return GroupTypeMapping.objects.filter(project_id=project.id).exists() def get_live_events_token(self, project: Project) -> Optional[str]: team = project.teams.get(pk=project.pk) diff --git a/posthog/api/team.py b/posthog/api/team.py index f2486a68fe8a0..b473fc490ec7e 100644 --- a/posthog/api/team.py +++ b/posthog/api/team.py @@ -234,7 +234,7 @@ def get_effective_membership_level(self, team: Team) -> Optional[OrganizationMem return self.user_permissions.team(team).effective_membership_level def get_has_group_types(self, team: Team) -> bool: - return GroupTypeMapping.objects.filter(team_id=team.id).exists() + return GroupTypeMapping.objects.filter(project_id=team.project_id).exists() def get_live_events_token(self, team: Team) -> Optional[str]: return encode_jwt( diff --git a/posthog/api/test/__snapshots__/test_decide.ambr b/posthog/api/test/__snapshots__/test_decide.ambr index 56f6257a978d2..7ab6aaee06295 100644 --- a/posthog/api/test/__snapshots__/test_decide.ambr +++ b/posthog/api/test/__snapshots__/test_decide.ambr @@ -519,7 +519,7 @@ ''' SELECT 1 AS "a" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 LIMIT 1 ''' # --- diff --git a/posthog/api/test/test_team.py b/posthog/api/test/test_team.py index 0040ddd257e2d..0e40b6a595d36 100644 --- a/posthog/api/test/test_team.py +++ b/posthog/api/test/test_team.py @@ -16,6 +16,7 @@ from posthog.models import ActivityLog, EarlyAccessFeature from posthog.models.async_deletion.async_deletion import AsyncDeletion, DeletionType from posthog.models.dashboard import Dashboard +from posthog.models.group_type_mapping import GroupTypeMapping from posthog.models.instance_setting import get_instance_setting from posthog.models.organization import Organization, OrganizationMembership from posthog.models.personal_api_key import PersonalAPIKey, hash_key_value @@ -87,6 +88,26 @@ def test_retrieve_team(self): self.assertNotIn("event_names_with_usage", response_data) self.assertNotIn("event_properties_with_usage", response_data) + def test_retrieve_team_has_group_types(self): + other_team = Team.objects.create(organization=self.organization, project=self.project) + + response = self.client.get("/api/environments/@current/") + response_data = response.json() + + self.assertEqual(response.status_code, status.HTTP_200_OK, response_data) + self.assertEqual(response_data["has_group_types"], False) + + # Creating a group type in the same project, but different team + GroupTypeMapping.objects.create( + project=self.project, team=other_team, group_type="person", group_type_index=0 + ) + + response = self.client.get("/api/environments/@current/") + response_data = response.json() + + self.assertEqual(response.status_code, status.HTTP_200_OK, response_data) + self.assertEqual(response_data["has_group_types"], True) # Irreleveant that group type has different `team` + def test_cant_retrieve_team_from_another_org(self): org = Organization.objects.create(name="New Org") team = Team.objects.create(organization=org, name="Default project") diff --git a/posthog/demo/matrix/manager.py b/posthog/demo/matrix/manager.py index 0abc17f32ca08..f52a74fa6ba9a 100644 --- a/posthog/demo/matrix/manager.py +++ b/posthog/demo/matrix/manager.py @@ -204,7 +204,7 @@ def _erase_master_team_data(cls): # ) # ] # ) - GroupTypeMapping.objects.filter(team_id=cls.MASTER_TEAM_ID).delete() + GroupTypeMapping.objects.filter(project_id=cls.MASTER_TEAM_ID).delete() def _copy_analytics_data_from_master_team(self, target_team: Team): from posthog.models.event.sql import COPY_EVENTS_BETWEEN_TEAMS @@ -222,11 +222,11 @@ def _copy_analytics_data_from_master_team(self, target_team: Team): sync_execute(COPY_PERSON_DISTINCT_ID2S_BETWEEN_TEAMS, copy_params) sync_execute(COPY_EVENTS_BETWEEN_TEAMS, copy_params) sync_execute(COPY_GROUPS_BETWEEN_TEAMS, copy_params) - GroupTypeMapping.objects.filter(team_id=target_team.pk).delete() + GroupTypeMapping.objects.filter(project_id=target_team.project_id).delete() GroupTypeMapping.objects.bulk_create( ( - GroupTypeMapping(team=target_team, project_id=target_team.project_id, **record) - for record in GroupTypeMapping.objects.filter(team_id=self.MASTER_TEAM_ID).values( + GroupTypeMapping(team_id=target_team.id, project_id=target_team.project_id, **record) + for record in GroupTypeMapping.objects.filter(project_id=self.MASTER_TEAM_ID).values( "group_type", "group_type_index", "name_singular", "name_plural" ) ), diff --git a/posthog/hogql/database/database.py b/posthog/hogql/database/database.py index 37370800f30c3..9ca4500aa2abd 100644 --- a/posthog/hogql/database/database.py +++ b/posthog/hogql/database/database.py @@ -287,7 +287,7 @@ def create_hogql_database( "$virt_initial_channel_type", modifiers.customChannelTypeRules ) - for mapping in GroupTypeMapping.objects.filter(team=team): + for mapping in GroupTypeMapping.objects.filter(project_id=team.project_id): if database.events.fields.get(mapping.group_type) is None: database.events.fields[mapping.group_type] = FieldTraverser(chain=[f"group_{mapping.group_type_index}"]) diff --git a/posthog/management/commands/generate_demo_data.py b/posthog/management/commands/generate_demo_data.py index ce094620453a1..dae5cca8ffa73 100644 --- a/posthog/management/commands/generate_demo_data.py +++ b/posthog/management/commands/generate_demo_data.py @@ -2,6 +2,7 @@ import logging import secrets from time import monotonic +from typing import Optional from django.core import exceptions from django.core.management.base import BaseCommand @@ -67,13 +68,13 @@ def handle(self, *args, **options): seed = options.get("seed") or secrets.token_hex(16) now = options.get("now") or dt.datetime.now(dt.UTC) existing_team_id = options.get("team_id") - if ( - existing_team_id is not None - and existing_team_id != 0 - and not Team.objects.filter(pk=existing_team_id).exists() - ): - print(f"Team with ID {options['team_id']} does not exist!") - return + existing_team: Optional[Team] = None + if existing_team_id is not None and existing_team_id != 0: + try: + existing_team = Team.objects.get(pk=existing_team_id) + except Team.DoesNotExist: + print(f"Team with ID {options['team_id']} does not exist!") + return print("Instantiating the Matrix...") matrix = HedgeboxMatrix( seed, @@ -81,8 +82,8 @@ def handle(self, *args, **options): days_past=options["days_past"], days_future=options["days_future"], n_clusters=options["n_clusters"], - group_type_index_offset=GroupTypeMapping.objects.filter(team_id=existing_team_id).count() - if existing_team_id + group_type_index_offset=GroupTypeMapping.objects.filter(project_id=existing_team.project_id).count() + if existing_team else 0, ) print("Running simulation...") diff --git a/posthog/session_recordings/test/__snapshots__/test_session_recordings.ambr b/posthog/session_recordings/test/__snapshots__/test_session_recordings.ambr index 88a534a569646..c25bdb4d587b4 100644 --- a/posthog/session_recordings/test/__snapshots__/test_session_recordings.ambr +++ b/posthog/session_recordings/test/__snapshots__/test_session_recordings.ambr @@ -738,7 +738,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_get_session_recordings.24 @@ -865,7 +865,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_get_session_recordings.28 @@ -1637,7 +1637,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_listing_recordings_is_not_nplus1_for_persons.100 @@ -1788,7 +1788,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_listing_recordings_is_not_nplus1_for_persons.105 @@ -1915,7 +1915,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_listing_recordings_is_not_nplus1_for_persons.109 @@ -2543,7 +2543,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_listing_recordings_is_not_nplus1_for_persons.125 @@ -2670,7 +2670,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_listing_recordings_is_not_nplus1_for_persons.129 @@ -3234,7 +3234,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_listing_recordings_is_not_nplus1_for_persons.145 @@ -3361,7 +3361,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_listing_recordings_is_not_nplus1_for_persons.149 @@ -3988,7 +3988,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_listing_recordings_is_not_nplus1_for_persons.165 @@ -4115,7 +4115,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_listing_recordings_is_not_nplus1_for_persons.169 @@ -4706,7 +4706,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_listing_recordings_is_not_nplus1_for_persons.185 @@ -4833,7 +4833,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_listing_recordings_is_not_nplus1_for_persons.189 @@ -5506,7 +5506,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_listing_recordings_is_not_nplus1_for_persons.205 @@ -5633,7 +5633,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_listing_recordings_is_not_nplus1_for_persons.209 @@ -5940,7 +5940,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_listing_recordings_is_not_nplus1_for_persons.25 @@ -6067,7 +6067,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_listing_recordings_is_not_nplus1_for_persons.29 @@ -6671,7 +6671,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_listing_recordings_is_not_nplus1_for_persons.45 @@ -6798,7 +6798,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_listing_recordings_is_not_nplus1_for_persons.49 @@ -7214,7 +7214,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_listing_recordings_is_not_nplus1_for_persons.60 @@ -7365,7 +7365,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_listing_recordings_is_not_nplus1_for_persons.65 @@ -7492,7 +7492,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_listing_recordings_is_not_nplus1_for_persons.69 @@ -8116,7 +8116,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_listing_recordings_is_not_nplus1_for_persons.85 @@ -8243,7 +8243,7 @@ "posthog_grouptypemapping"."name_singular", "posthog_grouptypemapping"."name_plural" FROM "posthog_grouptypemapping" - WHERE "posthog_grouptypemapping"."team_id" = 99999 + WHERE "posthog_grouptypemapping"."project_id" = 99999 ''' # --- # name: TestSessionRecordings.test_listing_recordings_is_not_nplus1_for_persons.89