Skip to content
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
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ALTER TABLE api.post_analytics_events_mv
MODIFY QUERY
SELECT target_id AS post_id,
sumIf(toUInt64(1), event_name = 'share post') AS shares_external,
sumIf(toUInt64(1), event_name = 'share to squad') AS shares_internal,
uniqStateIf(user_id, event_name = 'bookmark post') AS bookmarks,
sumIf(toUInt64(1), (event_name = 'impression') AND (target_type = 'post')) AS impressions,
uniqStateIf(user_id, (event_name = 'impression') AND (target_type = 'post')) AS reach,
uniqStateIf(user_id, (event_name = 'impression') AND (target_type = 'post')) AS reach_all,
sumIf(toUInt64(1), (event_name = 'click') AND (target_type = 'post')) AS clicks,
sumIf(toUInt64(1), (event_name = 'go to link') AND (target_type = 'post')) AS go_to_link,
max(server_timestamp) AS created_at
FROM events.raw_events
WHERE (event_name IN ('share post', 'bookmark post', 'impression', 'share to squad', 'click', 'go to link'))
AND (target_id IS NOT NULL)
AND (target_type = 'post')
AND (server_timestamp > '2025-10-08 13:00:00')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this hard coded date?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was there and I haven't changed it.

idea of specifying the date/timestamp is to cut the time when MV begins working, so you can manually backfill the data before this timestamp.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've only added AND (target_type = 'post')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah really hate you can't see actual diffs on these migrations :(

GROUP BY target_id
SETTINGS materialized_views_ignore_errors = 1;
2 changes: 1 addition & 1 deletion src/cron/postAnalyticsClickhouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const postAnalyticsClickhouseCron: Cron = {
sum(clicks_ads) AS "clicksAds",
sum(go_to_link) AS "goToLink"
FROM api.post_analytics
FINAL
WHERE "created_at" > {lastRunAt: DateTime}
GROUP BY id
HAVING "updatedAt" > {lastRunAt: DateTime}
ORDER BY "updatedAt" DESC;
Expand Down
Loading