forked from pinks/eris
1
0
Fork 0
eris/api/statsRoute.ts

97 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-10-10 16:21:25 +00:00
// deno-lint-ignore-file require-await
2023-10-09 19:03:31 +00:00
import { createEndpoint, createMethodFilter, createPathFilter } from "t_rest/server";
import { liveGlobalStats } from "../app/globalStatsStore.ts";
import { getDailyStats } from "../app/dailyStatsStore.ts";
2023-10-10 16:21:25 +00:00
import { getUserStats } from "../app/userStatsStore.ts";
import { getUserDailyStats } from "../app/userDailyStatsStore.ts";
2023-10-09 19:03:31 +00:00
export const statsRoute = createPathFilter({
2023-10-10 16:21:25 +00:00
"": createMethodFilter({
2023-10-09 19:03:31 +00:00
GET: createEndpoint(
{ query: null, body: null },
async () => {
2023-10-10 16:21:25 +00:00
const stats = liveGlobalStats;
2023-10-09 19:03:31 +00:00
return {
status: 200,
body: {
type: "application/json",
2023-10-10 16:21:25 +00:00
data: {
imageCount: stats.imageCount,
pixelCount: stats.pixelCount,
userCount: stats.userIds.length,
timestamp: stats.timestamp,
},
2023-10-09 19:03:31 +00:00
},
};
},
),
}),
"daily/{year}/{month}/{day}": createMethodFilter({
GET: createEndpoint(
{ query: null, body: null },
async ({ params }) => {
const year = Number(params.year);
const month = Number(params.month);
const day = Number(params.day);
2023-10-10 16:21:25 +00:00
const stats = await getDailyStats(year, month, day);
2023-10-09 19:03:31 +00:00
return {
status: 200,
body: {
type: "application/json",
2023-10-10 16:21:25 +00:00
data: {
imageCount: stats.imageCount,
pixelCount: stats.pixelCount,
userCount: stats.userIds.length,
timestamp: stats.timestamp,
},
},
};
},
),
}),
"users/{userId}": createMethodFilter({
GET: createEndpoint(
{ query: null, body: null },
async ({ params }) => {
const userId = Number(params.userId);
const stats = await getUserStats(userId);
return {
status: 200,
body: {
type: "application/json",
data: {
imageCount: stats.imageCount,
pixelCount: stats.pixelCount,
tagCountMap: stats.tagCountMap,
timestamp: stats.timestamp,
},
},
};
},
),
}),
"users/{userId}/daily/{year}/{month}/{day}": createMethodFilter({
GET: createEndpoint(
{ query: null, body: null },
async ({ params }) => {
const userId = Number(params.userId);
const year = Number(params.year);
const month = Number(params.month);
const day = Number(params.day);
const stats = await getUserDailyStats(userId, year, month, day);
return {
status: 200,
body: {
type: "application/json",
data: {
imageCount: stats.imageCount,
pixelCount: stats.pixelCount,
timestamp: stats.timestamp,
},
2023-10-09 19:03:31 +00:00
},
};
},
),
}),
});