-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement AntlrRunSelectionVisitor on frontend (#26243)
## Summary & Motivation We now want to implement the visitor methods for `RunSelectionVisitor`. This requires having access to the metadata for each run to get the status. This is not available on type `GraphQueryItem`, so a new type `RunGraphQueryItem` was added to include the metadata. ## How I Tested These Changes `AntlrRunSelection.test.ts`
- Loading branch information
Showing
8 changed files
with
421 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
js_modules/dagster-ui/packages/ui-core/src/run-selection/AntlrRunSelection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import {CharStreams, CommonTokenStream} from 'antlr4ts'; | ||
|
||
import {AntlrRunSelectionVisitor} from './AntlrRunSelectionVisitor'; | ||
import {AntlrInputErrorListener} from '../asset-selection/AntlrAssetSelection'; | ||
import {RunGraphQueryItem} from '../gantt/toGraphQueryItems'; | ||
import {RunSelectionLexer} from './generated/RunSelectionLexer'; | ||
import {RunSelectionParser} from './generated/RunSelectionParser'; | ||
|
||
type RunSelectionQueryResult = { | ||
all: RunGraphQueryItem[]; | ||
focus: RunGraphQueryItem[]; | ||
}; | ||
|
||
export const parseRunSelectionQuery = ( | ||
all_runs: RunGraphQueryItem[], | ||
query: string, | ||
): RunSelectionQueryResult | Error => { | ||
try { | ||
const lexer = new RunSelectionLexer(CharStreams.fromString(query)); | ||
lexer.removeErrorListeners(); | ||
lexer.addErrorListener(new AntlrInputErrorListener()); | ||
|
||
const tokenStream = new CommonTokenStream(lexer); | ||
|
||
const parser = new RunSelectionParser(tokenStream); | ||
parser.removeErrorListeners(); | ||
parser.addErrorListener(new AntlrInputErrorListener()); | ||
|
||
const tree = parser.start(); | ||
|
||
const visitor = new AntlrRunSelectionVisitor(all_runs); | ||
const all_selection = visitor.visit(tree); | ||
const focus_selection = visitor.focus_runs; | ||
|
||
return { | ||
all: Array.from(all_selection), | ||
focus: Array.from(focus_selection), | ||
}; | ||
} catch (e) { | ||
return e as Error; | ||
} | ||
}; |
165 changes: 165 additions & 0 deletions
165
js_modules/dagster-ui/packages/ui-core/src/run-selection/AntlrRunSelectionVisitor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import {AbstractParseTreeVisitor} from 'antlr4ts/tree/AbstractParseTreeVisitor'; | ||
|
||
import {GraphTraverser} from '../app/GraphQueryImpl'; | ||
import {RunGraphQueryItem} from '../gantt/toGraphQueryItems'; | ||
import { | ||
AllExpressionContext, | ||
AndExpressionContext, | ||
AttributeExpressionContext, | ||
DownTraversalExpressionContext, | ||
FunctionCallExpressionContext, | ||
NameExprContext, | ||
NameSubstringExprContext, | ||
NotExpressionContext, | ||
OrExpressionContext, | ||
ParenthesizedExpressionContext, | ||
StartContext, | ||
StatusAttributeExprContext, | ||
TraversalAllowedExpressionContext, | ||
UpAndDownTraversalExpressionContext, | ||
UpTraversalExpressionContext, | ||
} from './generated/RunSelectionParser'; | ||
import {RunSelectionVisitor} from './generated/RunSelectionVisitor'; | ||
import { | ||
getFunctionName, | ||
getTraversalDepth, | ||
getValue, | ||
} from '../asset-selection/AntlrAssetSelectionVisitor'; | ||
|
||
export class AntlrRunSelectionVisitor | ||
extends AbstractParseTreeVisitor<Set<RunGraphQueryItem>> | ||
implements RunSelectionVisitor<Set<RunGraphQueryItem>> | ||
{ | ||
all_runs: Set<RunGraphQueryItem>; | ||
focus_runs: Set<RunGraphQueryItem>; | ||
traverser: GraphTraverser<RunGraphQueryItem>; | ||
|
||
protected defaultResult() { | ||
return new Set<RunGraphQueryItem>(); | ||
} | ||
|
||
constructor(all_runs: RunGraphQueryItem[]) { | ||
super(); | ||
this.all_runs = new Set(all_runs); | ||
this.focus_runs = new Set(); | ||
this.traverser = new GraphTraverser(all_runs); | ||
} | ||
|
||
visitStart(ctx: StartContext) { | ||
return this.visit(ctx.expr()); | ||
} | ||
|
||
visitTraversalAllowedExpression(ctx: TraversalAllowedExpressionContext) { | ||
return this.visit(ctx.traversalAllowedExpr()); | ||
} | ||
|
||
visitUpAndDownTraversalExpression(ctx: UpAndDownTraversalExpressionContext) { | ||
const selection = this.visit(ctx.traversalAllowedExpr()); | ||
const up_depth: number = getTraversalDepth(ctx.traversal(0)); | ||
const down_depth: number = getTraversalDepth(ctx.traversal(1)); | ||
const selection_copy = new Set(selection); | ||
for (const item of selection_copy) { | ||
this.traverser.fetchUpstream(item, up_depth).forEach((i) => selection.add(i)); | ||
this.traverser.fetchDownstream(item, down_depth).forEach((i) => selection.add(i)); | ||
} | ||
return selection; | ||
} | ||
|
||
visitUpTraversalExpression(ctx: UpTraversalExpressionContext) { | ||
const selection = this.visit(ctx.traversalAllowedExpr()); | ||
const traversal_depth: number = getTraversalDepth(ctx.traversal()); | ||
const selection_copy = new Set(selection); | ||
for (const item of selection_copy) { | ||
this.traverser.fetchUpstream(item, traversal_depth).forEach((i) => selection.add(i)); | ||
} | ||
return selection; | ||
} | ||
|
||
visitDownTraversalExpression(ctx: DownTraversalExpressionContext) { | ||
const selection = this.visit(ctx.traversalAllowedExpr()); | ||
const traversal_depth: number = getTraversalDepth(ctx.traversal()); | ||
const selection_copy = new Set(selection); | ||
for (const item of selection_copy) { | ||
this.traverser.fetchDownstream(item, traversal_depth).forEach((i) => selection.add(i)); | ||
} | ||
return selection; | ||
} | ||
|
||
visitNotExpression(ctx: NotExpressionContext) { | ||
const selection = this.visit(ctx.expr()); | ||
return new Set([...this.all_runs].filter((i) => !selection.has(i))); | ||
} | ||
|
||
visitAndExpression(ctx: AndExpressionContext) { | ||
const left = this.visit(ctx.expr(0)); | ||
const right = this.visit(ctx.expr(1)); | ||
return new Set([...left].filter((i) => right.has(i))); | ||
} | ||
|
||
visitOrExpression(ctx: OrExpressionContext) { | ||
const left = this.visit(ctx.expr(0)); | ||
const right = this.visit(ctx.expr(1)); | ||
return new Set([...left, ...right]); | ||
} | ||
|
||
visitAllExpression(_ctx: AllExpressionContext) { | ||
return this.all_runs; | ||
} | ||
|
||
visitAttributeExpression(ctx: AttributeExpressionContext) { | ||
return this.visit(ctx.attributeExpr()); | ||
} | ||
|
||
visitFunctionCallExpression(ctx: FunctionCallExpressionContext) { | ||
const function_name: string = getFunctionName(ctx.functionName()); | ||
const selection = this.visit(ctx.expr()); | ||
if (function_name === 'sinks') { | ||
const sinks = new Set<RunGraphQueryItem>(); | ||
for (const item of selection) { | ||
const downstream = this.traverser | ||
.fetchDownstream(item, Number.MAX_VALUE) | ||
.filter((i) => selection.has(i)); | ||
if (downstream.length === 0 || (downstream.length === 1 && downstream[0] === item)) { | ||
sinks.add(item); | ||
} | ||
} | ||
return sinks; | ||
} | ||
if (function_name === 'roots') { | ||
const roots = new Set<RunGraphQueryItem>(); | ||
for (const item of selection) { | ||
const upstream = this.traverser | ||
.fetchUpstream(item, Number.MAX_VALUE) | ||
.filter((i) => selection.has(i)); | ||
if (upstream.length === 0 || (upstream.length === 1 && upstream[0] === item)) { | ||
roots.add(item); | ||
} | ||
} | ||
return roots; | ||
} | ||
throw new Error(`Unknown function: ${function_name}`); | ||
} | ||
|
||
visitParenthesizedExpression(ctx: ParenthesizedExpressionContext) { | ||
return this.visit(ctx.expr()); | ||
} | ||
|
||
visitNameExpr(ctx: NameExprContext) { | ||
const value: string = getValue(ctx.value()); | ||
const selection = [...this.all_runs].filter((i) => i.name === value); | ||
selection.forEach((i) => this.focus_runs.add(i)); | ||
return new Set(selection); | ||
} | ||
|
||
visitNameSubstringExpr(ctx: NameSubstringExprContext) { | ||
const value: string = getValue(ctx.value()); | ||
const selection = [...this.all_runs].filter((i) => i.name.includes(value)); | ||
selection.forEach((i) => this.focus_runs.add(i)); | ||
return new Set(selection); | ||
} | ||
|
||
visitStatusAttributeExpr(ctx: StatusAttributeExprContext) { | ||
const state: string = getValue(ctx.value()).toLowerCase(); | ||
return new Set([...this.all_runs].filter((i) => i.metadata?.state === state)); | ||
} | ||
} |
Oops, something went wrong.
40796bd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deploy preview for dagit-core-storybook ready!
✅ Preview
https://dagit-core-storybook-mhn3f0ugo-elementl.vercel.app
Built with commit 40796bd.
This pull request is being automatically deployed with vercel-action