-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
157 lines (134 loc) · 4.6 KB
/
index.html
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<!-- <meta name="viewport" content="width=device-width, initial-scale=1"> -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Severn Trent - State of the Reservoirs</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/sql-wasm.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chartjs-adapter-date-fns.bundle.min.js"></script>
<style type="text/css">
#reservoirList {
margin: 0 auto;
text-align: center;
margin-bottom: 50px;
margin-top: 20px;
}
#chart {
height: 100%;
margin: 100px;
}
</style>
</head>
<body>
<div id='reservoirList'></div>
<div id='chart'></div>
<script>
const reservoirList = document.getElementById("reservoirList");
function ucwords (str) {
return (str + '').toLowerCase().replace(/^([a-z])|\s+([a-z])/g, function ($1) {
return $1.toUpperCase();
});
}
function redirect(reservoir) {
window.history.pushState({"reservoir": reservoir}, null, "/" + encodeURIComponent(reservoir));
build();
}
async function build() {
const db = window.db;
const reservoirs = db.exec(`
SELECT DISTINCT
reservoir
FROM water_level
INNER JOIN date ON (date.id=water_level.date_id)
WHERE \`date\`.\`date\` >= "2023-01-01" AND
reservoir != ''
ORDER BY reservoir ASC
`)[0].values.map(data => data[0]);
if (history.state === null) {
const path = decodeURIComponent(window.location.pathname.replace(/^\//, ""));
if (reservoirs.includes(path)) {
redirect(path);
return;
}
redirect(reservoirs[0]);
return;
}
const currentReservoir = history.state["reservoir"];
reservoirList.innerHTML = reservoirs.map(reservoir =>
currentReservoir === reservoir ?
`<span>${ucwords(reservoir)}</span>` :
`<a href='#' onclick="redirect('${reservoir}'); return false;">${ucwords(reservoir)}</a>`
).join(" | ");
const waterLevels = db.exec(`
SELECT
current_storage,
max_storage,
STRFTIME("%Y-%m-%d", date) as day,
STRFTIME("%Y", date) as year
FROM
water_level
INNER JOIN
date ON (date.id=water_level.date_id)
WHERE
reservoir='${currentReservoir}'
ORDER BY
date ASC
`)[0].values.filter((value) => Number.isInteger(value[0]));
const minYear = Math.min(...waterLevels.map(row => Number(row[3])));
const maxYear = Math.max(...waterLevels.map(row => Number(row[3])));
const maxStorage = Math.max(...waterLevels.map(row => Number(row[1])));
const byYear = {};
for (let year=minYear; year<=maxYear; year++) {
byYear[year] = {
"label": year,
"data": []
}
}
waterLevels.forEach(row => {
byYear[row[3]]["data"].push({
"x": row[2],
"y": row[0]
})
});
const chart = document.getElementById("chart");
chart.innerHTML = '';
let canvas = document.createElement('canvas');
canvas.id = 'waterChart';
chart.appendChild(canvas);
// console.log(byYear);
const ctx = document.getElementById('waterChart');
new Chart(ctx, {
type: 'line',
data: {
datasets: Object.values(byYear)
},
options: {
scales: {
x: {
type: 'time',
time: {
unit: 'week',
}
},
y: {
max: maxStorage,
min: 0
}
}
}
});
}
(async () => {
const sqlPromise = initSqlJs({
locateFile: file => `https://cdn.jsdelivr.net/npm/[email protected]/dist/${file}`
});
const dataPromise = fetch("/database.db").then(res => res.arrayBuffer());
const [SQL, buf] = await Promise.all([sqlPromise, dataPromise])
window.db = new SQL.Database(new Uint8Array(buf));
await build();
})();
</script>
</body>
</html>