Browse Source

Merge pull request 'fix minor bugs' (#4) from fix-minor-bugs into main

Reviewed-on: #4
pull/5/head
rozetko 9 months ago
parent
commit
bd5614e583
  1. 2
      src/components/Panel.tsx
  2. 6
      src/components/editors/ThresholdsEditor.tsx
  3. 6
      src/components/editors/UseMetricEditor.tsx
  4. 7
      src/utils.ts

2
src/components/Panel.tsx

@ -88,7 +88,6 @@ 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,
@ -96,7 +95,6 @@ export function Panel({ options, data, width, height, timeRange, onChangeTimeRan
);
value = aggregatedValue !== null ? aggregatedValue : undefined;
}
}
additionalInfo = (
<div

6
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
<InlineField label="Use metric">
<InlineSwitch
value={threshold.useMetric}
onChange={(evt) => onThresholdFieldChange(thresholdIdx, 'useMetric', (evt.target as any).checked)}
onChange={(evt) => onThresholdFieldChange(thresholdIdx, 'useMetric', evt.currentTarget.checked)}
/>
</InlineField>
<InlineField>
@ -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)}
/>
)}
</InlineField>

6
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
<InlineField label="Use metric">
<InlineSwitch
value={config?.useMetric}
onChange={(evt) => onFieldChange('useMetric', (evt.target as any).checked)}
onChange={(evt) => onFieldChange('useMetric', evt.currentTarget.checked)}
/>
</InlineField>
<InlineField>
@ -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)}
/>
)}
</InlineField>

7
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);
}

Loading…
Cancel
Save