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: migrate feeds to use clickhouse client #3963

Merged
merged 1 commit into from
Nov 6, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/workers/feeds/.dev.vars.example
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CLICKHOUSE_REST_ENDPOINT=""
CLICKHOUSE_PASSWORD=""
1 change: 1 addition & 0 deletions packages/workers/feeds/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"worker:deploy": "wrangler deploy --var RELEASE:\"$(git rev-parse HEAD)\""
},
"dependencies": {
"@hey/clickhouse": "workspace:*",
"@hey/data": "workspace:*",
"@hey/lib": "workspace:*",
"itty-router": "^4.0.23",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import createClickhouseClient from '@hey/clickhouse/createClickhouseClient';
import { Errors } from '@hey/data/errors';
import { PUBLICATION } from '@hey/data/tracking';

import randomizeIds from '../../../helpers/randomizeIds';
import type { Env } from '../../../types';
import clickhouseQuery from '../clickhouseQuery';

const interactionAndWeights = {
[PUBLICATION.COLLECT_MODULE.COLLECT]: 10,
Expand Down Expand Up @@ -38,32 +38,41 @@ const heyMostInteracted = async (
}

try {
const query = `
SELECT
const client = createClickhouseClient(env.CLICKHOUSE_PASSWORD);
const rows = await client.query({
query: `
SELECT
JSONExtractString(properties, 'publication_id') AS publication_id,
SUM(CASE
${generateWeightedCaseStatement()}
ELSE 0
END) AS weighted_interaction_count
FROM
events
WHERE
FROM
events
WHERE
name IN (${interactionEvents.map((name) => `'${name}'`).join(',')})
AND JSONHas(properties, 'publication_id')
AND created >= now() - INTERVAL 1 DAY
GROUP BY
GROUP BY
publication_id
HAVING
HAVING
publication_id IS NOT NULL
AND
AND
publication_id != ''
ORDER BY
ORDER BY
weighted_interaction_count DESC
LIMIT ${limit}
OFFSET ${offset};
`;
const response = await clickhouseQuery(query, env);
const ids = response.map((row) => row[0]);
LIMIT ${limit}
OFFSET ${offset};
`,
format: 'JSONEachRow'
});

const result =
await rows.json<
Array<{ publication_id: string; weighted_interaction_count: number }>
>();

const ids = result.map((row) => row.publication_id);

return randomizeIds(ids);
} catch {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import createClickhouseClient from '@hey/clickhouse/createClickhouseClient';
import { Errors } from '@hey/data/errors';
import { PAGEVIEW } from '@hey/data/tracking';

import randomizeIds from '../../../helpers/randomizeIds';
import removeParamsFromString from '../../../helpers/removeParamsFromString';
import type { Env } from '../../../types';
import clickhouseQuery from '../clickhouseQuery';

const heyMostViewed = async (
limit: number,
Expand All @@ -16,28 +16,34 @@ const heyMostViewed = async (
}

try {
const query = `
SELECT
const client = createClickhouseClient(env.CLICKHOUSE_PASSWORD);
const rows = await client.query({
query: `
SELECT
url,
COUNT(*) AS view_count
FROM
FROM
events
WHERE
WHERE
name = '${PAGEVIEW}'
AND url LIKE 'https://hey.xyz/posts/%'
AND created >= now() - INTERVAL 1 DAY
GROUP BY
GROUP BY
url
ORDER BY
ORDER BY
view_count DESC
LIMIT ${limit}
OFFSET ${offset};
`;
const response = await clickhouseQuery(query, env);

const ids = response.map((row) => {
const url = row[0];
const id = url.split('/').pop();
LIMIT ${limit}
OFFSET ${offset};
`,
format: 'JSONEachRow'
});

const result =
await rows.json<Array<{ url: string; view_count: number }>>();

const ids = result.map((row) => {
const { url } = row;
const id = url.split('/').pop() || '';

return removeParamsFromString(id);
});
Expand Down
27 changes: 0 additions & 27 deletions packages/workers/feeds/src/providers/hey/clickhouseQuery.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/workers/feeds/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { IRequestStrict } from 'itty-router';

export interface Env {
RELEASE: string;
CLICKHOUSE_REST_ENDPOINT: string;
CLICKHOUSE_PASSWORD: string;
}

export type WorkerRequest = {
Expand Down
2 changes: 1 addition & 1 deletion packages/workers/feeds/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ routes = [

[env.production.vars]
RELEASE = ""
CLICKHOUSE_REST_ENDPOINT = ""
CLICKHOUSE_PASSWORD = ""
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.