diff --git a/package-lock.json b/package-lock.json index 46240e3..413a404 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2450,9 +2450,9 @@ } }, "node_modules/find-my-way": { - "version": "8.2.0", - "resolved": "https://registry.npmjs.org/find-my-way/-/find-my-way-8.2.0.tgz", - "integrity": "sha512-HdWXgFYc6b1BJcOBDBwjqWuHJj1WYiqrxSh25qtU4DabpMFdj/gSunNBQb83t+8Zt67D7CXEzJWTkxaShMTMOA==", + "version": "8.2.2", + "resolved": "https://registry.npmjs.org/find-my-way/-/find-my-way-8.2.2.tgz", + "integrity": "sha512-Dobi7gcTEq8yszimcfp/R7+owiT4WncAJ7VTTgFH1jYJ5GaG1FbhjwDG820hptN0QDFvzVY3RfCzdInvGPGzjA==", "dependencies": { "fast-deep-equal": "^3.1.3", "fast-querystring": "^1.0.0", @@ -3820,9 +3820,9 @@ "dev": true }, "node_modules/path-to-regexp": { - "version": "6.2.2", - "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-6.2.2.tgz", - "integrity": "sha512-GQX3SSMokngb36+whdpRXE+3f9V8UzyAorlYvOGx87ufGHehNTn5lCxrKtLyZ4Yl/wEKnNnr98ZzOwwDZV5ogw==" + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-6.3.0.tgz", + "integrity": "sha512-Yhpw4T9C6hPpgPeA28us07OJeqZ5EzQTkbfwuhsUg0c237RomFoETJgmp2sa3F/41gfLE6G5cqcYwznmeEeOlQ==" }, "node_modules/path-type": { "version": "4.0.0", diff --git a/src/server.ts b/src/server.ts index 07cccf0..1feeb06 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2,6 +2,7 @@ import { getMissedSearchRank, getPostViewRank, getPostViewsBulk, + getPostViewsUnique, getSearchRank, getViewCount, logMissedSearch, @@ -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; + + 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; let posts: Array; diff --git a/src/track.ts b/src/track.ts index c497e17..90e2876 100644 --- a/src/track.ts +++ b/src/track.ts @@ -41,6 +41,16 @@ export async function getPostViewRank(dates: Array, 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> { + 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, date?: string): Promise> { 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`,