import React, { SyntheticEvent } from 'react'; import { CoreApp, SelectableValue } from '@grafana/data'; import { EditorRow, EditorField, EditorSwitch } from '@grafana/experimental'; import { RadioButtonGroup, Select } from '@grafana/ui'; import { getQueryTypeChangeHandler, getQueryTypeOptions } from '../../components/PromExploreExtraField'; import { FORMAT_OPTIONS, INTERVAL_FACTOR_OPTIONS } from '../../components/PromQueryEditor'; import { PromQuery } from '../../types'; import { AutoSizeInput } from '../shared/AutoSizeInput'; import { QueryOptionGroup } from '../shared/QueryOptionGroup'; import { getLegendModeLabel, PromQueryLegendEditor } from './PromQueryLegendEditor'; export interface UIOptions { exemplars: boolean; type: boolean; format: boolean; minStep: boolean; legend: boolean; resolution: boolean; } export interface Props { query: PromQuery; app?: CoreApp; onChange: (update: PromQuery) => void; onRunQuery: () => void; } export const PromQueryBuilderOptions = React.memo(({ query, app, onChange, onRunQuery }) => { const onChangeFormat = (value: SelectableValue) => { onChange({ ...query, format: value.value }); onRunQuery(); }; const onChangeStep = (evt: React.FormEvent) => { onChange({ ...query, interval: evt.currentTarget.value }); onRunQuery(); }; const queryTypeOptions = getQueryTypeOptions(app === CoreApp.Explore); const onQueryTypeChange = getQueryTypeChangeHandler(query, onChange); const onExemplarChange = (event: SyntheticEvent) => { const isEnabled = event.currentTarget.checked; onChange({ ...query, exemplar: isEnabled }); onRunQuery(); }; const onIntervalFactorChange = (value: SelectableValue) => { onChange({ ...query, intervalFactor: value.value }); onRunQuery(); }; const formatOption = FORMAT_OPTIONS.find((option) => option.value === query.format) || FORMAT_OPTIONS[0]; const queryTypeValue = getQueryTypeValue(query); const queryTypeLabel = queryTypeOptions.find((x) => x.value === queryTypeValue)!.label; return ( onChange({ ...query, legendFormat })} onRunQuery={onRunQuery} /> An additional lower limit for the step parameter of the Prometheus query and for the{' '} $__interval and $__rate_interval variables. } > option.value === query.intervalFactor)} /> )} ); }); function shouldShowExemplarSwitch(query: PromQuery, app?: CoreApp) { if (app === CoreApp.UnifiedAlerting || !query.range) { return false; } return true; } function getQueryTypeValue(query: PromQuery) { return query.range && query.instant ? 'both' : query.instant ? 'instant' : 'range'; } function getCollapsedInfo(query: PromQuery, formatOption: string, queryType: string): string[] { const items: string[] = []; items.push(`Legend: ${getLegendModeLabel(query.legendFormat)}`); items.push(`Format: ${formatOption}`); items.push(`Step: ${query.interval ?? 'auto'}`); items.push(`Type: ${queryType}`); if (query.exemplar) { items.push(`Exemplars: true`); } return items; } PromQueryBuilderOptions.displayName = 'PromQueryBuilderOptions';