Added histogram page

This commit is contained in:
2018-11-15 17:19:13 +01:00
parent 6ce17cefc5
commit bdca489e62
7 changed files with 164 additions and 4 deletions
+11
View File
@@ -0,0 +1,11 @@
extends ./total.pug
block mistake
p.
If you think there is a mistake in this histogram, feel free to <a
href="mailto:thomas@forgione.fr">send me a mail</a> exaplaining what
are the problem so I can fix it. Thanks!
block extrajs
script(src="/static/chart/Chart.min.js")
script(src="/static/js/histogram.js")
+5 -4
View File
@@ -20,10 +20,11 @@ block content
.col-2
#result
p.
If you think there is a mistake in this table, feel free to
<a href="mailto:thomas@forgione.fr">send me a mail</a>
exaplaining what are the problem so I can fix it. Thanks!
block mistake
p.
If you think there is a mistake in this table, feel free to
<a href="mailto:thomas@forgione.fr">send me a mail</a>
exaplaining what are the problem so I can fix it. Thanks!
block extracss
style.
+2
View File
@@ -5,4 +5,6 @@ module.exports = [
url('/total-by-course/', 'totalByCourse', 'totalByCourse'),
url('/total-table', 'totalTable', 'totalTable'),
url('/total-table-by-course', 'totalTableByCourse', 'totalTableByCourse'),
url('/histogram', 'histogram', 'histogram'),
url('/histogram-data', 'histogramData', 'histogramData'),
]
+59
View File
@@ -3,6 +3,14 @@ const cal = require('calendar');
const redirectIfNotLogged = require('auth/views').redirectIfNotLogged;
const getUrl = require('create-url').getUrl;
function compareStrings(a, b) {
if (a < b)
return -1;
if (a > b)
return 1;
return 0;
}
function computeUserTable(user, callback) {
let result = {};
@@ -134,3 +142,54 @@ module.exports.totalTableByCourse = function(req, res, render, next) {
render('totalTableByCourse.pug');
});
}
module.exports.histogram = redirectIfNotLogged('histogram', function(req, res, render) {
render('histogram.pug');
});
module.exports.histogramData = function(req, res, render, next) {
if (req.session.user === undefined) {
res.status(404);
res.setHeader('Content-Type', 'text/html');
res.render(config.BASE_DIR + '/templates/404.pug', res.locals, function(err, result) {
if (err)
console.log(err);
res.send(result);
});
return;
}
cal.getCalendar(req.session.user, new Date(2000, 1, 1), new Date(3000, 1, 1), (calendar) => {
let histogram = {};
for (let event of calendar.events) {
let hours = event.startTime.getHours();
if (hours % 2 === 0) {
hours += 2;
} else {
hours += 1;
}
hours = hours.toString().padStart(2, '0');
let minutes = event.startTime.getMinutes().toString().padStart(2, '0');
let key = hours + ':' + minutes;
histogram[key] = histogram[key] || 0;
histogram[key]++;
}
let histogramArray = [];
for (let key in histogram) {
histogramArray.push({
time: key,
count: histogram[key],
});
}
histogramArray.sort((a, b) => compareStrings(a.time, b.time));
res.send(JSON.stringify(histogramArray));
});
}