-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrafiek.html
235 lines (215 loc) · 5.44 KB
/
grafiek.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>MonetDB Conductor</title>
<style>
canvas {
border: dotted lightgrey;
width: 30em;
height: 5em;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script type="text/javascript">
var the_data = [];
var charts = {};
function create_canvas_if_necessary(poolname) {
var id = "pool-canvas-" + poolname;
if (document.getElementById(id)) {
return id;
}
var canvases = document.getElementById("canvases");
var header = document.createElement("p");
header.innerHTML = "Pool " + poolname + ":";
var canvas = document.createElement("canvas");
canvas.setAttribute("id", id);
canvases.appendChild(header);
canvases.appendChild(canvas);
return canvas;
}
function create_chart(canvas, poolname) {
// Separate into series andconvert time to minutes before latest.
// Or more precisely, negative minutes after latest.
var load_series = [];
var running_series = [];
var starting_and_running_series = [];
var desired_series = [];
var ctx = canvas.getContext('2d');
// https://github.com/chartjs/Chart.js/blob/master/samples/scales/time/line-point-data.html
var chart = new Chart(ctx, {
// The type of chart we want to create
type: 'line',
// The data for our dataset
data: {
datasets: [
// Running: darker green: starting: lighter green on top of that
{
label: 'Running',
data: running_series,
steppedLine: 'before',
fill: true,
//
pointRadius: 0,
borderWidth: 0,
borderColor: '#afc',
backgroundColor: '#afc',
},
{
label: 'Starting + Running',
data: starting_and_running_series,
steppedLine: 'before',
fill: true,
//
pointRadius: 0,
borderWidth: 0,
borderColor: '#dfe',
backgroundColor: '#dfe'
},
// Desired: dotted line
{
label: 'Desired',
data: desired_series,
steppedLine: 'before',
fill: false,
//
pointRadius: 0,
borderWidth: 1,
borderDash: [5, 5],
borderColor: 'black',
backgroundColor: 'black',
},
// Load
{
label: 'Load',
data: load_series,
// steppedLine: 'before',
fill: false,
showLine: false,
//
pointRadius: 1,
borderWidth: 0,
borderColor: 'pink',
backgroundColor: 'pink',
}
]
},
// Configuration options go here
options: {
responsive: false,
animation: {
duration: 0,
},
legend: {
display: false,
},
scales: {
xAxes: [{
type: 'linear',
display: true,
scaleLabel: {
display: true,
labelString: 'minutes ago'
}
}],
yAxes: [
{
type: 'linear',
ticks: {
stepSize: 0.5,
},
// display: true,
// scaleLabel: {
// display: true,
// labelString: 'pear',
// }
},
],
}
}
});
function receive_update(item, now) {
var t = (item.timestamp - now) / 60000;
load_series.push({ x: t, y: item.load });
running_series.push({ x: t, y: item.up });
starting_and_running_series.push({ x: t, y: item.starting + item.up });
desired_series.push({ x: t, y: item.desired });
}
chart.receive_update = receive_update;
return chart;
}
function on_status_update(entry) {
for (pool in entry.stats) {
if (!charts[pool]) {
var canvas = create_canvas_if_necessary(pool);
charts[pool] = create_chart(canvas, pool);
}
var chart = charts[pool];
var item = entry.stats[pool];
item.timestamp = entry.timestamp;
chart.receive_update(item, entry.timestamp);
}
}
function on_load() {
var status_txt = document.getElementById('status_txt');
fetch("data.json")
.then(function (response) {
// unpack the data
if (!response.ok) {
console.log("not ok")
return response.text().then(t => Promise.reject(t));
}
console.log("ok")
return response.json();
})
.then(function (data) {
var per_pool = {};
var charts = {};
the_data = data;
var i = 0;
function updater() {
i += 1;
var entry = the_data[i];
status_txt.innerHTML = "" + i + "<br>" + JSON.stringify(entry);
if (entry) {
on_status_update(entry);
}
}
setInterval(updater, 1000);
// for (entry of data) {
// for (pool in entry.stats) {
// if (per_pool[pool] == undefined) {
// per_pool[pool] = [];
// }
// if (charts[pool] == undefined) {
// var canvas = create_canvas_if_necessary(pool);
// charts[pool] = create_chart(canvas, pool);
// }
// var item = entry.stats[pool];
// item.timestamp = entry.timestamp;
// charts[pool].receive_update(item, latest);
// }
// }
// status_txt.innerHTML += "" + latest + "<br>";
// for (pool in per_pool) {
// charts[pool].update();
// }
})
.catch(function (err) {
console.log("error!")
status_txt.innerHTML = "Error loading status: " + err;
status_txt.style.backgroundColor = "#FDD";
})
.finally(function () {
console.log("finally")
})
}
</script>
</head>
<body onload="on_load()">
<h1>Grafiekje</h1>
<div id="canvases"></div>
<h2>Pool status</h2>
<pre id="status_txt" />
</body>
</html>