Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Names web #81

Merged
merged 3 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/api/stats.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import express from 'express'
import {tokenChecker} from "./tools/APItokenChecker.mjs";
import {getMostUsedNames} from "../database/database.mjs";

let StatsRouter = new express.Router()

StatsRouter.use(tokenChecker)

StatsRouter.get('/names', express.json({type: 'application/json'}), async (req, res) => {
const names = await getMostUsedNames()
res.setHeader('Content-Type', 'application/json');
res.status(200).send({names: names})
})

export default StatsRouter
35 changes: 35 additions & 0 deletions app/database/database.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,39 @@ async function checkForLvlUp(userid) {
// send message here
console.log('lvl up!')
}
}

// aggregations

export async function getMostUsedNames(limit = 1000, sort = -1) {
const names = Attempt.aggregate(
[
{
$sortByCount:
/**
* expression: Grouping expression or string.
*/

"$cockName",
},
{
$sort:
/**
* Provide any number of field/order pairs.
*/
{
count: sort,
},
},
{
$limit:
/**
* Provide the number of documents to limit.
*/
limit,
},
]
)

return await names.exec()
}
2 changes: 2 additions & 0 deletions app/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import ItemsRouter from "./api/items.mjs"
import {addItem} from "./commands/test/addItem.mjs";
import {items} from "./commands/items.mjs";
import {topUsers} from "./commands/top.mjs";
import StatsRouter from "./api/stats.mjs";

// connect to DB
await connect()
Expand All @@ -39,6 +40,7 @@ api.listen(port, () => {
})
api.use(tokenChecker)
api.use('/items/', ItemsRouter)
api.use('/stats/', StatsRouter)

api.get('/user/:id', async (req, res) => {
await apiGetUserById(req, res)
Expand Down
Loading