1
0
cvsa/packages/backend/routes/song/milestone.ts

64 lines
1.8 KiB
TypeScript

import { Elysia, t } from "elysia";
import { db, bilibiliMetadata, eta } from "@core/drizzle";
import { eq, and, gte, lt } from "drizzle-orm";
import serverTiming from "@backend/middlewares/timing";
import z from "zod";
import { BiliVideoSchema } from "@backend/lib/schema";
type MileStoneType = "dendou" | "densetsu" | "shinwa";
const range = {
dendou: [0, 100000, 2160],
densetsu: [100000, 1000000, 8760],
shinwa: [1000000, 10000000, 43800]
};
export const closeMileStoneHandler = new Elysia({ prefix: "/songs" }).use(serverTiming()).get(
"/close-milestone/:type",
async ({ params, timeLog }) => {
timeLog.startTime("retrieveCandidates");
const type = params.type;
const min = range[type as MileStoneType][0];
const max = range[type as MileStoneType][1];
return db
.select()
.from(eta)
.innerJoin(bilibiliMetadata, eq(bilibiliMetadata.aid, eta.aid))
.where(
and(
gte(eta.currentViews, min),
lt(eta.currentViews, max),
lt(eta.eta, range[type as MileStoneType][2])
)
)
.orderBy(eta.eta);
},
{
response: {
200: z.array(
z.object({
eta: z.object({
aid: z.number(),
eta: z.number(),
speed: z.number(),
currentViews: z.number(),
updatedAt: z.string()
}),
bilibili_metadata: BiliVideoSchema
})
),
404: t.Object({
message: t.String()
})
},
detail: {
summary: "Get songs close to milestones",
description:
"This endpoint retrieves songs that are approaching significant view count milestones. \
It supports three milestone types: 'dendou' (0-100k views), 'densetsu' (100k-1M views), and 'shinwa' (1M-10M views). \
For each type, it returns videos that are within the specified view range and have an estimated time to reach \
the next milestone below the threshold. Results are ordered by estimated time to milestone."
}
}
);