From 99989e66aa60324a98d35468215267b97bca9184 Mon Sep 17 00:00:00 2001 From: Ben Grant Date: Wed, 16 Jun 2021 01:03:56 +1000 Subject: [PATCH] New stat retrieval and event store last visited date --- crabfit-backend/index.js | 1 + crabfit-backend/routes/getEvent.js | 6 ++++++ crabfit-backend/routes/stats.js | 25 +++++++++++++++---------- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/crabfit-backend/index.js b/crabfit-backend/index.js index 91b2c0b..484b472 100644 --- a/crabfit-backend/index.js +++ b/crabfit-backend/index.js @@ -30,6 +30,7 @@ app.use((req, res, next) => { req.types = { event: process.env.NODE_ENV === 'production' ? 'Event' : 'DevEvent', person: process.env.NODE_ENV === 'production' ? 'Person' : 'DevPerson', + stats: process.env.NODE_ENV === 'production' ? 'Stats' : 'DevStats', }; next(); }); diff --git a/crabfit-backend/routes/getEvent.js b/crabfit-backend/routes/getEvent.js index b3a464d..2d7d104 100644 --- a/crabfit-backend/routes/getEvent.js +++ b/crabfit-backend/routes/getEvent.js @@ -1,3 +1,5 @@ +const dayjs = require('dayjs'); + module.exports = async (req, res) => { const { eventId } = req.params; @@ -9,6 +11,10 @@ module.exports = async (req, res) => { id: eventId, ...event, }); + + // Update last visited time + event.visited = dayjs().unix(); + await req.datastore.upsert(event); } else { res.sendStatus(404); } diff --git a/crabfit-backend/routes/stats.js b/crabfit-backend/routes/stats.js index 35d405a..a7e59e2 100644 --- a/crabfit-backend/routes/stats.js +++ b/crabfit-backend/routes/stats.js @@ -4,19 +4,24 @@ module.exports = async (req, res) => { let eventCount = null; let personCount = null; - try { - const eventQuery = req.datastore.createQuery(['__Stat_Kind__']).filter('kind_name', req.types.event); - const personQuery = req.datastore.createQuery(['__Stat_Kind__']).filter('kind_name', req.types.person); + try { + const eventResult = (await req.datastore.get(req.datastore.key([req.types.stats, 'eventCount'])))[0] || null; + const personResult = (await req.datastore.get(req.datastore.key([req.types.stats, 'personCount'])))[0] || null; + + if (eventResult) { + eventCount = eventResult.value; + } + if (personResult) { + personCount = personResult.value; + } - eventCount = (await req.datastore.runQuery(eventQuery))[0][0].count; - personCount = (await req.datastore.runQuery(personQuery))[0][0].count; } catch (e) { console.error(e); } - res.send({ - eventCount: eventCount || null, - personCount: personCount || null, - version: package.version, - }); + res.send({ + eventCount, + personCount, + version: package.version, + }); };