Skip to content

Commit d4d41c3

Browse files
fix: join cohort people counts with cohorts, not people (#39922)
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 7d08c85 commit d4d41c3

File tree

3 files changed

+53
-22
lines changed

3 files changed

+53
-22
lines changed

ee/clickhouse/models/test/test_cohort.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,37 @@ def test_calculate_people_ch_in_multiteam_project(self):
15001500
self.assertCountEqual([r[0] for r in results_team1], [person2_team1.uuid])
15011501
self.assertCountEqual([r[0] for r in results_team2], [person1_team2.uuid])
15021502

1503+
def test_static_cohort_size_in_multiteam_project(self):
1504+
from posthog.models.cohort.util import get_static_cohort_size
1505+
1506+
# Create another team in the same project
1507+
team2 = Team.objects.create(organization=self.organization, project=self.team.project)
1508+
1509+
# Create people in team 1
1510+
_create_person(team_id=self.team.pk, distinct_ids=["person1_team1"])
1511+
_create_person(team_id=self.team.pk, distinct_ids=["person2_team1"])
1512+
1513+
# Create people in team 2
1514+
_create_person(team_id=team2.pk, distinct_ids=["person1_team2"])
1515+
_create_person(team_id=team2.pk, distinct_ids=["person2_team2"])
1516+
_create_person(team_id=team2.pk, distinct_ids=["person3_team2"])
1517+
1518+
# Create a static cohort in team2
1519+
static_cohort = Cohort.objects.create(team=team2, is_static=True, name="static cohort")
1520+
1521+
# Add people from both teams to the cohort
1522+
static_cohort.insert_users_by_list(["person1_team1", "person2_team1"]) # Team 1 people
1523+
static_cohort.insert_users_by_list(["person1_team2", "person2_team2"]) # Team 2 people
1524+
1525+
# Verify get_static_cohort_size correctly filters by team_id
1526+
# The cohort belongs to team2, so filtering by team2 should count team2 people
1527+
count_team2 = get_static_cohort_size(cohort_id=static_cohort.pk, team_id=team2.pk)
1528+
self.assertEqual(count_team2, 2)
1529+
1530+
# Filtering by team1 should return 0 since cohort belongs to team2
1531+
count_team1 = get_static_cohort_size(cohort_id=static_cohort.pk, team_id=self.team.pk)
1532+
self.assertEqual(count_team1, 0)
1533+
15031534
def test_cohortpeople_action_all_events(self):
15041535
# Create an action that matches all events (no specific event defined)
15051536
action = Action.objects.create(team=self.team, name="all events", steps_json=[{"event": None}])

posthog/api/test/__snapshots__/test_feature_flag.ambr

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -817,9 +817,9 @@
817817
'''
818818
SELECT COUNT(*) AS "__count"
819819
FROM "posthog_cohortpeople"
820-
INNER JOIN "posthog_person" ON ("posthog_cohortpeople"."person_id" = "posthog_person"."id")
821-
WHERE ("posthog_cohortpeople"."cohort_id" = 99999
822-
AND "posthog_person"."team_id" = 99999)
820+
INNER JOIN "posthog_cohort" ON ("posthog_cohortpeople"."cohort_id" = "posthog_cohort"."id")
821+
WHERE ("posthog_cohort"."team_id" = 99999
822+
AND "posthog_cohortpeople"."cohort_id" = 99999)
823823
'''
824824
# ---
825825
# name: TestCohortGenerationForFeatureFlag.test_creating_static_cohort_iterator.24
@@ -1185,9 +1185,9 @@
11851185
'''
11861186
SELECT COUNT(*) AS "__count"
11871187
FROM "posthog_cohortpeople"
1188-
INNER JOIN "posthog_person" ON ("posthog_cohortpeople"."person_id" = "posthog_person"."id")
1189-
WHERE ("posthog_cohortpeople"."cohort_id" = 99999
1190-
AND "posthog_person"."team_id" = 99999)
1188+
INNER JOIN "posthog_cohort" ON ("posthog_cohortpeople"."cohort_id" = "posthog_cohort"."id")
1189+
WHERE ("posthog_cohort"."team_id" = 99999
1190+
AND "posthog_cohortpeople"."cohort_id" = 99999)
11911191
'''
11921192
# ---
11931193
# name: TestCohortGenerationForFeatureFlag.test_creating_static_cohort_iterator.7
@@ -1423,9 +1423,9 @@
14231423
'''
14241424
SELECT COUNT(*) AS "__count"
14251425
FROM "posthog_cohortpeople"
1426-
INNER JOIN "posthog_person" ON ("posthog_cohortpeople"."person_id" = "posthog_person"."id")
1427-
WHERE ("posthog_cohortpeople"."cohort_id" = 99999
1428-
AND "posthog_person"."team_id" = 99999)
1426+
INNER JOIN "posthog_cohort" ON ("posthog_cohortpeople"."cohort_id" = "posthog_cohort"."id")
1427+
WHERE ("posthog_cohort"."team_id" = 99999
1428+
AND "posthog_cohortpeople"."cohort_id" = 99999)
14291429
'''
14301430
# ---
14311431
# name: TestCohortGenerationForFeatureFlag.test_creating_static_cohort_with_cohort_flag_adds_cohort_props_as_default_too.13
@@ -2535,9 +2535,9 @@
25352535
'''
25362536
SELECT COUNT(*) AS "__count"
25372537
FROM "posthog_cohortpeople"
2538-
INNER JOIN "posthog_person" ON ("posthog_cohortpeople"."person_id" = "posthog_person"."id")
2539-
WHERE ("posthog_cohortpeople"."cohort_id" = 99999
2540-
AND "posthog_person"."team_id" = 99999)
2538+
INNER JOIN "posthog_cohort" ON ("posthog_cohortpeople"."cohort_id" = "posthog_cohort"."id")
2539+
WHERE ("posthog_cohort"."team_id" = 99999
2540+
AND "posthog_cohortpeople"."cohort_id" = 99999)
25412541
'''
25422542
# ---
25432543
# name: TestCohortGenerationForFeatureFlag.test_creating_static_cohort_with_default_person_properties_adjustment.3
@@ -3043,9 +3043,9 @@
30433043
'''
30443044
SELECT COUNT(*) AS "__count"
30453045
FROM "posthog_cohortpeople"
3046-
INNER JOIN "posthog_person" ON ("posthog_cohortpeople"."person_id" = "posthog_person"."id")
3047-
WHERE ("posthog_cohortpeople"."cohort_id" = 99999
3048-
AND "posthog_person"."team_id" = 99999)
3046+
INNER JOIN "posthog_cohort" ON ("posthog_cohortpeople"."cohort_id" = "posthog_cohort"."id")
3047+
WHERE ("posthog_cohort"."team_id" = 99999
3048+
AND "posthog_cohortpeople"."cohort_id" = 99999)
30493049
'''
30503050
# ---
30513051
# name: TestCohortGenerationForFeatureFlag.test_creating_static_cohort_with_default_person_properties_adjustment.8
@@ -3205,9 +3205,9 @@
32053205
'''
32063206
SELECT COUNT(*) AS "__count"
32073207
FROM "posthog_cohortpeople"
3208-
INNER JOIN "posthog_person" ON ("posthog_cohortpeople"."person_id" = "posthog_person"."id")
3209-
WHERE ("posthog_cohortpeople"."cohort_id" = 99999
3210-
AND "posthog_person"."team_id" = 99999)
3208+
INNER JOIN "posthog_cohort" ON ("posthog_cohortpeople"."cohort_id" = "posthog_cohort"."id")
3209+
WHERE ("posthog_cohort"."team_id" = 99999
3210+
AND "posthog_cohortpeople"."cohort_id" = 99999)
32113211
'''
32123212
# ---
32133213
# name: TestCohortGenerationForFeatureFlag.test_creating_static_cohort_with_experience_continuity_flag.11
@@ -4101,9 +4101,9 @@
41014101
'''
41024102
SELECT COUNT(*) AS "__count"
41034103
FROM "posthog_cohortpeople"
4104-
INNER JOIN "posthog_person" ON ("posthog_cohortpeople"."person_id" = "posthog_person"."id")
4105-
WHERE ("posthog_cohortpeople"."cohort_id" = 99999
4106-
AND "posthog_person"."team_id" = 99999)
4104+
INNER JOIN "posthog_cohort" ON ("posthog_cohortpeople"."cohort_id" = "posthog_cohort"."id")
4105+
WHERE ("posthog_cohort"."team_id" = 99999
4106+
AND "posthog_cohortpeople"."cohort_id" = 99999)
41074107
'''
41084108
# ---
41094109
# name: TestFeatureFlag.test_creating_static_cohort.2

posthog/models/cohort/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ def remove_person_from_static_cohort(person_uuid: uuid.UUID, cohort_id: int, *,
420420

421421

422422
def get_static_cohort_size(*, cohort_id: int, team_id: int) -> int:
423-
count = CohortPeople.objects.filter(cohort_id=cohort_id, person__team_id=team_id).count()
423+
count = CohortPeople.objects.filter(cohort_id=cohort_id, cohort__team_id=team_id).count()
424424

425425
return count
426426

0 commit comments

Comments
 (0)