slight cache for analytics

This commit is contained in:
sky
2026-04-11 15:46:01 -04:00
parent a7d194631c
commit 7dd5fbe210

View File

@ -189,22 +189,32 @@ app.post("/api/analytics/game", async (req, res) => {
return; return;
}) })
let analyticsCache = null;
let analyticsCacheTime = 0;
const ANALYTICS_TTL = 10 * 1000;
app.use("/api/analytics/get", async(req, res) => { app.use("/api/analytics/get", async(req, res) => {
const now = Date.now();
if (analyticsCache && (now - analyticsCacheTime) < ANALYTICS_TTL) {
res.send(analyticsCache);
return;
}
const sql = ` const sql = `
SELECT SELECT
CASE CASE
WHEN game = 'nso_fix' THEN 'nso' WHEN game = 'nso_fix' THEN 'nso'
WHEN game = 'untitledgoosegamee' THEN 'untitledgoosegame' -- Fixes that typo too! WHEN game = 'untitledgoosegamee' THEN 'untitledgoosegame' -- Fixes that typo too!
ELSE game ELSE game
END AS name, END AS name,
SUM(plays) as plays SUM(plays) as plays
FROM stats FROM stats
GROUP BY name GROUP BY name
ORDER BY plays DESC ORDER BY plays DESC
`; `;
const query = top.prepare(sql); const query = top.prepare(sql);
let data = query.all(); analyticsCache = query.all();
res.send(data); analyticsCacheTime = now;
res.send(analyticsCache);
return; return;
}); });
app.use("/metrics", async(req, res) => { app.use("/metrics", async(req, res) => {
@ -259,21 +269,6 @@ app.use("/api/music/download", async (req, res, next) => {
} }
res.end(); res.end();
}); });
app.post("/api/recat", async (req, res) => {
if (isAdmin(req.cookies.token)) {
let domain = JSON.parse(req.body)["domain"];
let message;
if (recatters.includes(domain)) {
recatters.pop(domain);
message = `Successfully set ${domain} to Selenite.`
} else {
recatters.push(domain);
message = `Successfully set ${domain} as educational.`
}
fs.writeFile("./data/recats.json", JSON.stringify(recatters));
res.send(message);
};
})
// friends endpoints // friends endpoints
app.get("/api/friends/list", async (req, res) => { app.get("/api/friends/list", async (req, res) => {
@ -479,6 +474,6 @@ server.on("upgrade", (request, socket, head) => {
app.use(async (req, res) => { app.use(async (req, res) => {
res res
.type("text/html") .type("text/html")
.send(await fs.readFile(`./public/404.html`)) .status(404)
.status(404); .send(await fs.readFile(`./public/404.html`));
}); });