-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Azure/haitao/rich-template
Haitao/rich template
- Loading branch information
Showing
11 changed files
with
422 additions
and
104 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package graph | ||
|
||
type GraphErrorCode string | ||
|
||
const ( | ||
ErrDuplicateNode GraphErrorCode = "node with same key already exists in this graph" | ||
ErrConnectNotExistingNode GraphErrorCode = "node to connect does not exist in this graph" | ||
) | ||
|
||
func (ge GraphErrorCode) Error() string { | ||
return string(ge) | ||
} | ||
|
||
type GraphError struct { | ||
Code GraphErrorCode | ||
Message string | ||
} | ||
|
||
func (ge *GraphError) Error() string { | ||
return ge.Code.Error() + ": " + ge.Message | ||
} | ||
|
||
func (ge *GraphError) Unwrap() error { | ||
return ge.Code | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package graph_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/Azure/go-asyncjob/graph" | ||
) | ||
|
||
func TestSimpleJob(t *testing.T) { | ||
g := graph.NewGraph[*testNode](edgeSpecFromConnection) | ||
root := &testNode{Name: "root"} | ||
g.AddNode(root) | ||
calc1 := &testNode{Name: "calc1"} | ||
g.AddNode(calc1) | ||
calc2 := &testNode{Name: "calc2"} | ||
g.AddNode(calc2) | ||
summary := &testNode{Name: "summary"} | ||
g.AddNode(summary) | ||
|
||
g.Connect(root.DotSpec().ID, calc1.DotSpec().ID) | ||
g.Connect(root.DotSpec().ID, calc2.DotSpec().ID) | ||
g.Connect(calc1.DotSpec().ID, summary.DotSpec().ID) | ||
g.Connect(calc2.DotSpec().ID, summary.DotSpec().ID) | ||
|
||
graph, err := g.ToDotGraph() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
fmt.Println(graph) | ||
} | ||
|
||
type testNode struct { | ||
Name string | ||
} | ||
|
||
func (tn *testNode) DotSpec() *graph.DotNodeSpec { | ||
return &graph.DotNodeSpec{ | ||
ID: tn.Name, | ||
Name: tn.Name, | ||
Tooltip: tn.Name, | ||
Shape: "box", | ||
Style: "filled", | ||
FillColor: "green", | ||
} | ||
} | ||
|
||
func edgeSpecFromConnection(from, to *testNode) *graph.DotEdgeSpec { | ||
return &graph.DotEdgeSpec{ | ||
FromNodeID: from.DotSpec().ID, | ||
ToNodeID: to.DotSpec().ID, | ||
Tooltip: fmt.Sprintf("%s -> %s", from.DotSpec().Name, to.DotSpec().Name), | ||
Style: "solid", | ||
Color: "black", | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package asyncjob | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/Azure/go-asyncjob/graph" | ||
) | ||
|
||
type stepNode struct { | ||
StepMeta | ||
} | ||
|
||
func newStepNode(sm StepMeta) *stepNode { | ||
return &stepNode{ | ||
StepMeta: sm, | ||
} | ||
} | ||
|
||
func (sn *stepNode) DotSpec() *graph.DotNodeSpec { | ||
return &graph.DotNodeSpec{ | ||
ID: sn.getID(), | ||
Name: sn.GetName(), | ||
Shape: sn.getShape(), | ||
Style: "filled", | ||
FillColor: sn.getFillColor(), | ||
Tooltip: sn.getTooltip(), | ||
} | ||
} | ||
|
||
func (sn *stepNode) getShape() string { | ||
switch sn.getType() { | ||
case stepTypeRoot: | ||
return "triangle" | ||
case stepTypeParam: | ||
return "doublecircle" | ||
case stepTypeTask: | ||
return "box" | ||
default: | ||
return "egg" | ||
} | ||
} | ||
|
||
func (sn *stepNode) getFillColor() string { | ||
switch sn.GetState() { | ||
case StepStatePending: | ||
return "gray" | ||
case StepStateRunning: | ||
return "yellow" | ||
case StepStateCompleted: | ||
return "green" | ||
case StepStateFailed: | ||
return "red" | ||
default: | ||
return "white" | ||
} | ||
} | ||
|
||
func (sn *stepNode) getTooltip() string { | ||
state := sn.GetState() | ||
executionData := sn.ExecutionData() | ||
|
||
if state != StepStatePending && executionData != nil { | ||
return fmt.Sprintf("Type: %s\\nName: %s\\nState: %s\\nStartAt: %s\\nDuration: %s", string(sn.getType()), sn.GetName(), state, executionData.StartTime.Format(time.RFC3339Nano), executionData.Duration) | ||
} | ||
|
||
return fmt.Sprintf("Type: %s\\nName: %s", sn.getType(), sn.GetName()) | ||
} | ||
|
||
func stepConn(snFrom, snTo *stepNode) *graph.DotEdgeSpec { | ||
edgeSpec := &graph.DotEdgeSpec{ | ||
FromNodeID: snFrom.getID(), | ||
ToNodeID: snTo.getID(), | ||
Color: "black", | ||
Style: "bold", | ||
} | ||
|
||
// update edge color, tooltip if NodeTo is started already. | ||
if snTo.GetState() != StepStatePending { | ||
executionData := snTo.ExecutionData() | ||
edgeSpec.Tooltip = fmt.Sprintf("Time: %s", executionData.StartTime.Format(time.RFC3339Nano)) | ||
} | ||
|
||
fromNodeState := snFrom.GetState() | ||
if fromNodeState == StepStateCompleted { | ||
edgeSpec.Color = "green" | ||
} else if fromNodeState == StepStateFailed { | ||
edgeSpec.Color = "red" | ||
} | ||
|
||
return edgeSpec | ||
} |
Oops, something went wrong.