-
Notifications
You must be signed in to change notification settings - Fork 11.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 component
- Loading branch information
Showing
6 changed files
with
253 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
public/app/features/alerting/unified/components/rule-editor/RecordingRuleEditor.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)}; | ||
`, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.