JaBa/dashboard/routes/guild-stats.js

112 lines
3 KiB
JavaScript
Raw Normal View History

2022-01-04 02:18:28 +05:00
const express = require("express"),
utils = require("../utils"),
CheckAuth = require("../auth/CheckAuth"),
router = express.Router(),
generator = require("colors-generator");
router.get("/:serverID", CheckAuth, async(req, res) => {
// Check if the user has the permissions to edit this guild
const guild = req.client.guilds.cache.get(req.params.serverID);
if (!guild || !req.userInfos.displayedGuilds || !req.userInfos.displayedGuilds.find((g) => g.id === req.params.serverID)) {
return res.render("404", {
user: req.userInfos,
language: req.language,
currentURL: `${req.client.config.dashboard.baseURL}/${req.originalUrl}`
});
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
// Fetch guild informations
const guildInfos = await utils.fetchGuild(guild.id, req.client, req.user.guilds);
const membersData = await req.client.membersData.find({ guildID: guild.id }).lean();
const leaderboards = {
money: sortArrayOfObjects("money", membersData),
level: sortArrayOfObjects("level", membersData)
};
for (const cat in leaderboards) {
const e = leaderboards[cat];
if (e.length > 10) e.length = 10;
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
const stats = {
money: await utils.fetchUsers(leaderboards.money, req.client),
level: await utils.fetchUsers(leaderboards.level, req.client)
};
res.render("stats/guild", {
stats,
commands: getCommands(guildInfos.commands.filter((c) => c.date > Date.now()-604800000)),
commandsUsage: getCommandsUsage(guildInfos.commands),
user: req.userInfos,
language: req.language,
currentURL: `${req.client.config.dashboard.baseURL}/${req.originalUrl}`,
});
});
module.exports = router;
function getCommands(commands) {
const aDateCommand = {};
commands.forEach((cmd) => {
const tDate = formatDate(new Date(cmd.date));
if (aDateCommand[tDate]) aDateCommand[tDate]++;
else aDateCommand[tDate] = 1;
});
return aDateCommand;
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
function getCommandsUsage(commands) {
const objectCount = commands.reduce((acc, curr) => {
if (typeof acc[curr.command] == "undefined") acc[curr.command] = 1;
else acc[curr.command] += 1;
return acc;
}, {});
const percentages = getPercentagePerKey(objectCount); // [ { key: "help", percentage: 20 } ]
const colors = generator.generate("#86bff2", percentages.length).get();
let i = 0;
percentages.forEach((p) => {
p.color = colors[i];
i++;
});
return percentages;
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
function getPercentagePerKey(myArray) {
const sum = getSum(myArray);
const arrayWithPercentages = [];
for (const key in myArray) {
const val = myArray[key];
const percentage = Math.round((val / sum) * 100);
arrayWithPercentages.push({key, percentage});
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
return arrayWithPercentages;
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
function getSum(myArray) {
let sum = 0;
for (const key in myArray) sum += myArray[key];
return sum;
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
function sortArrayOfObjects(key, arr) {
const array = arr.slice(0);
return array.sort((a, b) => {
return b[key] - a[key];
});
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
function formatDate(date) {
let dd = date.getDate();
let mm = date.getMonth() + 1;
2022-01-13 00:26:23 +05:00
if (dd < 10) dd = `0${dd}`;
if (mm < 10) mm = `0${mm}`;
date = `${mm}/${dd}`;
2022-01-04 02:18:28 +05:00
return date;
2022-01-13 00:26:23 +05:00
}