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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
const Route = require('../../structures/Route');
const StatsGenerator = require('../../utils/StatsGenerator');
const moment = require('moment');
// 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/:category?', 'get', { adminOnly: true });
}
async run(req, res, db) {
const { category } = req.params;
if (category) {
const dbRes = await StatsGenerator.statGenerators[category](db);
return res.json({
statistics: {
[category]: {
...dbRes,
meta: {
cached: false,
generatedOn: moment().format('MMMM Do YYYY, h:mm:ss a z'),
type: StatsGenerator.Type.HIDDEN
}
}
}
});
}
const cachedStats = await db('statistics')
.select('type', 'data', 'batchId', 'createdAt')
.where('batchId', '=', db('statistics').max('batchId'));
let stats = cachedStats.reduce((acc, { type, data, createdAt }) => {
try {
// pg returns json, sqlite retuns a string...
if (typeof data === 'string' || data instanceof String) {
acc[type] = JSON.parse(data);
} else {
acc[type] = data;
}
acc[type].meta = {
cached: true,
generatedOn: moment(createdAt).format('MMMM Do YYYY, h:mm:ss a z'), // pg returns this as a date, sqlite3 returns an unix timestamp :<
type: StatsGenerator.Type.HIDDEN
};
} catch (e) {
console.error(e);
}
return acc;
}, {});
stats = { ...stats, ...(await StatsGenerator.getMissingStats(db, Object.keys(stats))) };
const ordered = StatsGenerator.keyOrder.reduce((acc, k) => {
acc[k] = stats[k];
return acc;
}, {});
return res.json({ statistics: ordered });
}
}
module.exports = filesGET;
|