This code is fault tolerant
This commit is contained in:
@@ -8,29 +8,36 @@
|
||||
|
||||
var current_dataset = [];
|
||||
while (true) {
|
||||
// fetch new data
|
||||
await d3.json("/data").then(function(data) {
|
||||
// then append to list
|
||||
current_dataset.push(data);
|
||||
// Map to a 10s (n S) sliding window
|
||||
// ie find the most recent value, then reduce/filter anything more than 10s earlier than that.
|
||||
const most_recent_time = current_dataset[current_dataset.length - 1].time;
|
||||
const window_start = Math.max(0, most_recent_time - 10);
|
||||
// filter the list
|
||||
current_dataset = current_dataset.filter(
|
||||
value => value.time >= window_start
|
||||
);
|
||||
// render dist graph
|
||||
var value_graph = Plot.plot({
|
||||
x: { grid: true},
|
||||
y: { grid: true},
|
||||
marks: [
|
||||
Plot.line(current_dataset, {x: "time", y: "value", stroke: "red"}),
|
||||
]
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 1000);
|
||||
// fetch new data
|
||||
await d3.json("/data", {signal: controller.signal}).then(function(data) {
|
||||
// then append to list
|
||||
current_dataset.push(data);
|
||||
// Map to a 10s (n S) sliding window
|
||||
// ie find the most recent value, then reduce/filter anything more than 10s earlier than that.
|
||||
const most_recent_time = current_dataset[current_dataset.length - 1].time;
|
||||
const window_start = Math.max(0, most_recent_time - 10);
|
||||
// filter the list
|
||||
current_dataset = current_dataset.filter(
|
||||
value => value.time >= window_start
|
||||
);
|
||||
// render dist graph
|
||||
var value_graph = Plot.plot({
|
||||
x: { grid: true},
|
||||
y: { grid: true},
|
||||
marks: [
|
||||
Plot.line(current_dataset, {x: "time", y: "value", stroke: "red"}),
|
||||
]
|
||||
});
|
||||
var graphNode = document.getElementById("value");
|
||||
graphNode.replaceChild(value_graph, graphNode.firstChild);
|
||||
});
|
||||
var graphNode = document.getElementById("value");
|
||||
graphNode.replaceChild(value_graph, graphNode.firstChild);
|
||||
});
|
||||
clearTimeout(timeoutId);
|
||||
} catch (error) {
|
||||
console.log(error.name === 'AbortError');
|
||||
}
|
||||
}
|
||||
// }
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user