fix minor bugs #4

Merged
rozetko merged 1 commits from fix-minor-bugs into main 9 months ago
  1. 14
      src/components/Panel.tsx
  2. 6
      src/components/editors/ThresholdsEditor.tsx
  3. 6
      src/components/editors/UseMetricEditor.tsx
  4. 7
      src/utils.ts

14
src/components/Panel.tsx

@ -88,14 +88,12 @@ export function Panel({ options, data, width, height, timeRange, onChangeTimeRan
if (!additionalInfoConfig.value?.useMetric) { if (!additionalInfoConfig.value?.useMetric) {
value = additionalInfoConfig.value.value; value = additionalInfoConfig.value.value;
} else { } else {
if (!_.isEmpty(additionalInfoConfig.value.metricName)) { const aggregatedValue = getLastMetricValue(
const aggregatedValue = getLastMetricValue( grafanaSeriesList,
grafanaSeriesList, additionalInfoConfig.value.metricName,
additionalInfoConfig.value.metricName, 'Additional Info'
'Additional Info' );
); value = aggregatedValue !== null ? aggregatedValue : undefined;
value = aggregatedValue !== null ? aggregatedValue : undefined;
}
} }
additionalInfo = ( additionalInfo = (

6
src/components/editors/ThresholdsEditor.tsx

@ -1,5 +1,7 @@
import { FieldNamePicker } from '../../grafana/MatchersUI/FieldNamePicker'; import { FieldNamePicker } from '../../grafana/MatchersUI/FieldNamePicker';
import { isNumber } from '../../utils';
import { GrafanaTheme, StandardEditorProps } from '@grafana/data'; import { GrafanaTheme, StandardEditorProps } from '@grafana/data';
import { import {
Button, Button,
@ -97,7 +99,7 @@ export function ThresholdsEditor({ onChange, value, context }: StandardEditorPro
<InlineField label="Use metric"> <InlineField label="Use metric">
<InlineSwitch <InlineSwitch
value={threshold.useMetric} value={threshold.useMetric}
onChange={(evt) => onThresholdFieldChange(thresholdIdx, 'useMetric', (evt.target as any).checked)} onChange={(evt) => onThresholdFieldChange(thresholdIdx, 'useMetric', evt.currentTarget.checked)}
/> />
</InlineField> </InlineField>
<InlineField> <InlineField>
@ -113,7 +115,7 @@ export function ThresholdsEditor({ onChange, value, context }: StandardEditorPro
placeholder="value" placeholder="value"
width={24} width={24}
value={threshold.value} 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)}
/> />
)} )}
</InlineField> </InlineField>

6
src/components/editors/UseMetricEditor.tsx

@ -1,5 +1,7 @@
import { FieldNamePicker } from '../../grafana/MatchersUI/FieldNamePicker'; import { FieldNamePicker } from '../../grafana/MatchersUI/FieldNamePicker';
import { isNumber } from '../../utils';
import { StandardEditorProps } from '@grafana/data'; import { StandardEditorProps } from '@grafana/data';
import { HorizontalGroup, InlineField, InlineSwitch, Input } from '@grafana/ui'; import { HorizontalGroup, InlineField, InlineSwitch, Input } from '@grafana/ui';
@ -32,7 +34,7 @@ export function UseMetricEditor({ onChange, value, context }: StandardEditorProp
<InlineField label="Use metric"> <InlineField label="Use metric">
<InlineSwitch <InlineSwitch
value={config?.useMetric} value={config?.useMetric}
onChange={(evt) => onFieldChange('useMetric', (evt.target as any).checked)} onChange={(evt) => onFieldChange('useMetric', evt.currentTarget.checked)}
/> />
</InlineField> </InlineField>
<InlineField> <InlineField>
@ -48,7 +50,7 @@ export function UseMetricEditor({ onChange, value, context }: StandardEditorProp
placeholder="value" placeholder="value"
value={config?.value} value={config?.value}
width={24} width={24}
onChange={(evt) => onFieldChange('value', (evt.target as any).value)} onChange={(evt) => onFieldChange('value', isNumber(evt.currentTarget.value) ? +evt.currentTarget.value : undefined)}
/> />
)} )}
</InlineField> </InlineField>

7
src/utils.ts

@ -26,6 +26,9 @@ export function getLastMetricValue(
metricName: string | undefined, metricName: string | undefined,
optionName: string optionName: string
): number | null { ): number | null {
if(metricName === undefined) {
return null;
}
// optionName -> helper in Error, mb use option path instead // optionName -> helper in Error, mb use option path instead
const filteredSeries = filterMetricListByAlias(grafanaSeriesList, metricName, optionName); const filteredSeries = filterMetricListByAlias(grafanaSeriesList, metricName, optionName);
const serie = filteredSeries[0]; const serie = filteredSeries[0];
@ -66,3 +69,7 @@ export function getAggregatedValueFromSerie(
throw new Error(`Unknown aggregation type: ${aggregation}`); throw new Error(`Unknown aggregation type: ${aggregation}`);
} }
} }
export function isNumber(value: string): boolean {
return !isNaN(parseFloat(value)) && isFinite(value as unknown as number);
}

Loading…
Cancel
Save