Files
grafana/public/app/features/alerting/utils/dataSourceFromExpression.ts
Sonia Aguilar 3f26ffde94 Alerting: Add relativeTimeRange from dataSource when using Resample expresions (#56652)
* Fix: Add relativeTimeRange from dataSource when using Resample expression

* Add test for relativeTimeRange when updating resample expression

* update time range for expressions when data sources are updated

* Get data source recursively from expression up to the last query that this tree is referencing to

* Remove unnecessary produce from immer, and check if there is a cycle in the queries structure
2022-10-17 12:29:05 +02:00

35 lines
1.0 KiB
TypeScript

import { ExpressionDatasourceUID } from 'app/features/expressions/ExpressionDatasource';
import { AlertQuery } from 'app/types/unified-alerting-dto';
export const hasCyclicalReferences = (queries: AlertQuery[]) => {
try {
JSON.stringify(queries);
return false;
} catch (e) {
return true;
}
};
export const findDataSourceFromExpressionRecursive = (
queries: AlertQuery[],
alertQuery: AlertQuery
): AlertQuery | null | undefined => {
//Check if this is not cyclical structre
if (hasCyclicalReferences(queries)) {
return null;
}
// We have the data source in this dataQuery
if (alertQuery.datasourceUid !== ExpressionDatasourceUID) {
return alertQuery;
}
// alertQuery it's an expression, we have to traverse all the tree up to the data source
else {
const alertQueryReferenced = queries.find((alertQuery_) => alertQuery_.refId === alertQuery.model.expression);
if (alertQueryReferenced) {
return findDataSourceFromExpressionRecursive(queries, alertQueryReferenced);
} else {
return null;
}
}
};