mirror of
https://github.com/grafana/grafana.git
synced 2024-12-24 16:10:22 -06:00
Alerting: Fix Recording Rules creation issues (#90362)
This commit is contained in:
parent
8a6107cd35
commit
5d8ca38b9b
@ -6,7 +6,10 @@ import { PanelData, CoreApp, GrafanaTheme2, LoadingState } from '@grafana/data';
|
||||
import { getDataSourceSrv } from '@grafana/runtime';
|
||||
import { DataQuery } from '@grafana/schema';
|
||||
import { useStyles2 } from '@grafana/ui';
|
||||
import { DataSourceType } from 'app/features/alerting/unified/utils/datasource';
|
||||
import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
||||
import { QueryErrorAlert } from 'app/features/query/components/QueryErrorAlert';
|
||||
import { LokiQueryType } from 'app/plugins/datasource/loki/dataquery.gen';
|
||||
import { AlertQuery } from 'app/types/unified-alerting-dto';
|
||||
|
||||
import { isPromOrLokiQuery } from '../../utils/rule-form';
|
||||
@ -49,13 +52,13 @@ export const RecordingRuleEditor: FC<RecordingRuleEditorProps> = ({
|
||||
}, [dataSourceName]);
|
||||
|
||||
const handleChangedQuery = (changedQuery: DataQuery) => {
|
||||
const query = queries[0];
|
||||
const dataSourceId = getDataSourceSrv().getInstanceSettings(dataSourceName)?.uid;
|
||||
|
||||
if (!isPromOrLokiQuery(changedQuery) || !dataSourceId) {
|
||||
if (!isPromOrLokiQuery(changedQuery) || !dataSource) {
|
||||
return;
|
||||
}
|
||||
|
||||
const [query] = queries;
|
||||
const { uid: dataSourceId, type } = dataSource;
|
||||
const isLoki = type === DataSourceType.Loki;
|
||||
const expr = changedQuery.expr;
|
||||
|
||||
const merged = {
|
||||
@ -72,7 +75,10 @@ export const RecordingRuleEditor: FC<RecordingRuleEditorProps> = ({
|
||||
instant: changedQuery.instant,
|
||||
range: changedQuery.range,
|
||||
// Query type is used by Loki queries
|
||||
queryType: changedQuery.queryType,
|
||||
// On first render/when creating a recording rule, the query type is not set
|
||||
// unless the user has changed it betwee range/instant. The cleanest way to handle this
|
||||
// is to default to instant, or whatever the changed type is
|
||||
queryType: isLoki ? changedQuery.queryType || LokiQueryType.Instant : changedQuery.queryType,
|
||||
legendFormat: changedQuery.legendFormat,
|
||||
},
|
||||
};
|
||||
@ -95,14 +101,19 @@ export const RecordingRuleEditor: FC<RecordingRuleEditorProps> = ({
|
||||
return (
|
||||
<>
|
||||
{queries.length && (
|
||||
<QueryEditor
|
||||
query={queries[0]}
|
||||
queries={queries}
|
||||
app={CoreApp.UnifiedAlerting}
|
||||
onChange={handleChangedQuery}
|
||||
onRunQuery={runQueries}
|
||||
datasource={dataSource}
|
||||
/>
|
||||
<>
|
||||
<QueryEditor
|
||||
query={queries[0]}
|
||||
queries={queries}
|
||||
app={CoreApp.UnifiedAlerting}
|
||||
onChange={handleChangedQuery}
|
||||
onRunQuery={runQueries}
|
||||
datasource={dataSource}
|
||||
/>
|
||||
{(data?.errors || []).map((err) => {
|
||||
return <QueryErrorAlert key={err.message} error={err} />;
|
||||
})}
|
||||
</>
|
||||
)}
|
||||
|
||||
{data && (
|
||||
|
@ -391,7 +391,7 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange }: P
|
||||
<RecordingRuleEditor
|
||||
dataSourceName={dataSourceName}
|
||||
queries={queries}
|
||||
runQueries={runQueriesPreview}
|
||||
runQueries={() => runQueriesPreview()}
|
||||
onChangeQuery={onChangeRecordingRulesQueries}
|
||||
panelData={queryPreviewData}
|
||||
/>
|
||||
|
Loading…
Reference in New Issue
Block a user