Debian Codegraph @

pkg/lib/plot.js

raw
1 /*
2 * Copyright (C) 2014 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import cockpit from 'cockpit';
7
8 /* The public API here is not general and a bit weird. It can only do
9 * what Cockpit itself needs right now, and what can be easily
10 * implemented without touching the older Metrics_series classes.
11 *
12 * The basic idea is that you create a global PlotState object and
13 * keep it alive for the lifetime of the page. Then you instantiate
14 * ZoomControl and SvgPlot components as needed.
15 *
16 * To control what to plot, you call some methods on the PlotState
17 * object:
18 *
19 * - plot_state.plot_single(id, metric_options)
20 *
21 * - plot_state.plot_instances(id, metric_options, instances, reset)
22 *
23 * You need to figure out the rest of the details from the existing
24 * users, unfortunately.
25 */
26
27 class Metrics_series {
28 constructor(desc, opts, grid, plot_data, interval) {
29 cockpit.event_target(this);
30 this.desc = desc;
31 this.options = opts;
32 this.grid = grid;
33 this.plot_data = plot_data;
34 this.interval = interval;
35 this.channel = null;
36 this.chanopts_list = [];
37 }
38
39 stop() {
40 if (this.channel)
41 this.channel.close();
42 }
43
44 remove_series() {
45 const pos = this.plot_data.indexOf(this.options);
46 if (pos >= 0)
47 this.plot_data.splice(pos, 1);
48 }
49
50 remove() {
51 this.stop();
52 this.remove_series();
53 this.dispatchEvent("removed");
54 }
55
56 build_metric(n) {
57 return { name: n, units: this.desc.units, derive: this.desc.derive };
58 }
59
60 check_archives() {
61 if (this.channel.archives)
62 this.dispatchEvent("changed");
63 }
64 }
65
66 class Metrics_sum_series extends Metrics_series {
67 constructor(desc, opts, grid, plot_data, interval) {
68 super(desc, opts, grid, plot_data, interval);
69 if (this.desc.direct) {
70 this.chanopts_list.push({
71 source: 'direct',
72 archive_source: 'pcp-archive',
73 metrics: this.desc.direct.map(this.build_metric, this),
74 instances: this.desc.instances,
75 'omit-instances': this.desc['omit-instances'],
76 host: this.desc.host
77 });
78 }
79 if (this.desc.pmcd) {
80 this.chanopts_list.push({
81 source: 'pmcd',
82 metrics: this.desc.pmcd.map(this.build_metric, this),
83 instances: this.desc.instances,
84 'omit-instances': this.desc['omit-instances'],
85 host: this.desc.host
86 });
87 }
88 if (this.desc.internal) {
89 this.chanopts_list.push({
90 source: 'internal',
91 metrics: this.desc.internal.map(this.build_metric, this),
92 instances: this.desc.instances,
93 'omit-instances': this.desc['omit-instances'],
94 host: this.desc.host
95 });
96 }
97 }
98
99 flat_sum(val) {
100 if (!val)
101 return 0;
102 if (val.length !== undefined) {
103 let sum = 0;
104 for (let i = 0; i < val.length; i++)
105 sum += this.flat_sum(val[i]);
106 return sum;
107 }
108 return val;
109 }
110
111 reset_series() {
112 if (this.channel)
113 this.channel.close();
114
115 this.channel = cockpit.metrics(this.interval, this.chanopts_list);
116
117 const metrics_row = this.grid.add(this.channel, []);
118 const factor = this.desc.factor || 1;
119 const threshold = this.desc.threshold || null;
120 const offset = this.desc.offset || 0;
121 this.options.data = this.grid.add((row, x, n) => {
122 for (let i = 0; i < n; i++) {
123 const value = offset + this.flat_sum(metrics_row[x + i]) * factor;
124 if (threshold !== null)
125 row[x + i] = [(this.grid.beg + x + i) * this.interval, Math.abs(value) > threshold ? value : null, threshold];
126 else
127 row[x + i] = [(this.grid.beg + x + i) * this.interval, value];
128 }
129 });
130
131 this.channel.addEventListener("changed", this.check_archives.bind(this));
132 this.check_archives();
133 }
134 }
135
136 class Metrics_stacked_instances_series extends Metrics_series {
137 constructor(desc, opts, grid, plot_data, interval) {
138 super(desc, opts, grid, plot_data, interval);
139 this.instances = { };
140 this.last_instance = null;
141 if (this.desc.direct) {
142 this.chanopts_list.push({
143 source: 'direct',
144 archive_source: 'pcp-archive',
145 metrics: [this.build_metric(this.desc.direct)],
146 metrics_path_names: ['a'],
147 instances: this.desc.instances,
148 'omit-instances': this.desc['omit-instances'],
149 host: this.desc.host
150 });
151 }
152 if (this.desc.pmcd) {
153 this.chanopts_list.push({
154 source: 'pmcd',
155 metrics: this.desc.pmcd.map(this.build_metric, this),
156 metrics_path_names: ['a'],
157 instances: this.desc.instances,
158 'omit-instances': this.desc['omit-instances'],
159 host: this.desc.host
160 });
161 }
162
163 if (this.desc.internal) {
164 this.chanopts_list.push({
165 source: 'internal',
166 metrics: [this.build_metric(this.desc.internal)],
167 metrics_path_names: ['a'],
168 instances: this.desc.instances,
169 'omit-instances': this.desc['omit-instances'],
170 host: this.desc.host
171 });
172 }
173 }
174
175 reset_series() {
176 if (this.channel)
177 this.channel.close();
178 this.channel = cockpit.metrics(this.interval, this.chanopts_list);
179 this.channel.addEventListener("changed", this.check_archives.bind(this));
180 this.check_archives();
181 for (const name in this.instances)
182 this.instances[name].reset();
183 }
184
185 add_instance(name, selector) {
186 if (this.instances[name])
187 return;
188
189 const instance_data = Object.assign({ selector }, this.options);
190 const factor = this.desc.factor || 1;
191 const threshold = this.desc.threshold || 0;
192 const last = this.last_instance;
193 let metrics_row;
194
195 function reset() {
196 metrics_row = this.grid.add(this.channel, ['a', name]);
197 instance_data.data = this.grid.add((row, x, n) => {
198 for (let i = 0; i < n; i++) {
199 const value = (metrics_row[x + i] || 0) * factor;
200 const ts = (this.grid.beg + x + i) * this.interval;
201 let floor = 0;
202
203 if (last) {
204 if (last.data[x + i][1])
205 floor = last.data[x + i][1];
206 else
207 floor = last.data[x + i][2];
208 }
209
210 if (Math.abs(value) > threshold) {
211 row[x + i] = [ts, floor + value, floor];
212 if (row[x + i - 1] && row[x + i - 1][1] === null)
213 row[x + i - 1][1] = row[x + i - 1][2];
214 } else {
215 row[x + i] = [ts, null, floor];
216 if (row[x + i - 1] && row[x + i - 1][1] !== null)
217 row[x + i - 1][1] = row[x + i - 1][2];
218 }
219 }
220 });
221 }
222
223 function remove() {
224 this.grid.remove(metrics_row);
225 this.grid.remove(instance_data.data);
226 const pos = this.plot_data.indexOf(instance_data);
227 if (pos >= 0)
228 this.plot_data.splice(pos, 1);
229 }
230
231 instance_data.reset = reset.bind(this);
232 instance_data.remove = remove.bind(this);
233 instance_data.name = name;
234 this.last_instance = instance_data;
235 this.instances[name] = instance_data;
236 instance_data.reset();
237 this.plot_data.push(instance_data);
238 this.grid.sync();
239 }
240
241 clear_instances() {
242 for (const i in this.instances)
243 this.instances[i].remove();
244 this.instances = { };
245 this.last_instance = null;
246 }
247 }
248
249 class Plot {
250 constructor(element, x_range_seconds, x_stop_seconds) {
251 cockpit.event_target(this);
252
253 this.series = [];
254 this.plot_data = [];
255
256 this.interval = Math.ceil(x_range_seconds / 1000) * 1000;
257 this.grid = null;
258
259 this.sync_suppressed = 0;
260 this.archives = false;
261
262 this.reset(x_range_seconds, x_stop_seconds);
263 }
264
265 refresh() {
266 this.dispatchEvent("plot", this.plot_data);
267 }
268
269 start_walking() {
270 this.grid.walk();
271 }
272
273 stop_walking() {
274 this.grid.move(this.grid.beg, this.grid.end);
275 }
276
277 reset(x_range_seconds, x_stop_seconds) {
278 // Fill the plot with about 1000 samples, but don't sample
279 // faster than once per second.
280 //
281 // TODO - do this based on the actual size of the plot.
282 this.interval = Math.ceil(x_range_seconds / 1000) * 1000;
283
284 const x_offset = (x_stop_seconds !== undefined)
285 ? (new Date().getTime()) - x_stop_seconds * 1000
286 : 0;
287
288 const beg = -Math.ceil((x_range_seconds * 1000 + x_offset) / this.interval);
289 const end = -Math.floor(x_offset / this.interval);
290
291 if (this.grid && this.grid.interval == this.interval) {
292 this.grid.move(beg, end);
293 } else {
294 if (this.grid)
295 this.grid.close();
296 this.grid = cockpit.grid(this.interval, beg, end);
297 this.sync_suppressed++;
298 for (let i = 0; i < this.series.length; i++) {
299 this.series[i].stop();
300 this.series[i].interval = this.interval;
301 this.series[i].grid = this.grid;
302 this.series[i].reset_series();
303 }
304 this.sync_suppressed--;
305 this.sync();
306
307 this.grid.addEventListener("notify", (event, index, count) => {
308 this.refresh();
309 });
310 }
311 }
312
313 sync() {
314 if (this.sync_suppressed === 0)
315 this.grid.sync();
316 }
317
318 destroy() {
319 this.grid.close();
320 for (let i = 0; i < this.series.length; i++)
321 this.series[i].stop();
322
323 this.options = { };
324 this.series = [];
325 this.plot_data = [];
326 }
327
328 check_archives() {
329 if (!this.archives) {
330 this.archives = true;
331 this.dispatchEvent('changed');
332 }
333 }
334
335 add_metrics_sum_series(desc, opts) {
336 const sum_series = new Metrics_sum_series(desc, opts, this.grid, this.plot_data, this.interval);
337
338 sum_series.addEventListener("removed", this.refresh.bind(this));
339 sum_series.addEventListener("changed", this.check_archives.bind(this));
340 sum_series.reset_series();
341 sum_series.check_archives();
342
343 this.series.push(sum_series);
344 this.sync();
345 this.plot_data.push(opts);
346
347 return sum_series;
348 }
349
350 add_metrics_stacked_instances_series(desc, opts) {
351 const stacked_series = new Metrics_stacked_instances_series(desc, opts, this.grid, this.plot_data, this.interval);
352
353 stacked_series.addEventListener("removed", this.refresh.bind(this));
354 stacked_series.addEventListener("changed", this.check_archives.bind(this));
355 stacked_series.reset_series();
356 stacked_series.check_archives();
357
358 this.series.push(stacked_series);
359 this.sync_suppressed++;
360 for (const name in stacked_series.instances)
361 stacked_series.instances[name].reset();
362 this.sync_suppressed--;
363 this.sync();
364
365 return stacked_series;
366 }
367 }
368
369 class ZoomState {
370 constructor(reset_callback) {
371 cockpit.event_target(this);
372
373 this.reset_callback = reset_callback;
374
375 this.x_range = 5 * 60;
376 this.x_stop = undefined;
377 this.history = [];
378
379 this.enable_zoom_in = false;
380 this.enable_zoom_out = true;
381 this.enable_scroll_left = true;
382 this.enable_scroll_right = false;
383 }
384
385 reset() {
386 const plot_min_x_range = 5 * 60;
387
388 if (this.x_range < plot_min_x_range) {
389 this.x_stop += (plot_min_x_range - this.x_range) / 2;
390 this.x_range = plot_min_x_range;
391 }
392 if (this.x_stop >= (new Date()).getTime() / 1000 - 10)
393 this.x_stop = undefined;
394
395 this.reset_callback(this.x_range, this.x_stop);
396
397 this.enable_zoom_in = (this.x_range > plot_min_x_range);
398 this.enable_scroll_right = (this.x_stop !== undefined);
399
400 this.dispatchEvent("changed");
401 }
402
403 set_range(x_range) {
404 this.history = [];
405 this.x_range = x_range;
406 this.reset();
407 }
408
409 zoom_in(x_range, x_stop) {
410 this.history.push(this.x_range);
411 this.x_range = x_range;
412 this.x_stop = x_stop;
413 this.reset();
414 }
415
416 zoom_out() {
417 const plot_zoom_steps = [
418 5 * 60,
419 60 * 60,
420 6 * 60 * 60,
421 24 * 60 * 60,
422 7 * 24 * 60 * 60,
423 30 * 24 * 60 * 60,
424 365 * 24 * 60 * 60
425 ];
426
427 let r = this.history.pop();
428 if (r === undefined) {
429 let i;
430 for (i = 0; i < plot_zoom_steps.length - 1; i++) {
431 if (plot_zoom_steps[i] > this.x_range)
432 break;
433 }
434 r = plot_zoom_steps[i];
435 }
436 if (this.x_stop !== undefined)
437 this.x_stop += (r - this.x_range) / 2;
438 this.x_range = r;
439 this.reset();
440 }
441
442 goto_now() {
443 this.x_stop = undefined;
444 this.reset();
445 }
446
447 scroll_left() {
448 const step = this.x_range / 10;
449 if (this.x_stop === undefined)
450 this.x_stop = (new Date()).getTime() / 1000;
451 this.x_stop -= step;
452 this.reset();
453 }
454
455 scroll_right() {
456 const step = this.x_range / 10;
457 if (this.x_stop !== undefined) {
458 this.x_stop += step;
459 this.reset();
460 }
461 }
462 }
463
464 class SinglePlotState {
465 constructor() {
466 this._plot = new Plot(null, 300);
467 this._plot.start_walking();
468 }
469
470 plot_single(metric) {
471 if (this._stacked_instances_series) {
472 this._stacked_instances_series.clear_instances();
473 this._stacked_instances_series.remove();
474 this._stacked_instances_series = null;
475 }
476 if (!this._sum_series) {
477 this._sum_series = this._plot.add_metrics_sum_series(metric, { });
478 }
479 }
480
481 plot_instances(metric, insts, reset) {
482 if (this._sum_series) {
483 this._sum_series.remove();
484 this._sum_series = null;
485 }
486 if (!this._stacked_instances_series) {
487 this._stacked_instances_series = this._plot.add_metrics_stacked_instances_series(metric, { });
488 } else if (reset) {
489 // We can't remove individual instances, only clear the
490 // whole thing (because of limitations of Metrics_stacked_instances_series above).
491 // So we do that, but only when there is at least one instance
492 // that needs to be removed. That avoids a lot of events and React warnings.
493 if (Object.keys(this._stacked_instances_series.instances).some(old => insts.indexOf(old) == -1))
494 this._stacked_instances_series.clear_instances();
495 }
496
497 for (let i = 0; i < insts.length; i++) {
498 this._stacked_instances_series.add_instance(insts[i]);
499 }
500 }
501
502 destroy() {
503 this._plot.destroy();
504 }
505 }
506
507 export class PlotState {
508 constructor() {
509 cockpit.event_target(this);
510 this.plots = { };
511 this.zoom_state = null;
512 }
513
514 _reset_plots(x_range, x_stop) {
515 for (const id in this.plots) {
516 const p = this.plots[id]._plot;
517 p.stop_walking();
518 p.reset(x_range, x_stop);
519 p.refresh();
520 if (x_stop === undefined)
521 p.start_walking();
522 }
523 }
524
525 _check_archives(plot) {
526 if (!this.zoom_state && plot.archives) {
527 this.zoom_state = new ZoomState(this._reset_plots.bind(this));
528 this.dispatchEvent("changed");
529 }
530 }
531
532 _get(id) {
533 if (this.plots[id])
534 return this.plots[id];
535
536 const ps = new SinglePlotState();
537 ps._plot.addEventListener("changed", () => this._check_archives(ps._plot));
538 this._check_archives(ps._plot);
539 ps._plot.addEventListener("plot", (event, data) => {
540 ps.data = data;
541 this.dispatchEvent("plot:" + id);
542 });
543
544 this.plots[id] = ps;
545 return ps;
546 }
547
548 plot_single(id, metric) {
549 const ps = this._get(id);
550 ps.plot_single(metric);
551 }
552
553 plot_instances(id, metric, insts, reset) {
554 this._get(id).plot_instances(metric, insts, reset);
555 }
556
557 data(id) {
558 return this.plots[id] && this.plots[id].data;
559 }
560 }