diff options
| author | Kana <[email protected]> | 2021-01-08 19:48:25 +0900 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-01-08 19:48:25 +0900 |
| commit | 3cfb2721ce64ab94fdbc9c97b54602acc3c654be (patch) | |
| tree | b427becf78c51081e58f1b2af98b2662f7626a39 /src/api/routes/service | |
| parent | Merge pull request #248 from WeebDev/feature/stats-dashboard (diff) | |
| parent | fix: pg driver doesn't return anything without .returning() (diff) | |
| download | host.fuwn.me-3cfb2721ce64ab94fdbc9c97b54602acc3c654be.tar.xz host.fuwn.me-3cfb2721ce64ab94fdbc9c97b54602acc3c654be.zip | |
Merge pull request #250 from Zephyrrus/feature/system_status_page
Feature/system status page
Diffstat (limited to 'src/api/routes/service')
| -rw-r--r-- | src/api/routes/service/statsGET.js | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/api/routes/service/statsGET.js b/src/api/routes/service/statsGET.js new file mode 100644 index 0000000..2241ca8 --- /dev/null +++ b/src/api/routes/service/statsGET.js @@ -0,0 +1,48 @@ +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', 'get', { adminOnly: true }); + } + + async run(req, res, db) { + 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; |