// Libraries import React, { ChangeEvent, FormEvent, useMemo } from 'react'; import { useAsync } from 'react-use'; // Components import { selectors as editorSelectors } from '@grafana/e2e-selectors'; import { Input, InlineFieldRow, InlineField, Select, TextArea, Switch } from '@grafana/ui'; import { QueryEditorProps, SelectableValue } from '@grafana/data'; import { StreamingClientEditor, ManualEntryEditor, RandomWalkEditor } from './components'; // Types import { TestDataDataSource } from './datasource'; import { TestDataQuery, Scenario, NodesQuery } from './types'; import { PredictablePulseEditor } from './components/PredictablePulseEditor'; import { CSVWaveEditor } from './components/CSVWaveEditor'; import { defaultQuery } from './constants'; import { GrafanaLiveEditor } from './components/GrafanaLiveEditor'; import { NodeGraphEditor } from './components/NodeGraphEditor'; const showLabelsFor = ['random_walk', 'predictable_pulse', 'predictable_csv_wave']; const endpoints = [ { value: 'datasources', label: 'Data Sources' }, { value: 'search', label: 'Search' }, { value: 'annotations', label: 'Annotations' }, ]; const selectors = editorSelectors.components.DataSource.TestData.QueryTab; export interface EditorProps { onChange: (value: any) => void; query: TestDataQuery; } export type Props = QueryEditorProps; export const QueryEditor = ({ query, datasource, onChange, onRunQuery }: Props) => { query = { ...defaultQuery, ...query }; const { loading, value: scenarioList } = useAsync(async () => { return datasource.getScenarios(); }, []); const onUpdate = (query: TestDataQuery) => { onChange(query); onRunQuery(); }; const currentScenario = useMemo(() => scenarioList?.find(scenario => scenario.id === query.scenarioId), [ scenarioList, query, ]); const scenarioId = currentScenario?.id; const onScenarioChange = (item: SelectableValue) => { const scenario = scenarioList?.find(sc => sc.id === item.value); if (!scenario) { return; } const update = { ...query, scenarioId: item.value! }; if (scenario.stringInput) { update.stringInput = scenario.stringInput; } if (scenario.id === 'grafana_api') { update.stringInput = 'datasources'; } else if (scenario.id === 'streaming_client') { update.stringInput = ''; } else if (scenario.id === 'live') { if (!update.channel) { update.channel = 'random-2s-stream'; // default stream } } onUpdate(update); }; const onInputChange = (e: FormEvent) => { const { name, value, type } = e.target as HTMLInputElement | HTMLTextAreaElement; let newValue: any = value; if (type === 'number') { newValue = Number(value); } if (name === 'levelColumn') { newValue = (e.target as HTMLInputElement).checked; } onUpdate({ ...query, [name]: newValue }); }; const onFieldChange = (field: string) => (e: ChangeEvent) => { const { name, value, type } = e.target as HTMLInputElement; let newValue: any = value; if (type === 'number') { newValue = Number(value); } onUpdate({ ...query, [field]: { ...query[field as keyof TestDataQuery], [name]: newValue } }); }; const onEndPointChange = ({ value }: SelectableValue) => { onUpdate({ ...query, stringInput: value }); }; const onStreamClientChange = onFieldChange('stream'); const onPulseWaveChange = onFieldChange('pulseWave'); const onCSVWaveChange = onFieldChange('csvWave'); const options = useMemo( () => (scenarioList || []) .map(item => ({ label: item.name, value: item.id })) .sort((a, b) => a.label.localeCompare(b.label)), [scenarioList] ); const showLabels = useMemo(() => showLabelsFor.includes(query.scenarioId), [query]); if (loading) { return null; } return ( <> )} {showLabels && ( Set labels using a key=value syntax:
{`{ key = "value", key2 = "value" }`}
key="value", key2="value"
key=value, key2=value
} >
)}
{scenarioId === 'manual_entry' && } {scenarioId === 'random_walk' && } {scenarioId === 'streaming_client' && } {scenarioId === 'live' && } {scenarioId === 'logs' && ( )} {scenarioId === 'grafana_api' && (