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/utils/Util.js | |
| 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/utils/Util.js')
| -rw-r--r-- | src/api/utils/Util.js | 65 |
1 files changed, 50 insertions, 15 deletions
diff --git a/src/api/utils/Util.js b/src/api/utils/Util.js index ae13eb5..6feedd4 100644 --- a/src/api/utils/Util.js +++ b/src/api/utils/Util.js @@ -3,27 +3,20 @@ const jetpack = require('fs-jetpack'); const randomstring = require('randomstring'); const path = require('path'); const JWT = require('jsonwebtoken'); -const db = require('knex')({ - client: process.env.DB_CLIENT, - connection: { - host: process.env.DB_HOST, - user: process.env.DB_USER, - password: process.env.DB_PASSWORD, - database: process.env.DB_DATABASE, - filename: path.join(__dirname, '../../../database/database.sqlite') - }, - useNullAsDefault: process.env.DB_CLIENT === 'sqlite' -}); +const db = require('../structures/Database'); const moment = require('moment'); const Zip = require('adm-zip'); const uuidv4 = require('uuid/v4'); const log = require('./Log'); const ThumbUtil = require('./ThumbUtil'); +const StatsGenerator = require('./StatsGenerator'); const blockedExtensions = process.env.BLOCKED_EXTENSIONS.split(','); const preserveExtensions = ['.tar.gz', '.tar.z', '.tar.bz2', '.tar.lzma', '.tar.lzo', '.tar.xz']; +let statsLastSavedTime = null; + class Util { static uploadPath = path.join(__dirname, '../../../', process.env.UPLOAD_FOLDER); @@ -35,6 +28,10 @@ class Util { return blockedExtensions.includes(extension); } + static getMimeFromType(fileTypeMimeObj) { + return fileTypeMimeObj ? fileTypeMimeObj.mime : undefined; + } + static constructFilePublicLink(file) { /* TODO: This wont work without a reverse proxy serving both @@ -102,7 +99,8 @@ class Util { await db .table('files') .where('name', filename) - .delete(); + .delete() + .wasMutated(); } } catch (error) { log.error(`There was an error removing the file < ${filename} >`); @@ -225,6 +223,7 @@ class Util { static async storeFileToDb(req, res, user, file, db) { const dbFile = await db.table('files') + // eslint-disable-next-line func-names .where(function() { if (user === undefined) { this.whereNull('userId'); @@ -259,9 +258,9 @@ class Util { let fileId; if (process.env.DB_CLIENT === 'sqlite3') { - fileId = await db.table('files').insert(data); + fileId = await db.table('files').insert(data).wasMutated(); } else { - fileId = await db.table('files').insert(data, 'id'); + fileId = await db.table('files').insert(data, 'id').wasMutated(); } return { @@ -275,7 +274,7 @@ class Util { const now = moment.utc().toDate(); try { - await db.table('albumsFiles').insert({ albumId, fileId: insertedId[0] }); + await db.table('albumsFiles').insert({ albumId, fileId: insertedId[0] }).wasMutated(); await db.table('albums').where('id', albumId).update('editedAt', now); } catch (error) { console.error(error); @@ -311,6 +310,42 @@ class Util { return extname + multi; } + + // 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. + if (!force && + (!db.userParams.lastMutationTime || + (statsLastSavedTime && statsLastSavedTime > db.userParams.lastMutationTime) + ) + ) { + return; + } + + const now = moment.utc().toDate(); + const stats = await StatsGenerator.getStats(db); + + let batchId = 1; + + const res = (await db('statistics').max({ lastBatch: 'batchId' }))[0]; + if (res && res.lastBatch) { + batchId = res.lastBatch + 1; + } + + try { + for (const [type, data] of Object.entries(stats)) { + await db.table('statistics').insert({ type, data: JSON.stringify(data), createdAt: now, batchId }); + } + + statsLastSavedTime = now.getTime(); + } catch (error) { + console.error(error); + } + } } module.exports = Util; |