import { css } from '@emotion/css'; import * as React from 'react'; import { useToggle } from 'react-use'; import { GrafanaTheme2 } from '@grafana/data'; import { EditorField, EditorRow } from '@grafana/plugin-ui'; import { AutoSizeInput, RadioButtonGroup, useStyles2 } from '@grafana/ui'; import { QueryOptionGroup } from '../_importedDependencies/datasources/prometheus/QueryOptionGroup'; import { SearchTableType, MetricsQueryType } from '../dataquery.gen'; import { DEFAULT_LIMIT, DEFAULT_SPSS } from '../datasource'; import { TempoQuery } from '../types'; interface Props { onChange: (value: TempoQuery) => void; query: Partial & TempoQuery; searchStreaming: boolean; metricsStreaming: boolean; } /** * Parse a string value to integer. If the conversion fails, for example because we are prosessing an empty value for * a field, return a fallback (default) value. * * @param val the value to be parsed to an integer * @param fallback the fallback value * @returns the converted value or the fallback value if the conversion fails */ const parseIntWithFallback = (val: string, fallback: number) => { const parsed = parseInt(val, 10); return isNaN(parsed) ? fallback : parsed; }; export const TempoQueryBuilderOptions = React.memo(({ onChange, query, searchStreaming, metricsStreaming }) => { const styles = useStyles2(getStyles); const [isOpen, toggleOpen] = useToggle(false); if (!query.hasOwnProperty('limit')) { query.limit = DEFAULT_LIMIT; } if (!query.hasOwnProperty('tableType')) { query.tableType = SearchTableType.Traces; } if (!query.hasOwnProperty('metricsQueryType')) { query.metricsQueryType = MetricsQueryType.Range; } const onLimitChange = (e: React.FormEvent) => { onChange({ ...query, limit: parseIntWithFallback(e.currentTarget.value, DEFAULT_LIMIT) }); }; const onSpssChange = (e: React.FormEvent) => { onChange({ ...query, spss: parseIntWithFallback(e.currentTarget.value, DEFAULT_SPSS) }); }; const onTableTypeChange = (val: SearchTableType) => { onChange({ ...query, tableType: val }); }; const onMetricsQueryTypeChange = (val: MetricsQueryType) => { onChange({ ...query, metricsQueryType: val }); }; const onStepChange = (e: React.FormEvent) => { onChange({ ...query, step: e.currentTarget.value }); }; // There's a bug in Tempo which causes the exemplars param to be ignored. It's commented out for now. // const onExemplarsChange = (e: React.FormEvent) => { // const exemplars = parseInt(e.currentTarget.value, 10); // if (!isNaN(exemplars) && exemplars >= 0) { // onChange({ ...query, exemplars }); // } else { // onChange({ ...query, exemplars: undefined }); // } // }; const collapsedSearchOptions = [ `Limit: ${query.limit || DEFAULT_LIMIT}`, `Spans Limit: ${query.spss || DEFAULT_SPSS}`, `Table Format: ${query.tableType === SearchTableType.Traces ? 'Traces' : 'Spans'}`, '|', `Streaming: ${searchStreaming ? 'Enabled' : 'Disabled'}`, ]; const collapsedMetricsOptions = [ `Step: ${query.step || 'auto'}`, `Type: ${query.metricsQueryType === MetricsQueryType.Range ? 'Range' : 'Instant'}`, '|', `Streaming: ${metricsStreaming ? 'Enabled' : 'Disabled'}`, // `Exemplars: ${query.exemplars !== undefined ? query.exemplars : 'auto'}`, ]; return (
} tooltipInteractive>
{searchStreaming ? 'Enabled' : 'Disabled'}
} tooltipInteractive>
{metricsStreaming ? 'Enabled' : 'Disabled'}
{/**/} {/* */} {/**/}
); }); const StreamingTooltip = () => { return (
Indicates if streaming is currently enabled. Streaming allows you to view partial query results before the entire query completes. Learn more
); }; TempoQueryBuilderOptions.displayName = 'TempoQueryBuilderOptions'; const getStyles = (theme: GrafanaTheme2) => { return { options: css({ display: 'flex', width: '-webkit-fill-available', gap: theme.spacing(1), '> div': { width: 'auto', }, }), }; };