Skip to content

Commit

Permalink
add 5x5 block
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffsteward committed Dec 5, 2024
1 parent 620b656 commit b4c2c67
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
83 changes: 83 additions & 0 deletions modules/data.js
Original file line number Diff line number Diff line change
@@ -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;})
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -322,5 +404,6 @@ module.exports = {
getUpcomingExhibitions: getUpcomingExhibitions,
getAltTextStats: getAltTextStats,
getActivityStats: getActivityStats,
getFiveByFiveStats: getFiveByFiveStats,
getKeyStats: getKeyStats
};
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
4 changes: 3 additions & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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'];
Expand Down

0 comments on commit b4c2c67

Please sign in to comment.