Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance graph to indicate workflow steps and show active state #656

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Support Azure AD authentication in Explorer (see nflow-explorer/src/config.js for configuration options)
- Improve REST API error response handling
- Sort state variables by name
- indicate workflow states used in graph and show active state in graph
- Use ReactJS 18.x, material UI 5.x
- Replace Create React App -build by Vite
- `nflow-engine`
Expand Down
45 changes: 38 additions & 7 deletions nflow-explorer/src/component/StateGraph.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,53 @@ import './StateGraph.scss';
// - zoom and pan state graph

// TODO move to service.ts?
function createGraph(definition) {
function createGraph(props) {
const g = new dagreD3.graphlib.Graph().setGraph({});
const states = definition.states;
const states = props.definition.states;
// create nodes
for (let state of states) {
g.setNode(state.id, {label: state.id, class: `node-${state.type}`});
// Round the corners of the nodes
const node = g.node(state.id);
node.rx = node.ry = 5;

//if instance is provided, highlight with the .active
//
if (props.instance && props.instance.state === state.id) {
node.class += ' current-state';

}

}

function hasNavigatedState(fromState, toState, instance) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more performant solution might traverse actions only once, and create a set of realized state transition pairs.

Then hasNavigatedState() would be just a lookup to this set, instead of iterating the actions for each transition in workflow definition.

But I can approve this solution as well, because loading wordGenerator (from nflow-tests) instance to the modified Explorer does not fail:
https://nflow.io/dev/index.html#/workflow/3

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats a huge state diagram :) ive changed it to blue as below

image

i did think a bit on the performance side but its browser based and you end up with
(number of workflow states) X(displayed actions (defaults to 100 based on system setting)

overall not too many to worry about (i think)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the performance is sufficient, and there are more important things to polish in Explorer. For instance, we need to migrate from dagre-d3 to something that is being maintained.

Thanks for changing the edge color to blue. I think I'd also slightly prefer blue in the current state highlight, though that's not important for me. I'll merge the PR later today 👍

//reverse iterate through the actions,
//when we have two actions in sequence that match the from state and the to state we return true
const actions = instance.actions;

for (let i = 0; i < actions.length - 1; i++) {
// Check if the current action state matches the toState (since it's reversed)
if (actions[i].state === toState && actions[i + 1].state === fromState) {
return true;
}
}
if(actions[0].state == fromState && toState == instance.state){
//this is the final state where there is no action showing
return true;
}

return false;
}

// create edges between nodes
for (let state of states) {
for (let transition of state.transitions || []) {
var hasNavigated = false;
if (props.instance) {
hasNavigated = hasNavigatedState(state.id, transition, props.instance);
}
g.setEdge(state.id, transition, {
class: 'edge-normal',
class: 'edge-normal' + (hasNavigated ? ' active' : ''),
curve: d3.curveBasis,
arrowhead: 'normal'
});
Expand All @@ -46,8 +77,8 @@ function createGraph(definition) {
curve: d3.curveBasis,
arrowhead: 'normal'
});
} else if (definition.onError) {
g.setEdge(state.id, definition.onError, {
} else if (props.definition.onError) {
g.setEdge(state.id, props.definition.onError, {
class: 'edge-error',
curve: d3.curveBasis,
arrowhead: 'normal'
Expand Down Expand Up @@ -84,8 +115,8 @@ function render(g, selector) {

function StateGraph(props) {
useEffect(() => {
console.info('StateGraph', props.definition);
const g = createGraph(props.definition);
console.info('StateGraph', props);
const g = createGraph(props);
render(g, 'svg#stategraph');
return () => {
// Remove svg element which is created in render(), since it is not managed by React
Expand Down
3 changes: 3 additions & 0 deletions nflow-explorer/src/component/StateGraph.scss
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
.edge-error.active,
.edge-failure.active .edge-unexpected {
stroke-width: 2px;
stroke: red;
}

.edge-normal.selected,
Expand Down Expand Up @@ -166,6 +167,8 @@
/* */
g.current-state > g > g > text {
text-decoration: underline;
fill: red;
font-weight: bold;
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ const InstanceSummary = ({
</Grid>
</Grid>
<Grid item xs={6} lg={6}>
<StateGraph definition={definition} />
<StateGraph definition={definition} instance={instance} />
</Grid>
</Grid>
</Grid>
Expand Down
Loading