From ab6445c24456d90e95c3893ab049b760f3805df7 Mon Sep 17 00:00:00 2001 From: Viktor Poluksht Date: Tue, 4 Nov 2025 17:14:21 +0000 Subject: [PATCH 1/2] Revert "chore: optimise post analytics query (#3256)" This reverts commit 1d99717bf04c30997c92f2d0f9e280d67cffb324. --- ...04112214_post_analytics_events_mv.down.sql | 0 ...1104112214_post_analytics_events_mv.up.sql | 19 ------------------- src/cron/postAnalyticsClickhouse.ts | 2 +- 3 files changed, 1 insertion(+), 20 deletions(-) delete mode 100644 clickhouse/migrations/20251104112214_post_analytics_events_mv.down.sql delete mode 100644 clickhouse/migrations/20251104112214_post_analytics_events_mv.up.sql diff --git a/clickhouse/migrations/20251104112214_post_analytics_events_mv.down.sql b/clickhouse/migrations/20251104112214_post_analytics_events_mv.down.sql deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/clickhouse/migrations/20251104112214_post_analytics_events_mv.up.sql b/clickhouse/migrations/20251104112214_post_analytics_events_mv.up.sql deleted file mode 100644 index 2310738309..0000000000 --- a/clickhouse/migrations/20251104112214_post_analytics_events_mv.up.sql +++ /dev/null @@ -1,19 +0,0 @@ -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') -GROUP BY target_id - SETTINGS materialized_views_ignore_errors = 1; diff --git a/src/cron/postAnalyticsClickhouse.ts b/src/cron/postAnalyticsClickhouse.ts index 0a7899a63b..b8a47be15d 100644 --- a/src/cron/postAnalyticsClickhouse.ts +++ b/src/cron/postAnalyticsClickhouse.ts @@ -57,7 +57,7 @@ export const postAnalyticsClickhouseCron: Cron = { sum(clicks_ads) AS "clicksAds", sum(go_to_link) AS "goToLink" FROM api.post_analytics - WHERE "created_at" > {lastRunAt: DateTime} + FINAL GROUP BY id HAVING "updatedAt" > {lastRunAt: DateTime} ORDER BY "updatedAt" DESC; From 4cb51ce142433613a25d8a27c42c75aad33d9636 Mon Sep 17 00:00:00 2001 From: Viktor Poluksht Date: Tue, 4 Nov 2025 17:31:40 +0000 Subject: [PATCH 2/2] chore: fix migrations Signed-off-by: Viktor Poluksht --- ...04112214_post_analytics_events_mv.down.sql | 0 ...1104112214_post_analytics_events_mv.up.sql | 19 +++++++++++++++++++ ...2_post_analytics_events_mv_revert.down.sql | 0 ...102_post_analytics_events_mv_revert.up.sql | 18 ++++++++++++++++++ 4 files changed, 37 insertions(+) create mode 100644 clickhouse/migrations/20251104112214_post_analytics_events_mv.down.sql create mode 100644 clickhouse/migrations/20251104112214_post_analytics_events_mv.up.sql create mode 100644 clickhouse/migrations/20251104173102_post_analytics_events_mv_revert.down.sql create mode 100644 clickhouse/migrations/20251104173102_post_analytics_events_mv_revert.up.sql diff --git a/clickhouse/migrations/20251104112214_post_analytics_events_mv.down.sql b/clickhouse/migrations/20251104112214_post_analytics_events_mv.down.sql new file mode 100644 index 0000000000..e69de29bb2 diff --git a/clickhouse/migrations/20251104112214_post_analytics_events_mv.up.sql b/clickhouse/migrations/20251104112214_post_analytics_events_mv.up.sql new file mode 100644 index 0000000000..2310738309 --- /dev/null +++ b/clickhouse/migrations/20251104112214_post_analytics_events_mv.up.sql @@ -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') +GROUP BY target_id + SETTINGS materialized_views_ignore_errors = 1; diff --git a/clickhouse/migrations/20251104173102_post_analytics_events_mv_revert.down.sql b/clickhouse/migrations/20251104173102_post_analytics_events_mv_revert.down.sql new file mode 100644 index 0000000000..e69de29bb2 diff --git a/clickhouse/migrations/20251104173102_post_analytics_events_mv_revert.up.sql b/clickhouse/migrations/20251104173102_post_analytics_events_mv_revert.up.sql new file mode 100644 index 0000000000..c78fe3e09e --- /dev/null +++ b/clickhouse/migrations/20251104173102_post_analytics_events_mv_revert.up.sql @@ -0,0 +1,18 @@ +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 (server_timestamp > '2025-10-08 13:00:00') +GROUP BY target_id + SETTINGS materialized_views_ignore_errors = 1;