mirror of https://github.com/grafana/grafana
parent
bc75bf4b91
commit
78da0e27e3
File diff suppressed because it is too large
Load Diff
@ -1,72 +0,0 @@ |
||||
import { css } from '@emotion/css'; |
||||
import React, { useState, useEffect } from 'react'; |
||||
|
||||
import { GrafanaTheme2, PanelData, QueryHint } from '@grafana/data'; |
||||
import { Button, Tooltip, useStyles2 } from '@grafana/ui'; |
||||
|
||||
import { PrometheusDatasource } from '../../datasource'; |
||||
import { promQueryModeller } from '../PromQueryModeller'; |
||||
import { buildVisualQueryFromString } from '../parsing'; |
||||
import { PromVisualQuery } from '../types'; |
||||
|
||||
export interface Props { |
||||
query: PromVisualQuery; |
||||
datasource: PrometheusDatasource; |
||||
onChange: (update: PromVisualQuery) => void; |
||||
data?: PanelData; |
||||
} |
||||
|
||||
export const PromQueryBuilderHints = React.memo<Props>(({ datasource, query, onChange, data }) => { |
||||
const [hints, setHints] = useState<QueryHint[]>([]); |
||||
const styles = useStyles2(getStyles); |
||||
|
||||
useEffect(() => { |
||||
const promQuery = { expr: promQueryModeller.renderQuery(query), refId: '' }; |
||||
// For now show only actionable hints
|
||||
const hints = datasource.getQueryHints(promQuery, data?.series || []).filter((hint) => hint.fix?.action); |
||||
setHints(hints); |
||||
}, [datasource, query, onChange, data, styles.hint]); |
||||
|
||||
return ( |
||||
<> |
||||
{hints.length > 0 && ( |
||||
<div className={styles.container}> |
||||
{hints.map((hint) => { |
||||
return ( |
||||
<Tooltip content={`${hint.label} ${hint.fix?.label}`} key={hint.type}> |
||||
<Button |
||||
onClick={() => { |
||||
const promQuery = { expr: promQueryModeller.renderQuery(query), refId: '' }; |
||||
const newPromQuery = datasource.modifyQuery(promQuery, hint!.fix!.action); |
||||
const visualQuery = buildVisualQueryFromString(newPromQuery.expr); |
||||
return onChange(visualQuery.query); |
||||
}} |
||||
fill="outline" |
||||
size="sm" |
||||
className={styles.hint} |
||||
> |
||||
{'hint: ' + hint.fix?.action?.type.toLowerCase().replace('_', ' ') + '()'} |
||||
</Button> |
||||
</Tooltip> |
||||
); |
||||
})} |
||||
</div> |
||||
)} |
||||
</> |
||||
); |
||||
}); |
||||
|
||||
PromQueryBuilderHints.displayName = 'PromQueryBuilderHints'; |
||||
|
||||
const getStyles = (theme: GrafanaTheme2) => { |
||||
return { |
||||
container: css` |
||||
display: flex; |
||||
margin-bottom: ${theme.spacing(1)}; |
||||
align-items: center; |
||||
`,
|
||||
hint: css` |
||||
margin-right: ${theme.spacing(1)}; |
||||
`,
|
||||
}; |
||||
}; |
@ -0,0 +1,75 @@ |
||||
import { css } from '@emotion/css'; |
||||
import React, { useState, useEffect } from 'react'; |
||||
|
||||
import { GrafanaTheme2, PanelData, QueryHint } from '@grafana/data'; |
||||
import { Button, Tooltip, useStyles2 } from '@grafana/ui'; |
||||
import { LokiDatasource } from 'app/plugins/datasource/loki/datasource'; |
||||
|
||||
import { PrometheusDatasource } from '../../datasource'; |
||||
|
||||
import { LokiAndPromQueryModellerBase, PromLokiVisualQuery } from './LokiAndPromQueryModellerBase'; |
||||
|
||||
export interface Props { |
||||
query: PromLokiVisualQuery; |
||||
datasource: PrometheusDatasource | LokiDatasource; |
||||
queryModeller: LokiAndPromQueryModellerBase; |
||||
buildVisualQueryFromString: (expr: string) => { query: PromLokiVisualQuery }; |
||||
onChange: (update: PromLokiVisualQuery) => void; |
||||
data?: PanelData; |
||||
} |
||||
|
||||
export const QueryBuilderHints = React.memo<Props>( |
||||
({ datasource, query: visualQuery, onChange, data, queryModeller, buildVisualQueryFromString }) => { |
||||
const [hints, setHints] = useState<QueryHint[]>([]); |
||||
const styles = useStyles2(getStyles); |
||||
|
||||
useEffect(() => { |
||||
const query = { expr: queryModeller.renderQuery(visualQuery), refId: '' }; |
||||
// For now show only actionable hints
|
||||
const hints = datasource.getQueryHints(query, data?.series || []).filter((hint) => hint.fix?.action); |
||||
setHints(hints); |
||||
}, [datasource, visualQuery, data, queryModeller]); |
||||
|
||||
return ( |
||||
<> |
||||
{hints.length > 0 && ( |
||||
<div className={styles.container}> |
||||
{hints.map((hint) => { |
||||
return ( |
||||
<Tooltip content={`${hint.label} ${hint.fix?.label}`} key={hint.type}> |
||||
<Button |
||||
onClick={() => { |
||||
const query = { expr: queryModeller.renderQuery(visualQuery), refId: '' }; |
||||
const newQuery = datasource.modifyQuery(query, hint!.fix!.action); |
||||
const newVisualQuery = buildVisualQueryFromString(newQuery.expr); |
||||
return onChange(newVisualQuery.query); |
||||
}} |
||||
fill="outline" |
||||
size="sm" |
||||
className={styles.hint} |
||||
> |
||||
{'hint: ' + hint.fix?.action?.type.toLowerCase().replace('_', ' ') + '()'} |
||||
</Button> |
||||
</Tooltip> |
||||
); |
||||
})} |
||||
</div> |
||||
)} |
||||
</> |
||||
); |
||||
} |
||||
); |
||||
|
||||
QueryBuilderHints.displayName = 'QueryBuilderHints'; |
||||
|
||||
const getStyles = (theme: GrafanaTheme2) => { |
||||
return { |
||||
container: css` |
||||
display: flex; |
||||
align-items: start; |
||||
`,
|
||||
hint: css` |
||||
margin-right: ${theme.spacing(1)}; |
||||
`,
|
||||
}; |
||||
}; |
Loading…
Reference in new issue