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

feat(web-analytics): Fix group by, remove date range #23367

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions posthog/clickhouse/migrations/0066_sessions_group_by.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
from posthog.models.raw_sessions.sql import RAW_SESSION_TABLE_UPDATE_SQL

operations = [
run_sql_with_exceptions(RAW_SESSION_TABLE_UPDATE_SQL),
]
11 changes: 6 additions & 5 deletions posthog/clickhouse/test/__snapshots__/test_schema.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -1745,11 +1745,12 @@
-- replay
false as maybe_has_session_replay
FROM posthog_test.sharded_events
WHERE and(
bitAnd(bitShiftRight(toUInt128(accurateCastOrNull(`$session_id`, 'UUID')), 76), 0xF) == 7, -- has a session id and is valid uuidv7
toYYYYMMDD(timestamp) >= 0
)
GROUP BY session_id_v7, team_id
WHERE bitAnd(bitShiftRight(toUInt128(accurateCastOrNull(`$session_id`, 'UUID')), 76), 0xF) == 7 -- has a session id and is valid uuidv7)
GROUP BY
team_id,
toStartOfHour(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(session_id_v7, 80)), 1000))),
cityHash64(session_id_v7),
session_id_v7


'''
Expand Down
19 changes: 6 additions & 13 deletions posthog/models/raw_sessions/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@
ReplicationScheme,
AggregatingMergeTree,
)
from posthog.settings import TEST

# the date of the day after this PR will be merged, this allows us to run the backfill script on complete days
# with a condition like toYYYYMMDD(timestamp) < X
INGEST_FROM_DATE = "toYYYYMMDD(timestamp) >= 20240626"
if TEST:
INGEST_FROM_DATE = "toYYYYMMDD(timestamp) >= 0"

TABLE_BASE_NAME = "raw_sessions"
RAW_SESSIONS_DATA_TABLE = lambda: f"sharded_{TABLE_BASE_NAME}"
Expand Down Expand Up @@ -329,11 +322,12 @@ def source_int_column(column_name: str) -> str:
-- replay
false as maybe_has_session_replay
FROM {database}.sharded_events
WHERE and(
bitAnd(bitShiftRight(toUInt128(accurateCastOrNull(`$session_id`, 'UUID')), 76), 0xF) == 7, -- has a session id and is valid uuidv7
{INGEST_FROM_DATE}
)
GROUP BY session_id_v7, team_id
WHERE bitAnd(bitShiftRight(toUInt128(accurateCastOrNull(`$session_id`, 'UUID')), 76), 0xF) == 7 -- has a session id and is valid uuidv7)
GROUP BY
team_id,
toStartOfHour(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(session_id_v7, 80)), 1000))),
cityHash64(session_id_v7),
session_id_v7
""".format(
database=settings.CLICKHOUSE_DATABASE,
current_url=source_url_column("$current_url"),
Expand Down Expand Up @@ -370,7 +364,6 @@ def source_int_column(column_name: str) -> str:
mc_cid=source_string_column("mc_cid"),
igshid=source_string_column("igshid"),
ttclid=source_string_column("ttclid"),
INGEST_FROM_DATE=INGEST_FROM_DATE,
)
)

Expand Down
Loading