Skip to content

Commit 4805eb4

Browse files
authored
feat: post analytics (#2971)
1 parent 59f6b7f commit 4805eb4

File tree

14 files changed

+277
-0
lines changed

14 files changed

+277
-0
lines changed

.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,7 @@ OPEN_EXCHANGE_RATES_APP_ID=topsecret
8585
PERSONALIZED_DIGEST_SECRET=topsecret
8686

8787
GEOIP_PATH=
88+
89+
CLICKHOUSE_URL=http://host.docker.internal:18123
90+
CLICKHOUSE_USER=default
91+
CLICKHOUSE_PASSWORD=changeme

.infra/Pulumi.prod.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ config:
160160
secure: AAABACPC/WLJuAHpqC3oE43YKjEzcFSXYSLfAx69vvRnC5xsOj2Md4tHo3RAAeVfrFaLRwugIQ7bPEZ4VBJnwJPem1NW0Nkm
161161
briefingFeed:
162162
secure: AAABAJWDkWAezGM15dk8xXwDdav6cmPwS/2x+tKvSwql/JUzFadVDpmlRRTSOGIh7WA++s+D7C6wdyy35Cg2FQ==
163+
clickhouseUser:
164+
secure: AAABAGoCA+Y4PvGtDV8m3osmBGGiI2vVoyzbdEDJ3n7W8a40Tk5rk/hbCQ==
165+
clickhousePassword:
166+
secure: AAABAAysCHiW8/lYE8NP3D9CE2UBE8YJdDZKf4B7aYVygfgyUAj9rk52klyHA26/jRtBW5pzCTTMHTatBm+ABdMTajgGy1GZ3yYkXMwzhhbJo30a+pq18vYGcx0F04MI
167+
clickhouseUrl:
168+
secure: AAABAOH/zIUW1KRNkCa3XH3Wxrs/GLDTjOeXsHqlDPoGowQC04O958oU8MXxtE0OeiNoHjTm/e6PrEr66nluAeEcwV0BhQLvVAAoP3L150tQNUU=
163169
api:k8s:
164170
host: subs.daily.dev
165171
namespace: daily

.infra/crons.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,8 @@ export const crons: Cron[] = [
122122
name: 'clean-stale-user-transactions',
123123
schedule: '50 6 * * 0',
124124
},
125+
{
126+
name: 'post-analytics-clickhouse',
127+
schedule: '*/5 * * * *',
128+
},
125129
];
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { crons } from '../../src/cron/index';
2+
import { postAnalyticsClickhouseCron as cron } from '../../src/cron/postAnalyticsClickhouse';
3+
4+
describe('postAnalyticsClickhouse cron', () => {
5+
it('should be registered', () => {
6+
const registeredWorker = crons.find((item) => item.name === cron.name);
7+
8+
expect(registeredWorker).toBeDefined();
9+
});
10+
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"dependencies": {
3232
"@apple/app-store-server-library": "^1.5.0",
3333
"@bufbuild/protobuf": "^1.10.0",
34+
"@clickhouse/client": "1.12.0",
3435
"@connectrpc/connect": "^1.6.1",
3536
"@connectrpc/connect-fastify": "^1.6.1",
3637
"@connectrpc/connect-node": "^1.6.1",

pnpm-lock.yaml

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/common/clickhouse.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { createClient } from '@clickhouse/client';
2+
3+
let client: ReturnType<typeof createClient> | null = null;
4+
5+
export const getClickHouseClient = () => {
6+
if (!client) {
7+
client = createClient({
8+
url: process.env.CLICKHOUSE_URL,
9+
username: process.env.CLICKHOUSE_USER,
10+
password: process.env.CLICKHOUSE_PASSWORD,
11+
});
12+
}
13+
14+
return client;
15+
};

src/common/schema/postAnalytics.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { z } from 'zod';
2+
3+
export const postAnalyticsClickhouseSchema = z
4+
.object({
5+
id: z.string(),
6+
updatedAt: z.coerce.date(),
7+
impressions: z.coerce.number().nonnegative(),
8+
reach: z.coerce.number().nonnegative(),
9+
bookmarks: z.coerce.number().nonnegative(),
10+
profileViews: z.coerce.number().nonnegative(),
11+
followers: z.coerce.number().nonnegative(),
12+
squadJoins: z.coerce.number().nonnegative(),
13+
sharesExternal: z.coerce.number().nonnegative(),
14+
sharesInternal: z.coerce.number().nonnegative(),
15+
})
16+
.strict();

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export enum StorageTopic {
3434
Njord = 'njord',
3535
Paddle = 'paddle',
3636
RedisCounter = 'redis_counter',
37+
Cron = 'cron',
3738
}
3839

3940
export enum StorageKey {

src/cron/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { cleanZombieUserCompany } from './cleanZombieUserCompany';
1919
import { calculateTopReaders } from './calculateTopReaders';
2020
import cleanGiftedPlus from './cleanGiftedPlus';
2121
import { cleanStaleUserTransactions } from './cleanStaleUserTransactions';
22+
import { postAnalyticsClickhouseCron } from './postAnalyticsClickhouse';
2223

2324
export const crons: Cron[] = [
2425
updateViews,
@@ -41,4 +42,5 @@ export const crons: Cron[] = [
4142
validateActiveUsers,
4243
cleanGiftedPlus,
4344
cleanStaleUserTransactions,
45+
postAnalyticsClickhouseCron,
4446
];

0 commit comments

Comments
 (0)