Alerting: Skip non-expression queries in DAG creation (#98315)

This commit is contained in:
Tom Ratcliffe 2024-12-20 16:13:32 +00:00 committed by GitHub
parent 1334caa6c8
commit d1846da0d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 8 deletions

View File

@ -35,30 +35,64 @@ describe('working with dag', () => {
type: 'math',
},
},
{
refId: 'D',
model: {
refId: 'D',
expression: 'B',
type: 'threshold',
},
},
] as AlertQuery[];
const dag = _createDagFromQueries(queries);
expect(Object.keys(dag.nodes)).toHaveLength(3);
expect(Object.keys(dag.nodes)).toHaveLength(4);
expect(() => {
dag.getNode('A');
dag.getNode('B');
dag.getNode('C');
dag.getNode('D');
}).not.toThrow();
expect(dag.getNode('A').inputEdges).toHaveLength(0);
expect(dag.getNode('A').outputEdges).toHaveLength(1);
expect(dag.getNode('A').outputEdges[0].outputNode).toHaveProperty('name', 'B');
expect(dag.getNode('A').outputEdges).toHaveLength(0);
expect(dag.getNode('B').inputEdges).toHaveLength(1);
expect(dag.getNode('B').outputEdges).toHaveLength(1);
expect(dag.getNode('B').inputEdges[0].inputNode).toHaveProperty('name', 'A');
expect(dag.getNode('B').inputEdges).toHaveLength(0);
expect(dag.getNode('B').outputEdges).toHaveLength(2);
expect(dag.getNode('B').outputEdges[0].outputNode).toHaveProperty('name', 'C');
expect(dag.getNode('B').outputEdges[1].outputNode).toHaveProperty('name', 'D');
expect(dag.getNode('C').inputEdges).toHaveLength(1);
expect(dag.getNode('C').outputEdges).toHaveLength(0);
expect(dag.getNode('C').inputEdges[0].inputNode).toHaveProperty('name', 'B');
expect(dag.getNode('C').outputEdges).toHaveLength(0);
expect(dag.getNode('D').inputEdges).toHaveLength(1);
expect(dag.getNode('D').inputEdges[0].inputNode).toHaveProperty('name', 'B');
expect(dag.getNode('D').outputEdges).toHaveLength(0);
});
test('data queries cannot have references', () => {
const queries = [
{
refId: 'A',
model: {
refId: 'A',
expression: 'vector(1)',
},
},
] as AlertQuery[];
expect(() => _createDagFromQueries(queries)).not.toThrow();
const dag = _createDagFromQueries(queries);
expect(Object.keys(dag.nodes)).toHaveLength(1);
expect(() => {
dag.getNode('A');
}).not.toThrow();
});
});

View File

@ -24,8 +24,11 @@ export function _createDagFromQueries(queries: AlertQuery[]): Graph {
graph.createNodes(nodes);
queries.forEach((query) => {
if (!isExpressionQuery(query.model)) {
return;
}
const source = query.refId;
const isMathExpression = isExpressionQuery(query.model) && query.model.type === 'math';
const isMathExpression = query.model.type === 'math';
// some expressions have multiple targets (like the math expression)
const targets = isMathExpression