Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sessions): Limit the teams for which we ingest sessions v1 events #25766

Merged
merged 10 commits into from
Oct 30, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
from posthog.models.sessions.sql import DROP_SESSION_MATERIALIZED_VIEW_SQL, SESSIONS_TABLE_MV_SQL

operations = [
# drop the mv, and recreate it with the new part of the WHERE clause
run_sql_with_exceptions(DROP_SESSION_MATERIALIZED_VIEW_SQL()),
run_sql_with_exceptions(SESSIONS_TABLE_MV_SQL()),
]
2 changes: 1 addition & 1 deletion posthog/clickhouse/test/__snapshots__/test_schema.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -2086,7 +2086,7 @@
sumIf(1, event='$autocapture') as autocapture_count

FROM posthog_test.sharded_events
WHERE `$session_id` IS NOT NULL AND `$session_id` != ''
WHERE `$session_id` IS NOT NULL AND `$session_id` != '' AND team_id IN (1, 2, 13610, 19279, 21173, 29929, 32050, 9910, 11775, 21129, 31490)
GROUP BY `$session_id`, team_id


Expand Down
53 changes: 30 additions & 23 deletions posthog/clickhouse/test/test_sessions_model.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from posthog.clickhouse.client import sync_execute, query_with_columns
from posthog.models import Team
from posthog.test.base import (
_create_event,
ClickhouseTestMixin,
BaseTest,
ClickhouseDestroyTablesMixin,
)

distinct_id_counter = 0
Expand All @@ -21,7 +23,12 @@ def create_session_id():
return f"s{session_id_counter}"


class TestSessionsModel(ClickhouseTestMixin, BaseTest):
# only certain team ids can insert events into this legacy sessions table, see sessions/sql.py for more info
TEAM_ID = 2
TEAM = Team(id=TEAM_ID)


class TestSessionsModel(ClickhouseDestroyTablesMixin, ClickhouseTestMixin, BaseTest):
def select_by_session_id(self, session_id):
return query_with_columns(
"""
Expand All @@ -34,15 +41,15 @@ def select_by_session_id(self, session_id):
""",
{
"session_id": session_id,
"team_id": self.team.id,
"team_id": TEAM_ID,
},
)

def test_it_creates_session_when_creating_event(self):
distinct_id = create_distinct_id()
session_id = create_session_id()
_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$current_url": "/", "$session_id": session_id},
Expand All @@ -60,7 +67,7 @@ def test_it_creates_session_when_creating_event(self):
""",
{
"distinct_id": distinct_id,
"team_id": self.team.id,
"team_id": TEAM_ID,
},
)

Expand All @@ -72,14 +79,14 @@ def test_handles_different_distinct_id_across_same_session(self):
session_id = create_session_id()

_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id1,
properties={"$session_id": session_id},
timestamp="2024-03-08",
)
_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id2,
properties={"$session_id": session_id},
Expand All @@ -96,28 +103,28 @@ def test_handles_entry_and_exit_urls(self):
session_id = create_session_id()

_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$current_url": "/entry", "$session_id": session_id},
timestamp="2024-03-08:01",
)
_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$current_url": "/middle", "$session_id": session_id},
timestamp="2024-03-08:02",
)
_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$current_url": "/middle", "$session_id": session_id},
timestamp="2024-03-08:03",
)
_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$current_url": "/exit", "$session_id": session_id},
Expand All @@ -136,14 +143,14 @@ def test_handles_initial_utm_properties(self):
session_id = create_session_id()

_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$session_id": session_id, "utm_source": "source"},
timestamp="2024-03-08",
)
_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$session_id": session_id, "utm_source": "other_source"},
Expand All @@ -159,35 +166,35 @@ def test_counts_pageviews_autocaptures_and_events(self):
session_id = create_session_id()

_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$session_id": session_id},
timestamp="2024-03-08",
)
_create_event(
team=self.team,
team=TEAM,
event="$autocapture",
distinct_id=distinct_id,
properties={"$session_id": session_id},
timestamp="2024-03-08",
)
_create_event(
team=self.team,
team=TEAM,
event="$autocapture",
distinct_id=distinct_id,
properties={"$session_id": session_id},
timestamp="2024-03-08",
)
_create_event(
team=self.team,
team=TEAM,
event="other event",
distinct_id=distinct_id,
properties={"$session_id": session_id},
timestamp="2024-03-08",
)
_create_event(
team=self.team,
team=TEAM,
event="$pageleave",
distinct_id=distinct_id,
properties={"$session_id": session_id},
Expand All @@ -209,14 +216,14 @@ def test_separates_sessions_across_same_user(self):
session_id3 = create_session_id()

_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$session_id": session_id1},
timestamp="2024-03-08",
)
_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$session_id": session_id2},
Expand All @@ -235,7 +242,7 @@ def test_select_from_sessions(self):
distinct_id = create_distinct_id()
session_id = create_session_id()
_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$session_id": session_id},
Expand All @@ -260,7 +267,7 @@ def test_select_from_sessions(self):
""",
{
"session_id": session_id,
"team_id": self.team.id,
"team_id": TEAM_ID,
},
)
self.assertEqual(len(responses), 1)
Expand All @@ -270,7 +277,7 @@ def test_select_from_sessions_mv(self):
distinct_id = create_distinct_id()
session_id = create_session_id()
_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$session_id": session_id},
Expand All @@ -295,7 +302,7 @@ def test_select_from_sessions_mv(self):
""",
{
"session_id": session_id,
"team_id": self.team.id,
"team_id": TEAM_ID,
},
)
self.assertEqual(len(responses), 1)
Loading
Loading