-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
104 lines (85 loc) · 2.94 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const express = require('express')
const expressPromiseRouter = require("express-promise-router");
const Promise = require("bluebird");
const unhandledError = require("unhandled-error");
const readFile = Promise.promisify(require("fs").readFile);
const mustacheExpress = require('mustache-express');
const path = require("path");
let errorReporter = unhandledError((error, context) => {
console.error(error, context);
});
const root = '/heatmap';
let app = express();
let router = expressPromiseRouter();
router.get(root, function(req, res){
let now = new Date();
const currentYear = `year${now.getFullYear()}`;
const weekDays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const fileNames = {
"7days": "Last 7 Days",
"70days": "Last 70 Days",
"365days": "Last 365 Days",
"alltime": "All Time"
};
return Promise.try(function(){
let yearFileNames = {};
for (var year=2010; year <= now.getFullYear(); year++) {
yearFileNames[`year${year}`] = year.toString();
}
let normalHeatmaps = Promise.map(Object.keys(fileNames), (fileName) => {
return getHeatmap(fileName, weekDays, fileNames);
});
let yearHeatmapSlider = Promise.map(Object.keys(yearFileNames), (fileName) => {
return getHeatmap(fileName, weekDays, yearFileNames);
});
return Promise.all([
normalHeatmaps,
yearHeatmapSlider
]);
}).then((data) => {
let normalHeatmaps = data[0];
let yearHeatmapSlider = data[1];
let yearSlider = {
firstYear: 2010,
lastYear: now.getFullYear(),
data: yearHeatmapSlider
};
res.render('index', {heatmaps: normalHeatmaps, yearSlider: yearSlider});
});
});
function getHeatmap(filename, weekDays, fileNames) {
return readFile(`heatmaps/${filename}.json`, 'ascii')
.then((file) => JSON.parse(file))
.then((json) => {
let dates = {
from: json.begin.iso,
to: json.end.iso
};
let jsonHeatmap = json.heatmap;
let heatmap = [];
json.heatmap.forEach((values, index) => {
let columns = [];
values.forEach((percentage) => {
let color = `hsla(${1.2*percentage}, 100%, 50%, 1)`;
columns.push({color: color, percentage: percentage.toString()}); //nbsp
});
heatmap.push({
day: weekDays[index],
columns: columns
});
});
//Move Sunday to end of array
let shiftedHeatmap = heatmap;
shiftedHeatmap.push(shiftedHeatmap.shift());
return {header: fileNames[filename], dates: dates, heatmap: shiftedHeatmap, filename: filename};
})
}
app.use(router);
app.use(root, express.static(path.join(__dirname, "public")));
function jsonHeaders(res) {
res.set('content-type', 'application/json');
}
app.use(`${root}/api`, express.static(path.join(__dirname, "heatmaps"), {setHeaders: jsonHeaders}));
app.engine('html', mustacheExpress());
app.set('view engine', 'html');
app.listen(3000, () => console.log('Heatmap started'))