Skip to content

Commit

Permalink
Add unique rank route
Browse files Browse the repository at this point in the history
  • Loading branch information
DonovanDMC committed Sep 26, 2024
1 parent 823862b commit 9e8d365
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

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

9 changes: 9 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
getMissedSearchRank,
getPostViewRank,
getPostViewsBulk,
getPostViewsUnique,
getSearchRank,
getViewCount,
logMissedSearch,
Expand Down Expand Up @@ -77,6 +78,14 @@ app.get("/views/rank", async (request, reply) => {
return reply.status(200).send({ data: await getPostViewRank(dates, limit), success: true });
});

app.get("/views/rank/unique", async (request, reply) => {
const qparams = request.query as Record<string, unknown>;

const limit = qparams.limit ? Number(qparams.limit) : 50;
const res = await getPostViewsUnique(limit);
return reply.status(200).send({ data: res, success: true });
});

app.get("/views/bulk", async (request, reply) => {
const qparams = request.query as Record<string, unknown>;
let posts: Array<number>;
Expand Down
10 changes: 10 additions & 0 deletions src/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ export async function getPostViewRank(dates: Array<string>, limit = 50): Promise
return (await r.json<{ post_id: string; view_count: string; }>()).data.map(v => ({ post: Number(v.post_id), count: Number(v.view_count) }));
}

export async function getPostViewsUnique(limit = 50): Promise<Array<{ count: number; post: number; }>> {
const r = await client.query({
query: "SELECT post_id, COUNT(DISTINCT ip_address) as view_count FROM post_views GROUP BY post_id ORDER BY view_count DESC LIMIT {limit:UInt64}",
query_params: { limit },
format: "JSON"
});

return (await r.json<{ post_id: string; view_count: string; }>()).data.map(v => ({ post: Number(v.post_id), count: Number(v.view_count) }));
}

export async function getPostViewsBulk(posts: Array<number>, date?: string): Promise<Array<{ count: number; post: number; }>> {
const r = await client.query({
query: `SELECT post_id, COUNT(*) as view_count FROM post_views WHERE post_id IN ({posts:Array(UInt64)})${date ? " AND date = {date:Date}" : ""} GROUP BY post_id LIMIT 100`,
Expand Down

0 comments on commit 9e8d365

Please sign in to comment.