aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/service/statsGET.js
blob: d6fa69e3f9756ca0c319e23b1b7b31c3f2853d2c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const Route = require('../../structures/Route');
const StatsGenerator = require('../../utils/StatsGenerator');

// Thank you Bobby for the stats code https://github.com/BobbyWibowo/lolisafe/blob/safe.fiery.me/controllers/utilsController.js
class filesGET extends Route {
	constructor() {
		super('/service/statistics', 'get', { adminOnly: true });
	}

	async run(req, res, db) {
		const cachedStats = await db('statistics')
			.select('type', 'data', 'batchId')
			.where('batchId', '=', db('statistics').max('batchId'));

		let stats = cachedStats.reduce((acc, { type, data }) => {
			try {
				acc[type] = JSON.parse(data);
			} catch (e) {
				console.error(e);
			}

			return acc;
		}, {});

		stats = { ...stats, ...(await StatsGenerator.getMissingStats(db, Object.keys(stats))) };

		return res.json(StatsGenerator.keyOrder.reduce((acc, k) => {
			acc[k] = stats[k];
			return acc;
		}, {}));
	}
}

module.exports = filesGET;