From 76c586c467dfba810acbcc6c6778745c9b9f1a00 Mon Sep 17 00:00:00 2001 From: vargburz Date: Thu, 9 Jun 2022 13:27:02 +0300 Subject: [PATCH 1/4] add checks and errors --- src/components/editors/UseMetricEditor.tsx | 16 +++++++++------- src/models/options.ts | 10 ++++++++-- src/models/series.ts | 2 +- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/components/editors/UseMetricEditor.tsx b/src/components/editors/UseMetricEditor.tsx index 4018e26..d91d1cc 100644 --- a/src/components/editors/UseMetricEditor.tsx +++ b/src/components/editors/UseMetricEditor.tsx @@ -8,7 +8,7 @@ import React from 'react'; import * as _ from 'lodash'; type UseMetricConfig = { - useMetric: boolean; + useMetric?: boolean; value?: number; metricName?: string; }; @@ -18,10 +18,12 @@ const fieldNamePickerSettings = { } as any; export function UseMetricEditor({ onChange, value, context }: StandardEditorProps) { - const config = value; + let config = value; const onFieldChange = (field: keyof UseMetricConfig, value: any) => { - // @ts-ignore + if(_.isNil(config)) { + config = {}; + } config[field] = value; onChange(config); @@ -31,22 +33,22 @@ export function UseMetricEditor({ onChange, value, context }: StandardEditorProp onFieldChange('useMetric', (evt.target as any).checked)} /> - {config.useMetric ? ( + {config?.useMetric ? ( onFieldChange('metricName', newVal)} item={fieldNamePickerSettings} /> ) : ( onFieldChange('value', (evt.target as any).value)} /> diff --git a/src/models/options.ts b/src/models/options.ts index f150d29..f63a74b 100644 --- a/src/models/options.ts +++ b/src/models/options.ts @@ -21,7 +21,10 @@ export class Options { } private _setMin(): void { - if (!this.grafanaOptions.gauge.min.useMetric) { + if (!this.grafanaOptions.gauge.min) { + throw new Error(`Min Config is not selected: [See options: Extremum -> Min]`); + } + if (!this.grafanaOptions.gauge.min?.useMetric) { this.minValue = this.grafanaOptions.gauge.min.value; return; } @@ -30,7 +33,10 @@ export class Options { } private _setMax(): void { - if (!this.grafanaOptions.gauge.max.useMetric) { + if (!this.grafanaOptions.gauge.max) { + throw new Error(`Max Config is not selected: [See options: Extremum -> Max]`); + } + if (!this.grafanaOptions.gauge.max?.useMetric) { this.maxValue = this.grafanaOptions.gauge.max.value; return; } diff --git a/src/models/series.ts b/src/models/series.ts index 3155a82..30a22cb 100644 --- a/src/models/series.ts +++ b/src/models/series.ts @@ -10,7 +10,7 @@ export class Series { private _selectedSerieName; constructor(grafanaSeriesList: any, private gaugeValueOptions: ValueOptions) { - if (_.isEmpty(this.gaugeValueOptions.metricName)) { + if (_.isEmpty(this.gaugeValueOptions?.metricName)) { throw new Error(`Value: metric is not selected. [See options: Value -> Metric]`); } this._selectedSerieName = this.gaugeValueOptions.metricName; From 1fcad48a2ad37b6920cf411f1de3bb0a1328b453 Mon Sep 17 00:00:00 2001 From: vargburz Date: Thu, 9 Jun 2022 13:29:59 +0300 Subject: [PATCH 2/4] linter --- src/components/editors/UseMetricEditor.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/editors/UseMetricEditor.tsx b/src/components/editors/UseMetricEditor.tsx index d91d1cc..183b635 100644 --- a/src/components/editors/UseMetricEditor.tsx +++ b/src/components/editors/UseMetricEditor.tsx @@ -21,7 +21,7 @@ export function UseMetricEditor({ onChange, value, context }: StandardEditorProp let config = value; const onFieldChange = (field: keyof UseMetricConfig, value: any) => { - if(_.isNil(config)) { + if (_.isNil(config)) { config = {}; } config[field] = value; From 3bab6996e7df5f36791314b2c0b4c8b3617c203d Mon Sep 17 00:00:00 2001 From: vargburz Date: Thu, 9 Jun 2022 14:32:12 +0300 Subject: [PATCH 3/4] add default values --- src/components/editors/UseMetricEditor.tsx | 8 +++----- src/module.ts | 3 ++- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/components/editors/UseMetricEditor.tsx b/src/components/editors/UseMetricEditor.tsx index 183b635..d35b375 100644 --- a/src/components/editors/UseMetricEditor.tsx +++ b/src/components/editors/UseMetricEditor.tsx @@ -8,7 +8,7 @@ import React from 'react'; import * as _ from 'lodash'; type UseMetricConfig = { - useMetric?: boolean; + useMetric: boolean; value?: number; metricName?: string; }; @@ -18,12 +18,10 @@ const fieldNamePickerSettings = { } as any; export function UseMetricEditor({ onChange, value, context }: StandardEditorProps) { - let config = value; + let config: UseMetricConfig = value; const onFieldChange = (field: keyof UseMetricConfig, value: any) => { - if (_.isNil(config)) { - config = {}; - } + // @ts-ignore config[field] = value; onChange(config); diff --git a/src/module.ts b/src/module.ts index a67da81..de92132 100644 --- a/src/module.ts +++ b/src/module.ts @@ -49,12 +49,12 @@ export const plugin = new PanelPlugin(Panel).setPanelOptions((buil category: ['Value'], showIf: (config) => config.visualizationType === Pod.GAUGE, }) - // TODO: defaults? .addCustomEditor({ id: 'min', name: 'Min', path: 'gauge.min', category: ['Extremum'], + defaultValue: { useMetric: false, value: 0 }, showIf: (config) => config.visualizationType === Pod.GAUGE, editor: UseMetricEditor as any, }) @@ -63,6 +63,7 @@ export const plugin = new PanelPlugin(Panel).setPanelOptions((buil name: 'Max', path: 'gauge.max', category: ['Extremum'], + defaultValue: { useMetric: false }, showIf: (config) => config.visualizationType === Pod.GAUGE, editor: UseMetricEditor as any, }) From 405fec293b68580835fa58e578cf10f8f16a88a0 Mon Sep 17 00:00:00 2001 From: vargburz Date: Thu, 9 Jun 2022 14:36:54 +0300 Subject: [PATCH 4/4] upd version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8c56f20..c28797d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "grafana-chartwerk-panel", - "version": "0.4.0", + "version": "0.4.1", "description": "Chartwerk Panel", "scripts": { "build": "grafana-toolkit plugin:build",