From 6763e07951b6ec85996d7adc8698f381d7ca0f83 Mon Sep 17 00:00:00 2001 From: rozetko Date: Fri, 6 May 2022 23:00:44 +0400 Subject: [PATCH] Thresholds Editor UI --- src/components/editors/ThresholdsEditor.tsx | 143 ++++++++++++++++++++ src/module.ts | 18 ++- 2 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 src/components/editors/ThresholdsEditor.tsx diff --git a/src/components/editors/ThresholdsEditor.tsx b/src/components/editors/ThresholdsEditor.tsx new file mode 100644 index 0000000..4ff18f8 --- /dev/null +++ b/src/components/editors/ThresholdsEditor.tsx @@ -0,0 +1,143 @@ +import { FieldNamePicker } from '../../grafana/MatchersUI/FieldNamePicker'; + +import { StandardEditorProps } from '@grafana/data'; +import { Button, ColorPicker, HorizontalGroup, IconButton, InlineField, InlineSwitch, Input, ThemeContext } from '@grafana/ui'; + +import React from 'react'; + +import * as _ from 'lodash'; + + +interface Props { + defaultColor: string, + arcBackground: string, + thresholds: Array +} + +type ThresholdConfig = { + color: string; + useMetric: boolean; + value?: number; + metricName?: string; +} + +const DEFAULT_THRESHOLD: ThresholdConfig = { + color: '#378372d', + useMetric: false, + value: 0 +} + +const fieldNamePickerSettings = { + settings: { width: 24 }, +} as any; + +export function ThresholdsEditor({ onChange, value, context }: StandardEditorProps) { + const config = value; + + const addThreshold = () => { + config.thresholds.push(_.cloneDeep(DEFAULT_THRESHOLD)); + onChange(config); + } + + const removeThreshold = (idx: number) => { + config.thresholds.splice(idx, 1); + onChange(config); + } + + const onFieldChange = (field: keyof Props, value: any) => { + config[field] = value; + onChange(config); + } + + const onThresholdFieldChange = (thresholdIdx: number, field: keyof ThresholdConfig, value: any) => { + // @ts-ignore + config.thresholds[thresholdIdx][field] = value; + onChange(config); + }; + + return ( + + { + () => { + // const styles = getStyles(theme.v1); + return ( +
+ + onFieldChange('defaultColor', val)} + enableNamedColors={true} + /> +
+ } + /> + + onFieldChange('arcBackground', val) } + enableNamedColors={true} + /> + + } + /> + { + config.thresholds.map((threshold, thresholdIdx) => { + return ( + + + onThresholdFieldChange(thresholdIdx, 'color', newVal)} + enableNamedColors={true} + /> + + } + /> + + onThresholdFieldChange(thresholdIdx, 'useMetric', (evt.target as any).checked)} + /> + + { + threshold.useMetric ? + onThresholdFieldChange(thresholdIdx, 'metricName', newVal)} + item={fieldNamePickerSettings} + /> : + onThresholdFieldChange(thresholdIdx, 'value', (evt.target as any).value)} + /> + } + removeThreshold(thresholdIdx)}> + + ) + }) + } + + + ) + } + } +
+ ) +} diff --git a/src/module.ts b/src/module.ts index 4dd7a1b..658e5e8 100644 --- a/src/module.ts +++ b/src/module.ts @@ -1,8 +1,10 @@ import { PanelOptions, Pod } from './types'; + import { Panel } from './components/Panel'; +import { IconsEditor } from './components/editors/IconsEditor'; +import { ThresholdsEditor } from './components/editors/ThresholdsEditor'; import { PanelPlugin } from '@grafana/data'; -import { IconsEditor } from 'components/editors/IconsEditor'; export const plugin = new PanelPlugin(Panel).setPanelOptions((builder) => { @@ -89,4 +91,18 @@ export const plugin = new PanelPlugin(Panel).setPanelOptions((buil showIf: (config) => config.visualizationType === Pod.GAUGE, editor: IconsEditor as any, }) + + .addCustomEditor({ + id: 'thresholds', + path: 'gauge.thresholds', + name: 'Thresholds', + category: ['Thresholds'], + defaultValue: { + defaultColor: '#37872d', + arcBackground: 'rgba(38, 38, 38, 0.1)', + thresholds: [] + }, + showIf: (config) => config.visualizationType === Pod.GAUGE, + editor: ThresholdsEditor as any, + }) });