From 98cd543a8d4316971e9486797f986b9d6680e3cf Mon Sep 17 00:00:00 2001 From: Igor Morozov <24278723+morozoffnor@users.noreply.github.com> Date: Fri, 2 Feb 2024 02:09:03 +0300 Subject: [PATCH 1/2] most used names aggregation --- app/api/stats.mjs | 15 +++++++++++++++ app/database/database.mjs | 35 +++++++++++++++++++++++++++++++++++ app/main.mjs | 2 ++ 3 files changed, 52 insertions(+) create mode 100644 app/api/stats.mjs diff --git a/app/api/stats.mjs b/app/api/stats.mjs new file mode 100644 index 0000000..e415d6d --- /dev/null +++ b/app/api/stats.mjs @@ -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 diff --git a/app/database/database.mjs b/app/database/database.mjs index 8d2f1fd..b055178 100644 --- a/app/database/database.mjs +++ b/app/database/database.mjs @@ -316,4 +316,39 @@ async function checkForLvlUp(userid) { // send message here console.log('lvl up!') } +} + +// aggregations + +export async function getMostUsedNames(limit = 20, 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() } \ No newline at end of file diff --git a/app/main.mjs b/app/main.mjs index 3e2a8b6..4536f2e 100644 --- a/app/main.mjs +++ b/app/main.mjs @@ -25,6 +25,7 @@ import {textTriggersHandler} from "./tools/phraseTrigger.mjs"; import ItemsRouter from "./api/items.mjs" import {addItem} from "./commands/test/addItem.mjs"; import {items} from "./commands/items.mjs"; +import StatsRouter from "./api/stats.mjs"; // connect to DB await connect() @@ -38,6 +39,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) From 47a923139af0c2f478ac97ae5de5afa9edfe803b Mon Sep 17 00:00:00 2001 From: Igor Morozov <24278723+morozoffnor@users.noreply.github.com> Date: Mon, 5 Feb 2024 18:13:26 +0700 Subject: [PATCH 2/2] send all names --- app/database/database.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/database/database.mjs b/app/database/database.mjs index 062ba55..0b08e1a 100644 --- a/app/database/database.mjs +++ b/app/database/database.mjs @@ -321,7 +321,7 @@ async function checkForLvlUp(userid) { // aggregations -export async function getMostUsedNames(limit = 20, sort = -1) { +export async function getMostUsedNames(limit = 1000, sort = -1) { const names = Attempt.aggregate( [ {