This repository has been archived by the owner on Jul 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
140 lines (121 loc) · 4.86 KB
/
main.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const DotEnv = require("dotenv");
const Express = require("express");
const Path = require("path");
const Schedule = require("node-schedule");
const Database = require("./lib/database");
const DataImporter = require("./lib/dataImporter");
const DateHelpers = require("./helpers/dateHelpers");
const HistoryFormatter = require("./helpers/historyFormatter");
const InterventionCounter = require("./lib/interventionCounter");
const ValueFormatter = require("./helpers/valueFormatter");
const config = require("./config/interventions.json");
DotEnv.config();
async function runImportAndCount(config, database) {
let data = await new DataImporter(config).run();
await database.storeCurrentInterventions(data);
// The counter has side effects. It mutates the data object coming in, and
// since JS does not support native deep-copying objects, let's just agree on
// not touching the order of execution here.
let counters = new InterventionCounter(config, data).run();
await database.storeCounters(counters);
}
(async () => {
let database = new Database();
await database.connect();
// Run the automatic importer once a day, at midnight. That's frequent enough
// to not lag too far behind current development, but rare enoough to have
// meaningful history without downscaling measurements. :)
Schedule.scheduleJob("0 0 0 * * *", async () => {
await runImportAndCount(config, database);
});
// If there are no active interventions, let's run a full import first. This
// helps bootstrapping the empty database and have the dashboard up and
// running immediately.
if ((await database.countActiveInterventions()) < 1) {
await runImportAndCount(config, database);
}
// Express is used for serving the dashboard itself. We have both the tracking
// and the dashboard in the same application to make deployment simple, and
// to only have a single node process running at all times.
let port = process.env.PORT || 5000;
Express()
.use(Express.static(Path.join(__dirname, "public")))
.set("views", Path.join(__dirname, "views"))
.set("view engine", "ejs")
.get("/", async (req, res) => {
let latestCount = await database.getLatestCounts();
res.render("pages/dashboard", { counts: latestCount["counters"] });
})
.get("/refresh", async (req, res) => {
await runImportAndCount(config, database);
res.writeHead(302, { Location: "/" });
res.end();
})
.get("/list/:distribution", async (req, res) => {
let distribution = req.params.distribution;
if (!config["distributions"].includes(distribution)) {
return res.status(404).render("pages/404");
}
let interventions = await database.getInterventionsForDistribution(
distribution
);
interventions = interventions.map(ValueFormatter);
res.render("pages/list", {
distribution: distribution,
interventions: interventions,
});
})
.get("/data.json", async (req, res) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Vary", "Origin");
res.setHeader("Content-Type", "application/json");
let startDate = req.query.start;
let endDate = req.query.end;
let distribution = req.query.distribution;
let type = req.query.type;
if (!startDate || !startDate.match(DateHelpers.VALID_DATE)) {
let today = new Date();
startDate = new Date(today.getFullYear(), 0, 1);
} else {
startDate = DateHelpers.parseDateString(startDate);
}
if (!endDate || !endDate.match(DateHelpers.VALID_DATE)) {
let today = new Date();
endDate = new Date(
Date.UTC(
today.getFullYear(),
today.getMonth(),
today.getDate(),
23,
59,
59,
999
)
);
} else {
endDate = DateHelpers.parseDateString(endDate, true);
}
if (!distribution || !config["distributions"].includes(distribution)) {
return res
.status(400)
.end('{"error": "distribution parameter unset or invalid!"}');
}
if (!type || !(config["types"].includes(type) || type == "all")) {
return res
.status(400)
.end('{"error": "type parameter unset or invalid!"}');
}
let data = await database.getHistoricalData(
startDate,
endDate,
distribution
);
res.end(JSON.stringify(data.map((row) => HistoryFormatter(row, type))));
})
.use((req, res) => res.status(404).render("pages/404"))
.listen(port, () => console.log(`Listening on port ${port}`));
})();