From 523359ec32e3fc8a8ede1fd394d73ec311979583 Mon Sep 17 00:00:00 2001 From: Pitu Date: Fri, 8 Jan 2021 20:55:57 +0900 Subject: chore: be able to refresh stats on demand --- src/api/routes/service/statsGET.js | 19 ++++++++++++++++++- src/site/pages/dashboard/admin/statistics.vue | 14 +++++++++++++- src/site/store/admin.js | 15 ++++++++++----- 3 files changed, 41 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/api/routes/service/statsGET.js b/src/api/routes/service/statsGET.js index 2241ca8..6d5197b 100644 --- a/src/api/routes/service/statsGET.js +++ b/src/api/routes/service/statsGET.js @@ -5,10 +5,27 @@ 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 }); + 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: true, + generatedOn: moment().format('MMMM Do YYYY, h:mm:ss a z'), // pg returns this as a date, sqlite3 returns an unix timestamp :< + type: StatsGenerator.Type.HIDDEN + } + } + } + }); + } + const cachedStats = await db('statistics') .select('type', 'data', 'batchId', 'createdAt') .where('batchId', '=', db('statistics').max('batchId')); diff --git a/src/site/pages/dashboard/admin/statistics.vue b/src/site/pages/dashboard/admin/statistics.vue index ecee102..c1e79fc 100644 --- a/src/site/pages/dashboard/admin/statistics.vue +++ b/src/site/pages/dashboard/admin/statistics.vue @@ -16,6 +16,10 @@

{{ category }} generated on {{ stats[category].meta.generatedOn }} +

@@ -84,7 +88,15 @@ export default { computed: mapState({ stats: state => state.admin.statistics }), - methods: {}, + methods: { + refresh(category) { + try { + this.$store.dispatch('admin/fetchStatistics', category); + } catch (error) { + this.$notifier.error(error.message); + } + } + }, head() { return { title: 'Service statistics' diff --git a/src/site/store/admin.js b/src/site/store/admin.js index b2d1926..0d0360b 100644 --- a/src/site/store/admin.js +++ b/src/site/store/admin.js @@ -22,9 +22,10 @@ export const actions = { return response; }, - async fetchStatistics({ commit }) { - const response = await this.$axios.$get('service/statistics'); - commit('setStatistics', response); + async fetchStatistics({ commit }, category) { + const url = category ? `service/statistics/${category}` : 'service/statistics'; + const response = await this.$axios.$get(url); + commit('setStatistics', { statistics: response.statistics, category: category }); return response; }, @@ -96,8 +97,12 @@ export const mutations = { setSettings(state, { config }) { state.settings = config; }, - setStatistics(state, { statistics }) { - state.statistics = statistics; + setStatistics(state, { statistics, category }) { + if (category) { + state.statistics[category] = statistics[category]; + } else { + state.statistics = statistics; + } }, setUsers(state, { users }) { state.users = users; -- cgit v1.2.3