Files
grafana/public/app/features/alerting/unified/utils/dataSourceFromExpression.ts
Gilles De Mey 8765c48389 Alerting: Remove legacy alerting (#83671)
Removes legacy alerting, so long and thanks for all the fish! 🐟

---------

Co-authored-by: Matthew Jacobson <matthew.jacobson@grafana.com>
Co-authored-by: Sonia Aguilar <soniaAguilarPeiron@users.noreply.github.com>
Co-authored-by: Armand Grillet <armandgrillet@users.noreply.github.com>
Co-authored-by: William Wernert <rwwiv@users.noreply.github.com>
Co-authored-by: Yuri Tseretyan <yuriy.tseretyan@grafana.com>
2024-03-14 15:36:35 +01:00

35 lines
1.0 KiB
TypeScript

import { ExpressionDatasourceUID } from 'app/features/expressions/types';
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;
}
}
};