Skip to content

Commit 5ba59dd

Browse files
authored
fix: post analytics queries (#2991)
1 parent c88cbc0 commit 5ba59dd

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

src/cron/postAnalyticsClickhouse.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,15 @@ export const postAnalyticsClickhouseCron: Cron = {
3333

3434
const clickhouseClient = getClickHouseClient();
3535

36+
const queryParams = {
37+
lastRunAt: format(lastRunAt, 'yyyy-MM-dd HH:mm:ss'),
38+
};
39+
3640
const response = await clickhouseClient.query({
3741
query: /* sql */ `
3842
SELECT
3943
post_id AS id,
40-
created_at AS "updatedAt",
44+
max(created_at) AS "updatedAt",
4145
sum(impressions) AS impressions,
4246
uniqMerge(reach) AS reach,
4347
uniqMerge(bookmarks) AS bookmarks,
@@ -48,15 +52,12 @@ export const postAnalyticsClickhouseCron: Cron = {
4852
sum(shares_internal) AS "sharesInternal"
4953
FROM api.post_analytics
5054
FINAL
51-
WHERE created_at > {lastRunAt: DateTime}
52-
GROUP BY post_id, created_at
53-
ORDER BY created_at DESC
54-
LIMIT 1 BY post_id;
55+
GROUP BY id
56+
HAVING "updatedAt" > {lastRunAt: DateTime}
57+
ORDER BY "updatedAt" DESC;
5558
`,
5659
format: 'JSONEachRow',
57-
query_params: {
58-
lastRunAt: format(lastRunAt, 'yyyy-MM-dd HH:mm:ss'),
59-
},
60+
query_params: queryParams,
6061
});
6162

6263
const result = z
@@ -111,5 +112,10 @@ export const postAnalyticsClickhouseCron: Cron = {
111112
await setRedisHash<PostAnalyticsClickhouseCronConfig>(redisStorageKey, {
112113
lastRunAt: currentRunAt.toISOString(),
113114
});
115+
116+
logger.info(
117+
{ rows: data.length, queryParams },
118+
'synced post analytics data',
119+
);
114120
},
115121
};

src/cron/postAnalyticsHistoryDayClickhouse.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ export const postAnalyticsHistoryDayClickhouseCron: Cron = {
3333

3434
const clickhouseClient = getClickHouseClient();
3535

36+
const queryParams = {
37+
lastRunAt: format(lastRunAt, 'yyyy-MM-dd HH:mm:ss'),
38+
date: format(new Date(), 'yyyy-MM-dd'),
39+
};
40+
3641
const response = await clickhouseClient.query({
3742
query: /* sql */ `
3843
SELECT
@@ -43,14 +48,11 @@ export const postAnalyticsHistoryDayClickhouseCron: Cron = {
4348
FROM api.post_analytics_history
4449
FINAL
4550
WHERE date = {date: Date}
46-
AND created_at > {lastRunAt: DateTime}
47-
GROUP BY date, post_id
51+
GROUP BY date, id
52+
HAVING "updatedAt" > {lastRunAt: DateTime}
4853
`,
4954
format: 'JSONEachRow',
50-
query_params: {
51-
lastRunAt: format(lastRunAt, 'yyyy-MM-dd HH:mm:ss'),
52-
date: format(new Date(), 'yyyy-MM-dd'),
53-
},
55+
query_params: queryParams,
5456
});
5557

5658
const result = z
@@ -108,5 +110,10 @@ export const postAnalyticsHistoryDayClickhouseCron: Cron = {
108110
lastRunAt: currentRunAt.toISOString(),
109111
},
110112
);
113+
114+
logger.info(
115+
{ rows: data.length, queryParams },
116+
'synced post analytics history data',
117+
);
111118
},
112119
};

0 commit comments

Comments
 (0)