Add leaderboard for dearrow

This commit is contained in:
Ajay 2023-07-07 14:32:45 -04:00
parent d1d2b011f8
commit d030de83bd
2 changed files with 45 additions and 0 deletions

View file

@ -54,6 +54,7 @@ import { postBranding } from "./routes/postBranding";
import { cacheMiddlware } from "./middleware/etag";
import { hostHeader } from "./middleware/hostHeader";
import { getBrandingStats } from "./routes/getBrandingStats";
import { getTopBrandingUsers } from "./routes/getTopBrandingUsers";
export function createServer(callback: () => void): Server {
// Create a service (the app object is just a callback).
@ -141,6 +142,7 @@ function setupRoutes(router: Router) {
router.get("/api/getTopUsers", getTopUsers);
router.get("/api/getTopCategoryUsers", getTopCategoryUsers);
router.get("/api/getTopBrandingUsers", getTopBrandingUsers);
//send out totals
//send the total submissions, total views and total minutes saved

View file

@ -0,0 +1,43 @@
import { db } from "../databases/databases";
import { Request, Response } from "express";
async function generateTopUsersStats(sortBy: string) {
const rows = await db.prepare("all", `SELECT COUNT(distinct "titles"."UUID") as "titleCount", COUNT(distinct "thumbnails"."UUID") as "thumbnailCount", COALESCE("userName", "titles"."userID") as "userName"
FROM "titles"
LEFT JOIN "titleVotes" ON "titles"."UUID" = "titleVotes"."UUID"
LEFT JOIN "userNames" ON "titles"."userID"="userNames"."userID"
LEFT JOIN "thumbnails" ON "titles"."userID" = "thumbnails"."userID"
LEFT JOIN "thumbnailVotes" ON "thumbnails"."UUID" = "thumbnailVotes"."UUID"
WHERE "titleVotes"."votes" > -1 AND "titleVotes"."shadowHidden" != 1
GROUP BY COALESCE("userName", "titles"."userID") HAVING SUM("titleVotes"."votes") > 2 OR SUM("thumbnailVotes"."votes") > 2
ORDER BY "${sortBy}" DESC LIMIT 100`, []) as { titleCount: number, thumbnailCount: number, userName: string }[];
return rows.map((row) => ({
userName: row.userName,
titles: row.titleCount,
thumbnails: row.thumbnailCount
}));
}
export async function getTopBrandingUsers(req: Request, res: Response): Promise<Response> {
const sortType = parseInt(req.query.sortType as string);
let sortBy = "";
if (sortType == 0) {
sortBy = "titleCount";
} else if (sortType == 1) {
sortBy = "thumbnailCount";
} else {
//invalid request
return res.sendStatus(400);
}
if (db.highLoad()) {
return res.status(503).send("Disabled for load reasons");
}
const stats = await generateTopUsersStats(sortBy);
//send this result
return res.send(stats);
}