|
11 | 11 | const CHART_LEGEND_FORMAT = new Intl.DateTimeFormat(undefined, {dateStyle: 'short', timeStyle: 'medium'}).format; |
12 | 12 |
|
13 | 13 | class Perfdatagraphs extends Icinga.EventListener { |
14 | | - // data contains the fetched chart data with the element ID where it is rendered as key. |
15 | | - // Where we store data in between the autorefresh. |
16 | | - data = new Map(); |
17 | 14 | // plots contains the chart objects with the element ID where it is rendered as key. |
18 | 15 | // Used for resizing the charts. |
19 | 16 | plots = new Map(); |
|
30 | 27 | // when their respective .container changes size. |
31 | 28 | this.resizeObserver = new ResizeObserver(entries => { |
32 | 29 | for (let elem of entries) { |
33 | | - const _plots = this.plots.get(elem.target) ?? []; |
34 | | - const s = this.getChartSize(elem.contentRect.width); |
35 | | - for (let u of _plots) { |
36 | | - u.setSize(s); |
| 30 | + const plot = this.plots.get(elem.target); |
| 31 | + if (plot !== undefined) { |
| 32 | + const s = this.getChartSize(elem.contentRect.width); |
| 33 | + plot.setSize(s); |
37 | 34 | } |
38 | 35 | } |
39 | 36 | }); |
|
50 | 47 | let _this = event.data.self; |
51 | 48 |
|
52 | 49 | if (!isAutorefresh) { |
| 50 | + _this.icinga.logger.debug('perfdatagraphs', 'not an autorefresh. resetting'); |
53 | 51 | // Reset the selection and set the duration when it's |
54 | 52 | // an autorefresh and new data is being loaded. |
55 | 53 | // _this.currentSelect = {min: 0, max: 0}; |
56 | 54 | _this.currentSelect = null; |
57 | 55 | // 1: value, 2: warning, 3: critical |
58 | 56 | _this.currentSeriesShow = {}; |
59 | 57 | _this.currentCursor = null; |
60 | | - _this.data = new Map(); |
61 | 58 | } |
62 | 59 |
|
63 | | - // Now we fetch |
64 | | - _this.fetchData(); |
65 | | - // ...and render in case we already have data |
66 | | - _this.renderCharts(); |
67 | | - } |
68 | | - |
69 | | - /** |
70 | | - * fetchData tries to get the data for the given object from the Controller. |
71 | | - */ |
72 | | - fetchData() |
73 | | - { |
74 | | - var _this = this; |
| 60 | + // Remove leftover eventhandlers and uPlot instances |
| 61 | + _this.plots.forEach((plot, element) => { |
| 62 | + plot.destroy(); |
| 63 | + }); |
| 64 | + // Then, reset the existing plots map for the new rendering |
| 65 | + _this.plots = new Map(); |
75 | 66 |
|
76 | 67 | // Get the elements we going to render the charts in |
77 | 68 | const lineCharts = document.querySelectorAll(CHART_CLASS); |
| 69 | + |
78 | 70 | // Check if the elements exist, just to be safe |
79 | 71 | if (lineCharts.length < 1) { |
80 | 72 | return; |
81 | 73 | } |
82 | 74 |
|
83 | | - _this.icinga.logger.debug('perfdatagraphs', 'start fetchData', lineCharts); |
84 | | - |
85 | | - for (let elem of lineCharts) { |
86 | | - const perfdata = JSON.parse(elem.getAttribute('data-perfdata')); |
87 | | - |
88 | | - _this.data.set(elem.getAttribute('id'), perfdata); |
89 | | - _this.renderCharts(); |
90 | | - } |
| 75 | + _this.renderCharts(lineCharts); |
91 | 76 | } |
92 | 77 |
|
93 | 78 | /** |
|
228 | 213 | /** |
229 | 214 | * renderCharts creates the canvas objects given the provided datasets. |
230 | 215 | */ |
231 | | - renderCharts() |
| 216 | + renderCharts(lineCharts) |
232 | 217 | { |
233 | 218 | // Get the colors from these sneaky little HTML elements |
234 | 219 | const axesColor = $('div.axes-color').css('background-color'); |
|
238 | 223 | // These are the shared options for all charts |
239 | 224 | const baseOpts = this.getChartBaseOptions(); |
240 | 225 |
|
241 | | - // Remove leftover eventhandlers and uPlot instances |
242 | | - this.plots.forEach((plots, element, map) => { |
243 | | - plots.forEach((plot) => { |
244 | | - plot.destroy(); |
245 | | - }); |
246 | | - }); |
247 | | - |
248 | | - // Reset the existing plots map for the new rendering |
249 | | - this.plots = new Map(); |
| 226 | + this.icinga.logger.debug('perfdatagraphs', 'start renderCharts'); |
250 | 227 |
|
251 | | - this.icinga.logger.debug('perfdatagraphs', 'start renderCharts', this.data); |
| 228 | + for (let elem of lineCharts) { |
| 229 | + this.icinga.logger.debug('perfdatagraphs', 'rendering for', elem); |
252 | 230 |
|
253 | | - this.data.forEach((dataset, elemID, map) => { |
254 | | - // Get the element in which we render the chart |
255 | | - const elem = document.getElementById(elemID); |
256 | | - |
257 | | - if (elem === null) { |
258 | | - return; |
259 | | - } |
| 231 | + const dataset = JSON.parse(elem.getAttribute('data-perfdata')); |
260 | 232 |
|
261 | 233 | // The size can vary from chart to chart for example when |
262 | 234 | // there are two contains on the page. |
|
355 | 327 | } |
356 | 328 |
|
357 | 329 | // Add the chart to the map which we use for the resize observer |
358 | | - const _plots = this.plots.get(elem) || []; |
359 | | - |
360 | | - _plots.push(u) |
361 | | - |
362 | | - this.plots.set(elem, _plots); |
363 | | - }); |
| 330 | + this.plots.set(elem, u); |
| 331 | + } |
364 | 332 |
|
365 | | - this.icinga.logger.debug('perfdatagraphs', 'finish renderCharts', this.plots); |
| 333 | + this.icinga.logger.debug('perfdatagraphs', 'finish renderCharts'); |
366 | 334 | } |
367 | 335 |
|
368 | 336 | /** |
|
0 commit comments