From b3df1dd7a653909fb3f18ed7c0c91ffa347480fb Mon Sep 17 00:00:00 2001 From: Zephyrrus Date: Thu, 7 Jan 2021 21:36:56 +0200 Subject: feat: change mutation timestamp on every database mutation --- src/api/routes/service/statsGET.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/api/routes/service/statsGET.js (limited to 'src/api/routes/service') diff --git a/src/api/routes/service/statsGET.js b/src/api/routes/service/statsGET.js new file mode 100644 index 0000000..d6fa69e --- /dev/null +++ b/src/api/routes/service/statsGET.js @@ -0,0 +1,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; -- cgit v1.2.3 From 925080f6a08a1f1515143db1bd6aef8109f5fb67 Mon Sep 17 00:00:00 2001 From: Zephyrrus Date: Thu, 7 Jan 2021 23:55:37 +0200 Subject: chore: refactor stats generator to use an enum for types fix: saved data not being converted to JSON automatically when using SQLite (and possibly MSSQL) feat: display when a stat was generated in the section's header fix: not being able to click through the footer (sorry IE11 users, you still won't be able to click through it) fix: add is-mobile to columns so they don't stack on top of each other feat: add text next to the NSFW toggle and make it look like the elements around it (begone round iOS edges) --- src/api/routes/service/statsGET.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'src/api/routes/service') diff --git a/src/api/routes/service/statsGET.js b/src/api/routes/service/statsGET.js index d6fa69e..2241ca8 100644 --- a/src/api/routes/service/statsGET.js +++ b/src/api/routes/service/statsGET.js @@ -1,5 +1,6 @@ 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 { @@ -9,12 +10,23 @@ class filesGET extends Route { async run(req, res, db) { const cachedStats = await db('statistics') - .select('type', 'data', 'batchId') + .select('type', 'data', 'batchId', 'createdAt') .where('batchId', '=', db('statistics').max('batchId')); - let stats = cachedStats.reduce((acc, { type, data }) => { + let stats = cachedStats.reduce((acc, { type, data, createdAt }) => { try { - acc[type] = JSON.parse(data); + // 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); } @@ -24,10 +36,12 @@ class filesGET extends Route { stats = { ...stats, ...(await StatsGenerator.getMissingStats(db, Object.keys(stats))) }; - return res.json(StatsGenerator.keyOrder.reduce((acc, k) => { + const ordered = StatsGenerator.keyOrder.reduce((acc, k) => { acc[k] = stats[k]; return acc; - }, {})); + }, {}); + + return res.json({ statistics: ordered }); } } -- cgit v1.2.3