Adds proxy support to NetData widgets. 🎆 First commit of 2022 :)

This commit is contained in:
Alicia Sykes 2022-01-01 00:40:29 +00:00
parent 395aea292e
commit eb4e45dfb5
3 changed files with 49 additions and 56 deletions

View File

@ -9,13 +9,6 @@ import ChartingMixin from '@/mixins/ChartingMixin';
export default { export default {
mixins: [WidgetMixin, ChartingMixin], mixins: [WidgetMixin, ChartingMixin],
components: {}, components: {},
data() {
return {
chartTitle: null,
chartData: null,
chartDom: null,
};
},
computed: { computed: {
/* URL where NetData is hosted */ /* URL where NetData is hosted */
netDataHost() { netDataHost() {
@ -45,38 +38,39 @@ export default {
); );
}, },
/* Assign data variables to the returned data */ /* Assign data variables to the returned data */
processData(data) { processData(inputData) {
const timeData = []; const { labels, data } = inputData;
const systemCpu = []; const timeData = []; // List of timestamps for axis
const userCpu = []; const resultGroup = {}; // List of datasets, for each label
data.data.reverse().forEach((reading) => { data.reverse().forEach((reading) => {
timeData.push(this.formatDate(reading[0] * 1000)); labels.forEach((label, indx) => {
systemCpu.push(reading[2]); if (indx === 0) { // First value is the timestamp, add to axis
userCpu.push(reading[3]); timeData.push(this.formatTime(reading[indx] * 1000));
} else { // All other values correspond to a label
if (!resultGroup[label]) resultGroup[label] = [];
resultGroup[label].push(reading[indx]);
}
}); });
this.chartData = { });
labels: timeData, const datasets = [];
datasets: [ Object.keys(resultGroup).forEach((label) => {
{ name: 'System CPU', type: 'bar', values: systemCpu }, datasets.push({ name: label, type: 'bar', values: resultGroup[label] });
{ name: 'User CPU', type: 'bar', values: userCpu }, });
], const timeChartData = { labels: timeData, datasets };
}; const chartTitle = this.makeChartTitle(data);
this.chartTitle = this.makeChartTitle(data.data); this.generateChart(timeChartData, chartTitle);
this.renderChart();
}, },
makeChartTitle(data) { makeChartTitle(data) {
if (!data || !data[0][0]) return ''; const prefix = this.$t('widgets.net-data.cpu-chart-title');
if (!data || !data[0][0]) return prefix;
const diff = Math.round((data[data.length - 1][0] - data[0][0]) / 60); const diff = Math.round((data[data.length - 1][0] - data[0][0]) / 60);
return `Past ${diff} minutes`; return `${prefix}: Past ${diff} minutes`;
},
renderChart() {
this.chartDom = this.generateChart();
}, },
/* Create new chart, using the crypto data */ /* Create new chart, using the crypto data */
generateChart() { generateChart(timeChartData, chartTitle) {
return new this.Chart(`#${this.chartId}`, { return new this.Chart(`#${this.chartId}`, {
title: this.chartTitle, title: chartTitle,
data: this.chartData, data: timeChartData,
type: 'axis-mixed', type: 'axis-mixed',
height: this.chartHeight, height: this.chartHeight,
colors: this.chartColors, colors: this.chartColors,

View File

@ -3,19 +3,13 @@
</template> </template>
<script> <script>
import axios from 'axios';
import WidgetMixin from '@/mixins/WidgetMixin'; import WidgetMixin from '@/mixins/WidgetMixin';
import ChartingMixin from '@/mixins/ChartingMixin'; import ChartingMixin from '@/mixins/ChartingMixin';
export default { export default {
mixins: [WidgetMixin, ChartingMixin], mixins: [WidgetMixin, ChartingMixin],
components: {}, components: {},
data() {
return {
chartTitle: null,
chartData: null,
chartDom: null,
};
},
computed: { computed: {
/* URL where NetData is hosted */ /* URL where NetData is hosted */
netDataHost() { netDataHost() {
@ -30,7 +24,7 @@ export default {
return this.options.apiVersion || 'v1'; return this.options.apiVersion || 'v1';
}, },
endpoint() { endpoint() {
return `${this.netDataHost}/api/${this.apiVersion}/data?chart=system.cpu`; return `${this.netDataHost}/api/${this.apiVersion}/data?chart=system.load`;
}, },
/* A sudo-random ID for the chart DOM element */ /* A sudo-random ID for the chart DOM element */
chartId() { chartId() {
@ -40,9 +34,16 @@ export default {
methods: { methods: {
/* Make GET request to NetData */ /* Make GET request to NetData */
fetchData() { fetchData() {
this.makeRequest(this.endpoint).then( axios.get(this.endpoint)
(response) => { this.processData(response); }, .then((response) => {
); this.processData(response.data);
})
.catch((dataFetchError) => {
this.error('Unable to fetch data', dataFetchError);
})
.finally(() => {
this.finishLoading();
});
}, },
/* Assign data variables to the returned data */ /* Assign data variables to the returned data */
processData(data) { processData(data) {
@ -56,7 +57,7 @@ export default {
load5mins.push(reading[2]); load5mins.push(reading[2]);
load15mins.push(reading[3]); load15mins.push(reading[3]);
}); });
this.chartData = { const chartData = {
labels: timeData, labels: timeData,
datasets: [ datasets: [
{ name: '1 Min', type: 'bar', values: load1min }, { name: '1 Min', type: 'bar', values: load1min },
@ -64,22 +65,20 @@ export default {
{ name: '15 Mins', type: 'bar', values: load15mins }, { name: '15 Mins', type: 'bar', values: load15mins },
], ],
}; };
this.chartTitle = this.makeChartTitle(data.data); const chartTitle = this.makeChartTitle(data.data);
this.renderChart(); this.generateChart(chartData, chartTitle);
}, },
makeChartTitle(data) { makeChartTitle(data) {
if (!data || !data[0][0]) return ''; const prefix = this.$t('widgets.net-data.load-chart-title');
if (!data || !data[0][0]) return prefix;
const diff = Math.round((data[data.length - 1][0] - data[0][0]) / 60); const diff = Math.round((data[data.length - 1][0] - data[0][0]) / 60);
return `Past ${diff} minutes`; return `${prefix}: Past ${diff} minutes`;
},
renderChart() {
this.chartDom = this.generateChart();
}, },
/* Create new chart, using the crypto data */ /* Create new chart, using the crypto data */
generateChart() { generateChart(chartData, chartTitle) {
return new this.Chart(`#${this.chartId}`, { return new this.Chart(`#${this.chartId}`, {
title: this.chartTitle, title: chartTitle,
data: this.chartData, data: chartData,
type: 'axis-mixed', type: 'axis-mixed',
height: this.chartHeight, height: this.chartHeight,
colors: this.chartColors, colors: this.chartColors,

View File

@ -78,7 +78,7 @@ export default {
/* Create new chart, using the crypto data */ /* Create new chart, using the crypto data */
generateHistoryChart(timeChartData) { generateHistoryChart(timeChartData) {
return new this.Chart(`#${this.chartId}`, { return new this.Chart(`#${this.chartId}`, {
title: 'History', title: this.$t('widgets.net-data.mem-chart-title'),
data: timeChartData, data: timeChartData,
type: 'axis-mixed', type: 'axis-mixed',
height: this.chartHeight, height: this.chartHeight,
@ -99,7 +99,7 @@ export default {
}, },
generateAggregateChart(aggregateChartData) { generateAggregateChart(aggregateChartData) {
return new this.Chart(`#aggregate-${this.chartId}`, { return new this.Chart(`#aggregate-${this.chartId}`, {
title: 'Averages', title: this.$t('widgets.net-data.mem-breakdown-title'),
data: aggregateChartData, data: aggregateChartData,
type: 'percentage', type: 'percentage',
height: 100, height: 100,