diff --git a/modules/data.js b/modules/data.js index df73fee..5fe0b88 100644 --- a/modules/data.js +++ b/modules/data.js @@ -1,6 +1,7 @@ const fetch = require('node-fetch'); const querystring = require('querystring'); const { response } = require('express'); +const { DateTime } = require('luxon'); let charts; import('./charts.mjs').then(c => {charts = c;}) @@ -263,6 +264,87 @@ function getActivityStats(callback) { }); } +function getFiveByFiveStats(callback) { + let output = {}; + + const lastWeek = DateTime.now().minus({weeks: 1}); + const startDate = DateTime.now().minus({weeks: 5}).startOf('week'); + const endDate = DateTime.now().minus({weeks: 1}).endOf('week'); + + let params = { + size: 0, + q: "activitytype:pageviews" + }; + + let aggs = { + "by_week": { + "date_histogram": { + "field": "date", + "calendar_interval": "1w", + "format": "yyyy-MM-dd", + "min_doc_count": 0, + "hard_bounds": { + "min": startDate.toISODate(), + "max": endDate.plus({days: 1}).toISODate() + } + }, + "aggs": { + "by_object": { + "terms": { + "field": "objectid", + "size": 5, + "order": { + "totals": "desc" + } + }, + "aggs": { + "totals": { + "sum": { + "field": "activitycount" + } + } + } + } + } + } + }; + + const url = makeURL('activity', params, aggs); + fetch(url) + .then(response => response.json()) + .then(results => { + let objects = results["aggregations"]["by_week"]["buckets"][0]["by_object"]["buckets"]; + let objectIdList = objects.map(o => o.key).join("|"); + + params = { + id: objectIdList, + fields: "title,images,url" + }; + + let objectsUrl = makeURL("object", params); + + fetch(objectsUrl) + .then(response => response.json()) + .then(results => { + + objects.forEach(o => { + o.data = results.records.find(r => r.id == o.key); + }) + + output = { + dateRange: { + start: startDate.toLocaleString('en-US', {timeZone: "America/New_York"}), + end: endDate.toLocaleString('en-US', {timeZone: "America/New_York"}) + }, + objects: objects + }; + + callback(null, output); + }); + + }); +} + function getKeyStats(callback) { const params = { size: 0 @@ -322,5 +404,6 @@ module.exports = { getUpcomingExhibitions: getUpcomingExhibitions, getAltTextStats: getAltTextStats, getActivityStats: getActivityStats, + getFiveByFiveStats: getFiveByFiveStats, getKeyStats: getKeyStats }; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index b5800d5..6deb8ef 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,6 +19,7 @@ "hbs": "^4.1.1", "http-errors": "~1.6.3", "jsdom": "^20.0.3", + "luxon": "^3.3.0", "morgan": "~1.9.1", "node-fetch": "^2.6.0" } @@ -1156,6 +1157,14 @@ "node": ">= 0.8.0" } }, + "node_modules/luxon": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/luxon/-/luxon-3.3.0.tgz", + "integrity": "sha512-An0UCfG/rSiqtAIiBPO0Y9/zAnHUZxAMiCpTd5h2smgsj7GGmcenvrvww2cqNA8/4A5ZrD1gJpHN2mIHZQF+Mg==", + "engines": { + "node": ">=12" + } + }, "node_modules/media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", @@ -2545,6 +2554,11 @@ "type-check": "~0.3.2" } }, + "luxon": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/luxon/-/luxon-3.3.0.tgz", + "integrity": "sha512-An0UCfG/rSiqtAIiBPO0Y9/zAnHUZxAMiCpTd5h2smgsj7GGmcenvrvww2cqNA8/4A5ZrD1gJpHN2mIHZQF+Mg==" + }, "media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", diff --git a/package.json b/package.json index 8c63bc4..26a1e5d 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "hbs": "^4.1.1", "http-errors": "~1.6.3", "jsdom": "^20.0.3", + "luxon": "^3.3.0", "morgan": "~1.9.1", "node-fetch": "^2.6.0" } diff --git a/routes/index.js b/routes/index.js index 11643cb..6d5dc9f 100644 --- a/routes/index.js +++ b/routes/index.js @@ -55,6 +55,7 @@ router.get('/', cache('6 hours'), function(req, res, next) { alttextStats: stats.getAltTextStats, objectsOnViewStats: stats.getObjectsInGalleryStats, activityStats: stats.getActivityStats, + fiveByFiveStats: stats.getFiveByFiveStats, keyStats: stats.getKeyStats }, function(err, results) { @@ -77,7 +78,8 @@ router.get('/', cache('6 hours'), function(req, res, next) { data.objects.alttext.by_division = results['alttextStats']['divisions']; 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.pageviews.objects.count_as_percent = ((data.pageviews.objects.count/data.objects.public.count)*100).toFixed(2); + data.fivebyfive = results['fiveByFiveStats']; data.keys.count = results['keyStats']['keys']['count']; data.keys.count_as_string = data.keys.count.toLocaleString('en'); data.keys.statsdates = results['keyStats']['keys']['statsdates'];