mirror of https://github.com/grafana/grafana
Alerting: Enable preview for recording rules (#63260)
* Create RecordingRuleEditor component It reuses QueryEditor and propagates a few properties to allow to filter the visible datasources and customize what's shown in the editor header * Set recording rules queries as a new state prop Otherwise it would get mixed up with the alert rules queries when switching back and forth from this option. This also allows me to initialize these queries with the right datasource * Show CloudRulesSourcePicker only for Loki/Mimir rules As now we use the query editor for recording rules which already includes a datasource picker within * Fix lint and tests * Fix saving a recording rule * Show expression when editing the recording rule * Show query editor back for cloud rules * Fix duplicated import * Tweak after rebase * Remove ts-ignore * Refactor to use queries state instead of recordingRuleQueries * Refacrtor RecordingRuleEditor to use ds QueryEditor * Revert extra properties previously added to QueryEditor components * Remove console.log * Fix saving/editing a recording rule * Fix tests * Add margin to vizwrapper componentpull/65179/head
parent
e243db6e6b
commit
a1fc515c88
@ -0,0 +1,115 @@ |
||||
import { css } from '@emotion/css'; |
||||
import React, { FC, useEffect, useState } from 'react'; |
||||
import { useAsync } from 'react-use'; |
||||
|
||||
import { PanelData, CoreApp, GrafanaTheme2 } from '@grafana/data'; |
||||
import { getDataSourceSrv } from '@grafana/runtime'; |
||||
import { DataQuery, LoadingState } from '@grafana/schema'; |
||||
import { useStyles2 } from '@grafana/ui'; |
||||
import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv'; |
||||
import { isExpressionQuery } from 'app/features/expressions/guards'; |
||||
import { AlertQuery } from 'app/types/unified-alerting-dto'; |
||||
|
||||
import { TABLE, TIMESERIES } from '../../utils/constants'; |
||||
import { SupportedPanelPlugins } from '../PanelPluginsButtonGroup'; |
||||
|
||||
import { VizWrapper } from './VizWrapper'; |
||||
|
||||
export interface RecordingRuleEditorProps { |
||||
queries: AlertQuery[]; |
||||
onChangeQuery: (updatedQueries: AlertQuery[]) => void; |
||||
runQueries: (queries: AlertQuery[]) => void; |
||||
panelData: Record<string, PanelData>; |
||||
dataSourceName: string; |
||||
} |
||||
|
||||
export const RecordingRuleEditor: FC<RecordingRuleEditorProps> = ({ |
||||
queries, |
||||
onChangeQuery, |
||||
runQueries, |
||||
panelData, |
||||
dataSourceName, |
||||
}) => { |
||||
const [data, setData] = useState<PanelData>({ |
||||
series: [], |
||||
state: LoadingState.NotStarted, |
||||
timeRange: getTimeSrv().timeRange(), |
||||
}); |
||||
|
||||
const styles = useStyles2(getStyles); |
||||
|
||||
const isExpression = isExpressionQuery(queries[0]?.model); |
||||
|
||||
const [pluginId, changePluginId] = useState<SupportedPanelPlugins>(isExpression ? TABLE : TIMESERIES); |
||||
|
||||
useEffect(() => { |
||||
setData(panelData?.[queries[0]?.refId]); |
||||
}, [panelData, queries]); |
||||
|
||||
const { |
||||
error, |
||||
loading, |
||||
value: dataSource, |
||||
} = useAsync(() => { |
||||
return getDataSourceSrv().get(dataSourceName); |
||||
}, [dataSourceName]); |
||||
|
||||
const handleChangedQuery = (changedQuery: DataQuery) => { |
||||
const query = queries[0]; |
||||
|
||||
const merged = { |
||||
...query, |
||||
refId: changedQuery.refId, |
||||
queryType: query.model.queryType ?? '', |
||||
//@ts-ignore
|
||||
expr: changedQuery?.expr, |
||||
model: { |
||||
refId: changedQuery.refId, |
||||
//@ts-ignore
|
||||
expr: changedQuery?.expr, |
||||
editorMode: 'code', |
||||
}, |
||||
}; |
||||
onChangeQuery([merged]); |
||||
}; |
||||
|
||||
if (loading || dataSource?.name !== dataSourceName) { |
||||
return null; |
||||
} |
||||
|
||||
const dsi = getDataSourceSrv().getInstanceSettings(dataSourceName); |
||||
|
||||
if (error || !dataSource || !dataSource?.components?.QueryEditor || !dsi) { |
||||
const errorMessage = error?.message || 'Data source plugin does not export any Query Editor component'; |
||||
return <div>Could not load query editor due to: {errorMessage}</div>; |
||||
} |
||||
|
||||
const QueryEditor = dataSource.components.QueryEditor; |
||||
|
||||
return ( |
||||
<> |
||||
{queries.length && ( |
||||
<QueryEditor |
||||
query={queries[0]} |
||||
queries={queries} |
||||
app={CoreApp.UnifiedAlerting} |
||||
onChange={handleChangedQuery} |
||||
onRunQuery={() => runQueries(queries)} |
||||
datasource={dataSource} |
||||
/> |
||||
)} |
||||
|
||||
{data && ( |
||||
<div className={styles.vizWrapper}> |
||||
<VizWrapper data={data} currentPanel={pluginId} changePanel={changePluginId} /> |
||||
</div> |
||||
)} |
||||
</> |
||||
); |
||||
}; |
||||
|
||||
const getStyles = (theme: GrafanaTheme2) => ({ |
||||
vizWrapper: css` |
||||
margin: ${theme.spacing(1, 0)}; |
||||
`,
|
||||
}); |
Loading…
Reference in new issue