forked from arl/statsviz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
148 lines (123 loc) 路 3.68 KB
/
Copy pathapp.js
File metadata and controls
148 lines (123 loc) 路 3.68 KB
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
import * as stats from './stats.js';
import * as plot from "./plot.js";
import * as theme from "./theme.js";
import PlotsDef from './plotsdef.js';
const buildWebsocketURI = () => {
var loc = window.location,
ws_prot = "ws:";
if (loc.protocol === "https:") {
ws_prot = "wss:";
}
return ws_prot + "//" + loc.host + loc.pathname + "ws"
}
const dataRetentionSeconds = 600;
var timeout = 250;
const clamp = (val, min, max) => {
if (val < min) return min;
if (val > max) return max;
return val;
}
/* nav bar ui management */
let paused = false;
let show_gc = true;
let timerange = 60;
/* WebSocket connection handling */
const connect = () => {
const uri = buildWebsocketURI();
let ws = new WebSocket(uri);
console.info(`Attempting websocket connection to server at ${uri}`);
ws.onopen = () => {
console.info("Successfully connected");
timeout = 250; // reset connection timeout for next time
};
ws.onclose = event => {
console.error(`Closed websocket connection: code ${event.code}`);
setTimeout(connect, clamp(timeout += timeout, 250, 5000));
};
ws.onerror = err => {
console.error(`Websocket error, closing connection.`);
ws.close();
};
let initDone = false;
ws.onmessage = event => {
let data = JSON.parse(event.data)
if (!initDone) {
configurePlots(PlotsDef);
stats.init(PlotsDef, dataRetentionSeconds);
attachPlots();
$('#play_pause').change(() => { paused = !paused; });
$('#show_gc').change(() => {
show_gc = !show_gc;
updatePlots();
});
$('#select_timerange').click(() => {
const val = parseInt($("#select_timerange option:selected").val(), 10);
timerange = val;
updatePlots();
});
setInterval(() => {
if (paused || ws.readyState !== WebSocket.OPEN) {
return
}
updatePlots(PlotsDef.events);
}, 1000)
initDone = true;
stats.pushData(data);
updatePlots(PlotsDef.events);
return
}
stats.pushData(data);
}
}
connect();
let plots = [];
const configurePlots = (plotdefs) => {
plots = [];
plotdefs.series.forEach(plotdef => {
plots.push(new plot.Plot(plotdef));
});
}
const attachPlots = () => {
let plotsDiv = $('#plots');
plotsDiv.empty();
for (let i = 0; i < plots.length; i++) {
const plot = plots[i];
let div = $(`<div id="${plot.name()}">`);
plot.createElement(div[0], i)
plotsDiv.append(div);
}
}
const updatePlots = () => {
// Create shapes.
let shapes = new Map();
let data = stats.slice(timerange);
if (show_gc) {
for (const [name, serie] of data.events) {
shapes.set(name, plot.createVerticalLines(serie));
}
}
// Always show the full range on x axis.
const now = data.times[data.times.length - 1];
let xrange = [now - timerange * 1000, now];
plots.forEach(plot => {
if (!plot.hidden) {
plot.update(xrange, data, shapes);
}
});
}
const updatePlotsLayout = () => {
plots.forEach(plot => {
plot.updateTheme();
});
}
theme.updateThemeMode();
/**
* Change color theme when the user presses the theme switch button
*/
$('#color_theme_sw').change(() => {
const themeMode = theme.getThemeMode();
const newTheme = themeMode === "dark" && "light" || "dark";
localStorage.setItem("theme-mode", newTheme);
theme.updateThemeMode();
updatePlotsLayout();
});