From ce6896ef86b88854040ef61225c4c0dba660da24 Mon Sep 17 00:00:00 2001 From: Jeff Steward Date: Fri, 7 Aug 2020 08:41:07 -0400 Subject: [PATCH] add a card of api stats --- modules/data.js | 33 ++++++++++++++++++++++++++++++++- routes/index.js | 12 +++++++++++- views/production.hbs | 9 +++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/modules/data.js b/modules/data.js index 16e2792..813491a 100644 --- a/modules/data.js +++ b/modules/data.js @@ -162,10 +162,41 @@ function getActivityStats(callback) { }); } +function getKeyStats(callback) { + const params = { + size: 0 + }; + const aggs = { + "date_stats": { + "extended_stats": { + "field": "createdat" + } + } + }; + const url = makeURL('key', params, aggs); + + fetch(url) + .then(response => response.json()) + .then(results => { + let output = { + keys: { + count: results.info.totalrecords, + statsdates: { + start: results.aggregations.date_stats.min_as_string, + end: results.aggregations.date_stats.max_as_string + }, + } + }; + + callback(null, output); + }); +} + module.exports = { getObjectStats: getObjectStats, getObjectsInGalleryStats: getObjectsInGalleryStats, getCurrentExhibitions: getCurrentExhibitions, getAltTextStats: getAltTextStats, - getActivityStats: getActivityStats + getActivityStats: getActivityStats, + getKeyStats: getKeyStats }; \ No newline at end of file diff --git a/routes/index.js b/routes/index.js index 88aa0b6..4211767 100644 --- a/routes/index.js +++ b/routes/index.js @@ -10,6 +10,11 @@ let data = { datafreshness: 0, dateoflastrefresh: "2000-01-01", dateoflastexport: "2000-01-01", + keys: { + count: 0, + count_as_string: 0, + statsdates: {} + }, objects: { count: 0, public: { @@ -44,7 +49,8 @@ router.get('/', function(req, res, next) { currentExhibitions: stats.getCurrentExhibitions, alttextStats: stats.getAltTextStats, objectsOnViewStats: stats.getObjectsInGalleryStats, - activityStats: stats.getActivityStats + activityStats: stats.getActivityStats, + keyStats: stats.getKeyStats }, function(err, results) { data.dateoflastrefresh = results['objectStats']['lastrefresh']; @@ -63,6 +69,10 @@ router.get('/', function(req, res, next) { data.pageviews = results['activityStats']['pageviews']; data.pageviews.objects.count_as_string = data.pageviews.objects.count.toLocaleString('en'); data.pageviews.objects.count_as_percent = ((data.pageviews.objects.count/data.objects.public.count)*100).toFixed(2) + data.keys.count = results['keyStats']['keys']['count']; + data.keys.count_as_string = data.keys.count.toLocaleString('en'); + data.keys.statsdates = results['keyStats']['keys']['statsdates']; + data.keys.statsdates.start_short = data.keys.statsdates.start.substr(0, 10); // calculate the age of the data // freshness = number of hours old diff --git a/views/production.hbs b/views/production.hbs index 04ceef8..4121a11 100644 --- a/views/production.hbs +++ b/views/production.hbs @@ -27,6 +27,15 @@ +
+
+

API Statistics

+ +

{{apistats.keys.count_as_string}} keys have been issued since {{apistats.keys.statsdates.start_short}}.

+

The newest key was cut at precisely {{apistats.keys.statsdates.end}}.

+ +
+