From 4056372a4f63d0a7864d1d207153df1e88a7ac43 Mon Sep 17 00:00:00 2001 From: rozetko Date: Thu, 4 Apr 2024 17:21:29 +0300 Subject: [PATCH] fix minor bugs: - useMetric -> false: convert input value to number - useMetric -> true: do not throw when metric is not selected --- src/components/Panel.tsx | 14 ++++++-------- src/components/editors/ThresholdsEditor.tsx | 6 ++++-- src/components/editors/UseMetricEditor.tsx | 6 ++++-- src/utils.ts | 7 +++++++ 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/components/Panel.tsx b/src/components/Panel.tsx index b2b2603..d25052f 100644 --- a/src/components/Panel.tsx +++ b/src/components/Panel.tsx @@ -88,14 +88,12 @@ export function Panel({ options, data, width, height, timeRange, onChangeTimeRan if (!additionalInfoConfig.value?.useMetric) { value = additionalInfoConfig.value.value; } else { - if (!_.isEmpty(additionalInfoConfig.value.metricName)) { - const aggregatedValue = getLastMetricValue( - grafanaSeriesList, - additionalInfoConfig.value.metricName, - 'Additional Info' - ); - value = aggregatedValue !== null ? aggregatedValue : undefined; - } + const aggregatedValue = getLastMetricValue( + grafanaSeriesList, + additionalInfoConfig.value.metricName, + 'Additional Info' + ); + value = aggregatedValue !== null ? aggregatedValue : undefined; } additionalInfo = ( diff --git a/src/components/editors/ThresholdsEditor.tsx b/src/components/editors/ThresholdsEditor.tsx index 354b4d3..ea6b11a 100644 --- a/src/components/editors/ThresholdsEditor.tsx +++ b/src/components/editors/ThresholdsEditor.tsx @@ -1,5 +1,7 @@ import { FieldNamePicker } from '../../grafana/MatchersUI/FieldNamePicker'; +import { isNumber } from '../../utils'; + import { GrafanaTheme, StandardEditorProps } from '@grafana/data'; import { Button, @@ -97,7 +99,7 @@ export function ThresholdsEditor({ onChange, value, context }: StandardEditorPro onThresholdFieldChange(thresholdIdx, 'useMetric', (evt.target as any).checked)} + onChange={(evt) => onThresholdFieldChange(thresholdIdx, 'useMetric', evt.currentTarget.checked)} /> @@ -113,7 +115,7 @@ export function ThresholdsEditor({ onChange, value, context }: StandardEditorPro placeholder="value" width={24} value={threshold.value} - onChange={(evt) => onThresholdFieldChange(thresholdIdx, 'value', (evt.target as any).value)} + onChange={(evt) => onThresholdFieldChange(thresholdIdx, 'value', isNumber(evt.currentTarget.value) ? +evt.currentTarget.value : undefined)} /> )} diff --git a/src/components/editors/UseMetricEditor.tsx b/src/components/editors/UseMetricEditor.tsx index d35b375..8d51fa8 100644 --- a/src/components/editors/UseMetricEditor.tsx +++ b/src/components/editors/UseMetricEditor.tsx @@ -1,5 +1,7 @@ import { FieldNamePicker } from '../../grafana/MatchersUI/FieldNamePicker'; +import { isNumber } from '../../utils'; + import { StandardEditorProps } from '@grafana/data'; import { HorizontalGroup, InlineField, InlineSwitch, Input } from '@grafana/ui'; @@ -32,7 +34,7 @@ export function UseMetricEditor({ onChange, value, context }: StandardEditorProp onFieldChange('useMetric', (evt.target as any).checked)} + onChange={(evt) => onFieldChange('useMetric', evt.currentTarget.checked)} /> @@ -48,7 +50,7 @@ export function UseMetricEditor({ onChange, value, context }: StandardEditorProp placeholder="value" value={config?.value} width={24} - onChange={(evt) => onFieldChange('value', (evt.target as any).value)} + onChange={(evt) => onFieldChange('value', isNumber(evt.currentTarget.value) ? +evt.currentTarget.value : undefined)} /> )} diff --git a/src/utils.ts b/src/utils.ts index 1a29ba3..eda0983 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -26,6 +26,9 @@ export function getLastMetricValue( metricName: string | undefined, optionName: string ): number | null { + if(metricName === undefined) { + return null; + } // optionName -> helper in Error, mb use option path instead const filteredSeries = filterMetricListByAlias(grafanaSeriesList, metricName, optionName); const serie = filteredSeries[0]; @@ -66,3 +69,7 @@ export function getAggregatedValueFromSerie( throw new Error(`Unknown aggregation type: ${aggregation}`); } } + +export function isNumber(value: string): boolean { + return !isNaN(parseFloat(value)) && isFinite(value as unknown as number); +}