diff --git a/public/app/features/alerting/unified/components/rule-editor/QueryEditor.tsx b/public/app/features/alerting/unified/components/rule-editor/QueryEditor.tsx index dbbbbd686f7..14df01f64c9 100644 --- a/public/app/features/alerting/unified/components/rule-editor/QueryEditor.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/QueryEditor.tsx @@ -19,14 +19,14 @@ import { import { getNextRefIdChar } from 'app/core/utils/query'; import { defaultCondition } from 'app/features/expressions/utils/expressionTypes'; import { ExpressionQueryType } from 'app/features/expressions/types'; -import { GrafanaQuery } from 'app/types/unified-alerting-dto'; +import { AlertQuery } from 'app/types/unified-alerting-dto'; import { AlertingQueryRunner } from '../../state/AlertingQueryRunner'; import { getDatasourceSrv } from 'app/features/plugins/datasource_srv'; import { isExpressionQuery } from 'app/features/expressions/guards'; interface Props { - value?: GrafanaQuery[]; - onChange: (queries: GrafanaQuery[]) => void; + value?: AlertQuery[]; + onChange: (queries: AlertQuery[]) => void; } interface State { @@ -34,7 +34,7 @@ interface State { } export class QueryEditor extends PureComponent { private runner: AlertingQueryRunner; - private queries: GrafanaQuery[]; + private queries: AlertQuery[]; constructor(props: Props) { super(props); @@ -62,12 +62,12 @@ export class QueryEditor extends PureComponent { this.runner.cancel(); }; - onChangeQueries = (queries: GrafanaQuery[]) => { + onChangeQueries = (queries: AlertQuery[]) => { this.queries = queries; this.props.onChange(queries); }; - onDuplicateQuery = (query: GrafanaQuery) => { + onDuplicateQuery = (query: AlertQuery) => { const { queries } = this; this.onChangeQueries(addQuery(queries, query)); }; @@ -184,13 +184,10 @@ export class QueryEditor extends PureComponent { } } -const addQuery = ( - queries: GrafanaQuery[], - queryToAdd: Pick -): GrafanaQuery[] => { +const addQuery = (queries: AlertQuery[], queryToAdd: Pick): AlertQuery[] => { const refId = getNextRefIdChar(queries); - const query: GrafanaQuery = { + const query: AlertQuery = { ...queryToAdd, refId, queryType: '', diff --git a/public/app/features/alerting/unified/components/rule-editor/QueryRows.tsx b/public/app/features/alerting/unified/components/rule-editor/QueryRows.tsx index 0023c41b39d..70dc339a4cd 100644 --- a/public/app/features/alerting/unified/components/rule-editor/QueryRows.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/QueryRows.tsx @@ -3,16 +3,16 @@ import { DragDropContext, Droppable, DropResult } from 'react-beautiful-dnd'; import { DataQuery, DataSourceInstanceSettings, PanelData, RelativeTimeRange } from '@grafana/data'; import { getDataSourceSrv } from '@grafana/runtime'; import { QueryWrapper } from './QueryWrapper'; -import { GrafanaQuery } from 'app/types/unified-alerting-dto'; +import { AlertQuery } from 'app/types/unified-alerting-dto'; interface Props { // The query configuration - queries: GrafanaQuery[]; + queries: AlertQuery[]; data: Record; // Query editing - onQueriesChange: (queries: GrafanaQuery[]) => void; - onDuplicateQuery: (query: GrafanaQuery) => void; + onQueriesChange: (queries: AlertQuery[]) => void; + onDuplicateQuery: (query: AlertQuery) => void; onRunQueries: () => void; } @@ -119,14 +119,14 @@ export class QueryRows extends PureComponent { onQueriesChange(update); }; - onDuplicateQuery = (query: DataQuery, source: GrafanaQuery): void => { + onDuplicateQuery = (query: DataQuery, source: AlertQuery): void => { this.props.onDuplicateQuery({ ...source, model: query, }); }; - getDataSourceSettings = (query: GrafanaQuery): DataSourceInstanceSettings | undefined => { + getDataSourceSettings = (query: AlertQuery): DataSourceInstanceSettings | undefined => { return getDataSourceSrv().getInstanceSettings(query.datasourceUid); }; diff --git a/public/app/features/alerting/unified/components/rule-editor/QueryWrapper.tsx b/public/app/features/alerting/unified/components/rule-editor/QueryWrapper.tsx index 8c19c8d6540..5ec80a68442 100644 --- a/public/app/features/alerting/unified/components/rule-editor/QueryWrapper.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/QueryWrapper.tsx @@ -14,18 +14,18 @@ import { QueryEditorRow } from 'app/features/query/components/QueryEditorRow'; import { VizWrapper } from './VizWrapper'; import { isExpressionQuery } from 'app/features/expressions/guards'; import { TABLE, TIMESERIES } from '../../utils/constants'; -import { GrafanaQuery } from 'app/types/unified-alerting-dto'; +import { AlertQuery } from 'app/types/unified-alerting-dto'; interface Props { data: PanelData; - query: GrafanaQuery; - queries: GrafanaQuery[]; + query: AlertQuery; + queries: AlertQuery[]; dsSettings: DataSourceInstanceSettings; onChangeDataSource: (settings: DataSourceInstanceSettings, index: number) => void; onChangeQuery: (query: DataQuery, index: number) => void; onChangeTimeRange?: (timeRange: RelativeTimeRange, index: number) => void; onRemoveQuery: (query: DataQuery) => void; - onDuplicateQuery: (query: GrafanaQuery) => void; + onDuplicateQuery: (query: AlertQuery) => void; onRunQueries: () => void; index: number; } @@ -49,7 +49,7 @@ export const QueryWrapper: FC = ({ const isExpression = isExpressionQuery(query.model); const [pluginId, changePluginId] = useState(isExpression ? TABLE : TIMESERIES); - const renderTimePicker = (query: GrafanaQuery, index: number): ReactNode => { + const renderTimePicker = (query: AlertQuery, index: number): ReactNode => { if (isExpressionQuery(query.model) || !onChangeTimeRange) { return null; } diff --git a/public/app/features/alerting/unified/mocks/grafana-queries.ts b/public/app/features/alerting/unified/mocks/grafana-queries.ts deleted file mode 100644 index ac9a0448cd4..00000000000 --- a/public/app/features/alerting/unified/mocks/grafana-queries.ts +++ /dev/null @@ -1,57 +0,0 @@ -export const SAMPLE_QUERIES = [ - { - refId: 'A', - queryType: '', - relativeTimeRange: { - from: 21600, - to: 0, - }, - datasourceUid: '6_hUDNQGz', - model: { - intervalMs: 1000, - maxDataPoints: 100, - pulseWave: { - offCount: 6, - offValue: 1, - onCount: 6, - onValue: 10, - timeStep: 5, - }, - refId: 'A', - scenarioId: 'predictable_pulse', - stringInput: '', - }, - }, - { - refId: 'B', - queryType: '', - relativeTimeRange: { - from: 0, - to: 0, - }, - datasourceUid: '-100', - model: { - conditions: [ - { - evaluator: { - params: [3], - type: 'gt', - }, - operator: { - type: 'and', - }, - query: { - params: ['A'], - }, - reducer: { - type: 'last', - }, - }, - ], - intervalMs: 1000, - maxDataPoints: 100, - refId: 'B', - type: 'classic_conditions', - }, - }, -]; diff --git a/public/app/features/alerting/unified/state/AlertingQueryRunner.test.ts b/public/app/features/alerting/unified/state/AlertingQueryRunner.test.ts index 9e250aba688..68ea5be3962 100644 --- a/public/app/features/alerting/unified/state/AlertingQueryRunner.test.ts +++ b/public/app/features/alerting/unified/state/AlertingQueryRunner.test.ts @@ -10,7 +10,7 @@ import { } from '@grafana/data'; import { FetchResponse } from '@grafana/runtime'; import { BackendSrv } from 'app/core/services/backend_srv'; -import { GrafanaQuery } from 'app/types/unified-alerting-dto'; +import { AlertQuery } from 'app/types/unified-alerting-dto'; import { Observable, of, throwError } from 'rxjs'; import { delay, take } from 'rxjs/operators'; import { createFetchResponse } from 'test/helpers/createFetchResponse'; @@ -232,7 +232,7 @@ const createDataFrameJSON = (values: number[]): DataFrameJSON => { }; }; -const createQuery = (refId: string): GrafanaQuery => { +const createQuery = (refId: string): AlertQuery => { return { refId, queryType: '', diff --git a/public/app/features/alerting/unified/state/AlertingQueryRunner.ts b/public/app/features/alerting/unified/state/AlertingQueryRunner.ts index 8bf5daaccc2..8d34bea192c 100644 --- a/public/app/features/alerting/unified/state/AlertingQueryRunner.ts +++ b/public/app/features/alerting/unified/state/AlertingQueryRunner.ts @@ -14,7 +14,7 @@ import { import { FetchResponse, toDataQueryError } from '@grafana/runtime'; import { BackendSrv, getBackendSrv } from 'app/core/services/backend_srv'; import { preProcessPanelData } from 'app/features/query/state/runRequest'; -import { GrafanaQuery } from 'app/types/unified-alerting-dto'; +import { AlertQuery } from 'app/types/unified-alerting-dto'; import { getTimeRangeForExpression } from '../utils/timeRange'; import { isExpressionQuery } from 'app/features/expressions/guards'; import { setStructureRevision } from 'app/features/query/state/processing/revision'; @@ -41,7 +41,7 @@ export class AlertingQueryRunner { return this.subject.asObservable(); } - run(queries: GrafanaQuery[]) { + run(queries: AlertQuery[]) { if (queries.length === 0) { const empty = initialState(queries, LoadingState.Done); return this.subject.next(empty); @@ -99,7 +99,7 @@ export class AlertingQueryRunner { } } -const runRequest = (backendSrv: BackendSrv, queries: GrafanaQuery[]): Observable> => { +const runRequest = (backendSrv: BackendSrv, queries: AlertQuery[]): Observable> => { const initial = initialState(queries, LoadingState.Loading); const request = { data: { data: queries }, @@ -119,7 +119,7 @@ const runRequest = (backendSrv: BackendSrv, queries: GrafanaQuery[]): Observable }); }; -const initialState = (queries: GrafanaQuery[], state: LoadingState): Record => { +const initialState = (queries: AlertQuery[], state: LoadingState): Record => { return queries.reduce((dataByQuery: Record, query) => { dataByQuery[query.refId] = { state, @@ -131,7 +131,7 @@ const initialState = (queries: GrafanaQuery[], state: LoadingState): Record { +const getTimeRange = (query: AlertQuery, queries: AlertQuery[]): TimeRange => { if (isExpressionQuery(query.model)) { const relative = getTimeRangeForExpression(query.model, queries); return rangeUtil.relativeToTimeRange(relative); diff --git a/public/app/features/alerting/unified/types/preview.ts b/public/app/features/alerting/unified/types/preview.ts index c46fea30851..e94891a656c 100644 --- a/public/app/features/alerting/unified/types/preview.ts +++ b/public/app/features/alerting/unified/types/preview.ts @@ -1,5 +1,5 @@ import { PanelData } from '@grafana/data'; -import { GrafanaQuery } from 'app/types/unified-alerting-dto'; +import { AlertQuery } from 'app/types/unified-alerting-dto'; import { RuleFormType } from './rule-form'; export type PreviewRuleRequest = GrafanaPreviewRuleRequest | CloudPreviewRuleRequest; @@ -7,7 +7,7 @@ export type PreviewRuleRequest = GrafanaPreviewRuleRequest | CloudPreviewRuleReq export type GrafanaPreviewRuleRequest = { grafana_condition: { condition: string; - data: GrafanaQuery[]; + data: AlertQuery[]; now: string; }; }; diff --git a/public/app/features/alerting/unified/types/rule-form.ts b/public/app/features/alerting/unified/types/rule-form.ts index 2c7dc691c52..1044b0e5787 100644 --- a/public/app/features/alerting/unified/types/rule-form.ts +++ b/public/app/features/alerting/unified/types/rule-form.ts @@ -1,4 +1,4 @@ -import { GrafanaQuery, GrafanaAlertStateDecision } from 'app/types/unified-alerting-dto'; +import { AlertQuery, GrafanaAlertStateDecision } from 'app/types/unified-alerting-dto'; export enum RuleFormType { grafana = 'grafana', @@ -15,7 +15,7 @@ export interface RuleFormValues { annotations: Array<{ key: string; value: string }>; // grafana rules - queries: GrafanaQuery[]; + queries: AlertQuery[]; condition: string | null; // refId of the query that gets alerted on noDataState: GrafanaAlertStateDecision; execErrState: GrafanaAlertStateDecision; diff --git a/public/app/features/alerting/unified/utils/rule-form.ts b/public/app/features/alerting/unified/utils/rule-form.ts index 4f967b06f78..4868d938453 100644 --- a/public/app/features/alerting/unified/utils/rule-form.ts +++ b/public/app/features/alerting/unified/utils/rule-form.ts @@ -10,7 +10,7 @@ import { RuleWithLocation } from 'app/types/unified-alerting'; import { Annotations, GrafanaAlertStateDecision, - GrafanaQuery, + AlertQuery, Labels, PostableRuleGrafanaRuleDTO, RulerAlertingRuleDTO, @@ -135,7 +135,7 @@ export function rulerRuleToFormValues(ruleWithLocation: RuleWithLocation): RuleF } } -export const getDefaultQueries = (): GrafanaQuery[] => { +export const getDefaultQueries = (): AlertQuery[] => { const dataSource = getDataSourceSrv().getInstanceSettings('default'); if (!dataSource) { @@ -158,7 +158,7 @@ export const getDefaultQueries = (): GrafanaQuery[] => { ]; }; -const getDefaultExpression = (refId: string): GrafanaQuery => { +const getDefaultExpression = (refId: string): AlertQuery => { const model: ExpressionQuery = { refId, hide: false, @@ -197,13 +197,13 @@ const dataQueriesToGrafanaQueries = ( queries: DataQuery[], relativeTimeRange: RelativeTimeRange, datasourceName?: string -): GrafanaQuery[] => { - return queries.reduce((queries, target) => { +): AlertQuery[] => { + return queries.reduce((queries, target) => { const dsName = target.datasource || datasourceName; if (dsName) { // expressions if (dsName === ExpressionDatasourceID) { - const newQuery: GrafanaQuery = { + const newQuery: AlertQuery = { refId: target.refId, queryType: '', relativeTimeRange, @@ -215,7 +215,7 @@ const dataQueriesToGrafanaQueries = ( } else { const datasource = getDataSourceSrv().getInstanceSettings(target.datasource || datasourceName); if (datasource && datasource.meta.alerting) { - const newQuery: GrafanaQuery = { + const newQuery: AlertQuery = { refId: target.refId, queryType: target.queryType ?? '', relativeTimeRange, diff --git a/public/app/features/alerting/unified/utils/timeRange.test.ts b/public/app/features/alerting/unified/utils/timeRange.test.ts index 4fdf5cdaac3..b3d80a3b2a0 100644 --- a/public/app/features/alerting/unified/utils/timeRange.test.ts +++ b/public/app/features/alerting/unified/utils/timeRange.test.ts @@ -2,13 +2,13 @@ import { ReducerID } from '@grafana/data'; import { getTimeRangeForExpression } from './timeRange'; import { defaultCondition } from 'app/features/expressions/utils/expressionTypes'; import { ExpressionQuery, ExpressionQueryType } from 'app/features/expressions/types'; -import { GrafanaQuery } from 'app/types/unified-alerting-dto'; +import { AlertQuery } from 'app/types/unified-alerting-dto'; describe('timeRange', () => { describe('getTimeRangeForExpression', () => { describe('classic condition', () => { it('should return referenced query timeRange for classic condition', () => { - const expressionQuery: GrafanaQuery = { + const expressionQuery: AlertQuery = { refId: 'B', queryType: 'expression', datasourceUid: '-100', @@ -20,14 +20,14 @@ describe('timeRange', () => { type: ExpressionQueryType.classic, } as ExpressionQuery, }; - const query: GrafanaQuery = { + const query: AlertQuery = { refId: 'A', relativeTimeRange: { from: 300, to: 0 }, queryType: 'query', datasourceUid: 'dsuid', model: { refId: 'A' }, }; - const queries: GrafanaQuery[] = [query, expressionQuery]; + const queries: AlertQuery[] = [query, expressionQuery]; expect(getTimeRangeForExpression(expressionQuery.model as ExpressionQuery, queries)).toEqual({ from: 300, @@ -36,7 +36,7 @@ describe('timeRange', () => { }); it('should return the min and max time range', () => { - const expressionQuery: GrafanaQuery = { + const expressionQuery: AlertQuery = { refId: 'C', queryType: 'expression', datasourceUid: '-100', @@ -51,21 +51,21 @@ describe('timeRange', () => { type: ExpressionQueryType.classic, } as ExpressionQuery, }; - const queryA: GrafanaQuery = { + const queryA: AlertQuery = { refId: 'A', relativeTimeRange: { from: 300, to: 0 }, datasourceUid: 'dsuid', model: { refId: 'A' }, queryType: 'query', }; - const queryB: GrafanaQuery = { + const queryB: AlertQuery = { refId: 'B', relativeTimeRange: { from: 600, to: 300 }, datasourceUid: 'dsuid', model: { refId: 'B' }, queryType: 'query', }; - const queries: GrafanaQuery[] = [queryA, queryB, expressionQuery]; + const queries: AlertQuery[] = [queryA, queryB, expressionQuery]; expect(getTimeRangeForExpression(expressionQuery.model as ExpressionQuery, queries)).toEqual({ from: 600, @@ -76,7 +76,7 @@ describe('timeRange', () => { }); describe('math', () => { it('should get timerange for referenced query', () => { - const expressionQuery: GrafanaQuery = { + const expressionQuery: AlertQuery = { refId: 'B', queryType: 'expression', datasourceUid: '-100', @@ -89,7 +89,7 @@ describe('timeRange', () => { } as ExpressionQuery, }; - const query: GrafanaQuery = { + const query: AlertQuery = { refId: 'A', datasourceUid: 'dsuid', relativeTimeRange: { from: 300, to: 0 }, @@ -101,7 +101,7 @@ describe('timeRange', () => { }); it('should get time ranges for multiple referenced queries', () => { - const expressionQuery: GrafanaQuery = { + const expressionQuery: AlertQuery = { refId: 'C', queryType: 'expression', datasourceUid: '-100', @@ -114,7 +114,7 @@ describe('timeRange', () => { } as ExpressionQuery, }; - const queryA: GrafanaQuery = { + const queryA: AlertQuery = { refId: 'A', relativeTimeRange: { from: 300, to: 0 }, datasourceUid: 'dsuid', @@ -122,7 +122,7 @@ describe('timeRange', () => { queryType: 'query', }; - const queryB: GrafanaQuery = { + const queryB: AlertQuery = { refId: 'queryB', relativeTimeRange: { from: 600, to: 300 }, datasourceUid: 'dsuid', @@ -138,7 +138,7 @@ describe('timeRange', () => { describe('resample', () => { it('should get referenced timerange for resample expression', () => { - const expressionQuery: GrafanaQuery = { + const expressionQuery: AlertQuery = { refId: 'B', queryType: 'expression', datasourceUid: '-100', @@ -152,7 +152,7 @@ describe('timeRange', () => { } as ExpressionQuery, }; - const queryA: GrafanaQuery = { + const queryA: AlertQuery = { refId: 'A', relativeTimeRange: { from: 300, to: 0 }, datasourceUid: 'dsuid', @@ -171,7 +171,7 @@ describe('timeRange', () => { describe('reduce', () => { it('should get referenced timerange for reduce expression', () => { - const expressionQuery: GrafanaQuery = { + const expressionQuery: AlertQuery = { refId: 'B', queryType: 'expression', datasourceUid: '-100', @@ -185,7 +185,7 @@ describe('timeRange', () => { } as ExpressionQuery, }; - const queryA: GrafanaQuery = { + const queryA: AlertQuery = { refId: 'A', relativeTimeRange: { from: 300, to: 0 }, datasourceUid: 'dsuid', diff --git a/public/app/features/alerting/unified/utils/timeRange.ts b/public/app/features/alerting/unified/utils/timeRange.ts index d5331ab9eb7..1e335b4ebcd 100644 --- a/public/app/features/alerting/unified/utils/timeRange.ts +++ b/public/app/features/alerting/unified/utils/timeRange.ts @@ -1,10 +1,10 @@ import { RelativeTimeRange } from '@grafana/data'; -import { GrafanaQuery } from 'app/types/unified-alerting-dto'; +import { AlertQuery } from 'app/types/unified-alerting-dto'; import { ExpressionQuery, ExpressionQueryType } from '../../../expressions/types'; const FALL_BACK_TIME_RANGE = { from: 21600, to: 0 }; -export const getTimeRangeForExpression = (query: ExpressionQuery, queries: GrafanaQuery[]): RelativeTimeRange => { +export const getTimeRangeForExpression = (query: ExpressionQuery, queries: AlertQuery[]): RelativeTimeRange => { const referencedRefIds: string[] | undefined = getReferencedIds(query, queries); if (!referencedRefIds) { @@ -23,7 +23,7 @@ export const getTimeRangeForExpression = (query: ExpressionQuery, queries: Grafa }; }; -const getReferencedIds = (model: ExpressionQuery, queries: GrafanaQuery[]): string[] | undefined => { +const getReferencedIds = (model: ExpressionQuery, queries: AlertQuery[]): string[] | undefined => { switch (model.type) { case ExpressionQueryType.classic: return getReferencedIdsForClassicCondition(model); @@ -41,7 +41,7 @@ const getReferencedIdsForClassicCondition = (model: ExpressionQuery) => { }); }; -const getTimeRanges = (referencedRefIds: string[], queries: GrafanaQuery[]) => { +const getTimeRanges = (referencedRefIds: string[], queries: AlertQuery[]) => { let from: number[] = []; let to = [FALL_BACK_TIME_RANGE.to]; for (const referencedRefIdsKey of referencedRefIds) { @@ -60,7 +60,7 @@ const getTimeRanges = (referencedRefIds: string[], queries: GrafanaQuery[]) => { }; }; -const getReferencedIdsForMath = (model: ExpressionQuery, queries: GrafanaQuery[]) => { +const getReferencedIdsForMath = (model: ExpressionQuery, queries: AlertQuery[]) => { return ( queries // filter queries of type query and filter expression on if it includes any refIds diff --git a/public/app/types/unified-alerting-dto.ts b/public/app/types/unified-alerting-dto.ts index a4fb7dc8ae8..6ed43aa6829 100644 --- a/public/app/types/unified-alerting-dto.ts +++ b/public/app/types/unified-alerting-dto.ts @@ -101,7 +101,7 @@ export enum GrafanaAlertStateDecision { OK = 'OK', } -export interface GrafanaQuery { +export interface AlertQuery { refId: string; queryType: string; relativeTimeRange?: RelativeTimeRange; @@ -115,7 +115,7 @@ export interface PostableGrafanaRuleDefinition { condition: string; no_data_state: GrafanaAlertStateDecision; exec_err_state: GrafanaAlertStateDecision; - data: GrafanaQuery[]; + data: AlertQuery[]; } export interface GrafanaRuleDefinition extends PostableGrafanaRuleDefinition { uid: string;