diff --git a/packages/grafana-prometheus/src/querybuilder/components/PromQueryCodeEditor.tsx b/packages/grafana-prometheus/src/querybuilder/components/PromQueryCodeEditor.tsx index 0942b93ff88..9ad1b409456 100644 --- a/packages/grafana-prometheus/src/querybuilder/components/PromQueryCodeEditor.tsx +++ b/packages/grafana-prometheus/src/querybuilder/components/PromQueryCodeEditor.tsx @@ -7,6 +7,7 @@ import { useStyles2 } from '@grafana/ui'; import { PromQueryField } from '../../components/PromQueryField'; import { PromQueryEditorProps } from '../../components/types'; +import { QueryEditorHints } from '../shared/QueryEditorHints'; import { PromQueryBuilderExplained } from './PromQueryBuilderExplained'; @@ -33,8 +34,8 @@ export function PromQueryCodeEditor(props: PromQueryCodeEditorProps) { data={data} app={app} /> - {showExplain && } + ); } diff --git a/packages/grafana-prometheus/src/querybuilder/shared/QueryEditorHints.tsx b/packages/grafana-prometheus/src/querybuilder/shared/QueryEditorHints.tsx new file mode 100644 index 00000000000..fa69a1c1eaa --- /dev/null +++ b/packages/grafana-prometheus/src/querybuilder/shared/QueryEditorHints.tsx @@ -0,0 +1,63 @@ +import { css } from '@emotion/css'; +import { useEffect, useState } from 'react'; + +import { GrafanaTheme2, QueryHint } from '@grafana/data'; +import { reportInteraction } from '@grafana/runtime'; +import { Button, Tooltip, useStyles2 } from '@grafana/ui'; + +import { PromQueryEditorProps } from '../../components/types'; + +export function QueryEditorHints(props: PromQueryEditorProps) { + const [hints, setHints] = useState([]); + const { query, data, datasource } = props; + const styles = useStyles2(getStyles); + + useEffect(() => { + const promQuery = { expr: query.expr, refId: query.refId }; + const hints = datasource.getQueryHints(promQuery, data?.series || []).filter((hint) => hint.fix?.action); + setHints(hints); + }, [datasource, data, query]); + + return ( + <> + {hints.length > 0 && ( +
+ {hints.map((hint) => { + return ( + + + + ); + })} +
+ )} + + ); +} + +function onHintButtonClick(hint: QueryHint, props: PromQueryEditorProps) { + reportInteraction('grafana_query_builder_hints_clicked', { + hint: hint.type, + datasourceType: props.datasource.type, + }); + + if (hint.fix?.action) { + const newQuery = props.datasource.modifyQuery(props.query, hint.fix.action); + return props.onChange(newQuery); + } +} + +const getStyles = (theme: GrafanaTheme2) => { + return { + container: css({ + display: 'flex', + alignItems: 'start', + }), + hint: css({ + marginRight: theme.spacing(1), + padEnd: theme.spacing(2), + }), + }; +};