-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename analytics and add ranking queries
- Loading branch information
Showing
6 changed files
with
98 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { prisma } from "../../config"; | ||
|
||
const topUsersByGuild = async (guild: string, limit: number) => { | ||
const topUsers = await prisma.$queryRaw` | ||
SELECT | ||
"userId", | ||
"telegramUsername", | ||
"firstName", | ||
"lastName", | ||
SUM("earnedPoints") as "totalPoints", | ||
COUNT(*) as "totalEntries" | ||
FROM | ||
"Entry" JOIN "User" ON "Entry"."userId" = "User"."telegramUserId" | ||
WHERE | ||
"guild" = ${guild} | ||
GROUP BY | ||
"userId", | ||
"telegramUsername", | ||
"firstName", | ||
"lastName" | ||
ORDER BY | ||
"totalPoints" DESC | ||
LIMIT | ||
${limit} | ||
`; | ||
return topUsers; | ||
} | ||
|
||
export default topUsersByGuild; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import express from 'express'; | ||
import { validatePeriod } from './middleware'; | ||
import { calculateGuildStatistics } from '../analytics/statistics'; | ||
import topUsersByGuild from '../analytics/rankings'; | ||
import { arrayToCSV } from '../common/utils'; | ||
|
||
const router = express.Router({mergeParams: true}); | ||
|
||
export interface StatisticsResponse extends express.Response { | ||
locals: { | ||
guild: string; | ||
periodStart: Date; | ||
periodEnd: Date; | ||
}; | ||
} | ||
|
||
router.get('/ranking/:guild', async (req, res: StatisticsResponse) => { | ||
if (req.query.pass !== process.env.ADMIN_PASSWORD) { | ||
console.log("Wrong password"); | ||
return res.status(401).send("Wrong password!"); | ||
} | ||
|
||
const guild = req.params.guild; | ||
if (!req.query.limit || ! ((typeof req.query.limit) === 'string')) { | ||
return res.status(400).send('Limit query parameter is required as a string'); | ||
} | ||
const limit = Number.parseInt(req.query.limit as string ?? '10'); | ||
try { | ||
const topUsers = await topUsersByGuild(guild, limit) as Record<string, unknown>[]; | ||
const csv = arrayToCSV([ | ||
"userId", | ||
"telegramUsername", | ||
"firstName", | ||
"lastName", | ||
"totalPoints", | ||
"totalEntries" | ||
], topUsers); | ||
res.header("Content-Type", "text/csv"); | ||
res.status(200).send(csv); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).send('An error occurred while calculating rankings'); | ||
} | ||
}); | ||
|
||
// Middleware to validate the period start and end query parameters | ||
router.use(validatePeriod); | ||
|
||
router.get('/statistics', async (req, res: StatisticsResponse) => { | ||
if (req.query.pass !== process.env.ADMIN_PASSWORD) { | ||
console.log("Wrong password"); | ||
return res.status(401).send("Wrong password!"); | ||
} | ||
|
||
const { periodStart, periodEnd } = res.locals; | ||
try { | ||
const statistics = await calculateGuildStatistics(periodStart, periodEnd); | ||
res.json(Object.fromEntries(statistics)); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).send('An error occurred while calculating statistics'); | ||
} | ||
}); | ||
|
||
|
||
export default router; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.