From 2cd07c32733072f0fa2b92f49834ac9ddded16cd Mon Sep 17 00:00:00 2001 From: Stephan de Wit Date: Thu, 15 Aug 2024 15:44:46 +0200 Subject: [PATCH] dashboard: CPU widget graph selection (https://github.com/opnsense/core/issues/7672) --- .../www/js/opnsense_widget_manager.js | 9 +-- src/opnsense/www/js/widgets/Cpu.js | 71 +++++++++++++------ src/opnsense/www/js/widgets/Metadata/Core.xml | 5 +- src/opnsense/www/js/widgets/Traffic.js | 2 +- 4 files changed, 57 insertions(+), 30 deletions(-) diff --git a/src/opnsense/www/js/opnsense_widget_manager.js b/src/opnsense/www/js/opnsense_widget_manager.js index fcf7b100c..2f37a727f 100644 --- a/src/opnsense/www/js/opnsense_widget_manager.js +++ b/src/opnsense/www/js/opnsense_widget_manager.js @@ -629,6 +629,7 @@ class WidgetManager { // parse widget options const options = await widget.getWidgetOptions(); + const config = await widget.getWidgetConfig(); for (const [key, value] of Object.entries(options)) { let $option = $(`
`); switch (value.type) { @@ -640,12 +641,8 @@ class WidgetManager { multiple="multiple">`); for (const option of value.options) { - $select.append($(``)); - } - - if (value.options.every(obj => !obj.selected)) { - // No selection, apply the default. - $select.val(value.default); + let selected = config[key].includes(option.value); + $select.append($(``)); } $option.append($(`
${value.title}
`)); diff --git a/src/opnsense/www/js/widgets/Cpu.js b/src/opnsense/www/js/widgets/Cpu.js index 0ab273126..617bad0d6 100644 --- a/src/opnsense/www/js/widgets/Cpu.js +++ b/src/opnsense/www/js/widgets/Cpu.js @@ -27,9 +27,11 @@ import BaseWidget from 'widget-base'; export default class Cpu extends BaseWidget { - constructor() { - super(); + constructor(config) { + super(config); this.resizeHandles = "e, w"; + this.configurable = true; + this.graphs = ['total', 'intr', 'user', 'sys']; } _createChart(selector, timeSeries) { @@ -57,24 +59,46 @@ export default class Cpu extends BaseWidget { }); } + async getWidgetOptions() { + return { + graphs: { + title: this.translations.graphs, + type: 'select_multiple', + options: ['total', 'intr', 'user', 'sys'].map((value) => { + return { + value: value, + label: this.translations[value] + } + }), + default: ['total'] + } + } + } + + onWidgetOptionsChanged(options) { + this.graphs.filter(x => !options.graphs.includes(x)).forEach(graph => $(`#cpu-${graph}`).hide()); + this.graphs = options.graphs; + this.graphs.forEach(graph => $(`#cpu-${graph}`).show()); + } + getMarkup() { let $container = $(`
-
+
${this.translations.total} -
+
-
- ${this.translations.interrupt} +
+ ${this.translations.intr}
-
+
${this.translations.user}
-
- ${this.translations.system} +
+ ${this.translations.sys}
`); @@ -86,24 +110,29 @@ export default class Cpu extends BaseWidget { const data = await this.ajaxCall('/api/diagnostics/cpu_usage/getcputype'); $('.cpu-type').text(data); - let total_ts = new TimeSeries(); - let intr_ts = new TimeSeries(); - let user_ts = new TimeSeries(); - let sys_ts = new TimeSeries(); - this._createChart('cpu-usage', total_ts); - this._createChart('cpu-usage-intr', intr_ts); - this._createChart('cpu-usage-user', user_ts); - this._createChart('cpu-usage-sys', sys_ts); + const config = await this.getWidgetConfig(); + + let ts = {}; + this.graphs.forEach((graph) => { + let timeSeries = new TimeSeries(); + this._createChart(`cpu-usage-${graph}`, timeSeries); + ts[graph] = timeSeries; + + if (!config.graphs.includes(graph)) { + // hide canvas container + $(`#cpu-${graph}`).hide(); + } + }); + super.openEventSource('/api/diagnostics/cpu_usage/stream', (event) => { if (!event) { super.closeEventSource(); } const data = JSON.parse(event.data); let date = Date.now(); - total_ts.append(date, data.total); - intr_ts.append(date, data.intr); - user_ts.append(date, data.user); - sys_ts.append(date, data.sys); + this.graphs.forEach((graph) => { + ts[graph].append(date, data[graph]); + }); }); } diff --git a/src/opnsense/www/js/widgets/Metadata/Core.xml b/src/opnsense/www/js/widgets/Metadata/Core.xml index 528019037..2a2c8b750 100644 --- a/src/opnsense/www/js/widgets/Metadata/Core.xml +++ b/src/opnsense/www/js/widgets/Metadata/Core.xml @@ -43,8 +43,9 @@ CPU Total User - System - Interrupt + System + Interrupt + Graphs diff --git a/src/opnsense/www/js/widgets/Traffic.js b/src/opnsense/www/js/widgets/Traffic.js index f9815ade9..e2c04e197 100644 --- a/src/opnsense/www/js/widgets/Traffic.js +++ b/src/opnsense/www/js/widgets/Traffic.js @@ -225,7 +225,7 @@ export default class Traffic extends BaseWidget { options: Object.entries(interfaces.interfaces).map(([key,intf]) => { return { value: intf.name, - selected: this.config.widget?.interfaces?.includes(intf.name) ?? false + label: intf.name, }; }), default: Object.entries(interfaces.interfaces)