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/utils/StatsGenerator.js | 34 +++++++++++++++++++++++++--------- src/api/utils/Util.js | 13 ++++++++++--- 2 files changed, 35 insertions(+), 12 deletions(-) (limited to 'src/api/utils') diff --git a/src/api/utils/StatsGenerator.js b/src/api/utils/StatsGenerator.js index 2e48f32..ce73cd2 100644 --- a/src/api/utils/StatsGenerator.js +++ b/src/api/utils/StatsGenerator.js @@ -1,6 +1,22 @@ const si = require('systeminformation'); class StatsGenerator { + // symbols would be better because they're unique, but harder to serialize them + static Type = Object.freeze({ + // should contain key value: number + TIME: 'time', + // should contain key value: number + BYTE: 'byte', + // should contain key value: { used: number, total: number } + BYTE_USAGE: 'byteUsage', + // should contain key data: Array<{ key: string, value: number | string }> + // and optionally a count/total + DETAILED: 'detailed', + // hidden type should be skipped during iteration, can contain anything + // these should be treated on a case by case basis on the frontend + HIDDEN: 'hidden' + }); + static statGenerators = { system: StatsGenerator.getSystemInfo, fileSystems: StatsGenerator.getFileSystemsInfo, @@ -30,20 +46,20 @@ class StatsGenerator { used: mem.active, total: mem.total }, - type: 'byteUsage' + type: StatsGenerator.Type.BYTE_USAGE }, 'Memory Usage': { value: process.memoryUsage().rss, - type: 'byte' + type: StatsGenerator.Type.BYTE }, 'System Uptime': { value: time.uptime, - type: 'time' + type: StatsGenerator.Type.TIME }, 'Node.js': `${process.versions.node}`, 'Service Uptime': { value: Math.floor(nodeUptime), - type: 'time' + type: StatsGenerator.Type.TIME } }; } @@ -58,7 +74,7 @@ class StatsGenerator { total: fs.size, used: fs.used }, - type: 'byteUsage' + type: StatsGenerator.Type.BYTE_USAGE }; } @@ -73,11 +89,11 @@ class StatsGenerator { 'Others': { data: {}, count: 0, - type: 'detailed' + type: StatsGenerator.Type.DETAILED }, 'Size in DB': { value: 0, - type: 'byte' + type: StatsGenerator.Type.BYTE } }; @@ -88,7 +104,7 @@ class StatsGenerator { 'Total': uploads.length, 'Size in DB': { value: uploads.reduce((acc, upload) => acc + parseInt(upload.size, 10), 0), - type: 'byte' + type: StatsGenerator.Type.BYTE } }; }; @@ -127,7 +143,7 @@ class StatsGenerator { Others: { data, count, - type: 'detailed' + type: StatsGenerator.Type.DETAILED } }; }; diff --git a/src/api/utils/Util.js b/src/api/utils/Util.js index 658ac61..6feedd4 100644 --- a/src/api/utils/Util.js +++ b/src/api/utils/Util.js @@ -311,11 +311,18 @@ class Util { return extname + multi; } - static async saveStatsToDb() { + // TODO: Allow choosing what to save to db and what stats we care about in general + // TODO: if a stat is not saved to db but selected to be shows on the dashboard, it will be generated during the request + static async saveStatsToDb(force) { + // If there were no changes since the instance started, don't generate new stats + // OR // if we alredy saved a stats to the db, and there were no new changes to the db since then // skip generating and saving new stats. - // XXX: Should we save non-db related statistics to the database anyway? (like performance, disk usage) - if (statsLastSavedTime && statsLastSavedTime > db.userParams.lastMutationTime) { + if (!force && + (!db.userParams.lastMutationTime || + (statsLastSavedTime && statsLastSavedTime > db.userParams.lastMutationTime) + ) + ) { return; } -- cgit v1.2.3