Files
pipelines/internal/tui/graph_layout.go
T

253 lines
7.4 KiB
Go

package tui
import (
"fmt"
"git.a-wan.net/alan/pipelines/internal/protocol"
"sort"
)
// Forward forks use separate branch lanes. Unbranched graphs keep a horizontal
// row; jumps and feedback routes use dedicated upper arcs.
// Terminals share a lower row, with
// arrows entering their top borders. Every edge has independent ports/tracks.
func layoutGraph(graph protocol.Graph) graphLayout {
if columns, order, children, parents, ok := forkColumns(graph); ok {
return layoutForkGraph(graph, columns, order, children, parents)
}
return layoutSingleRowGraph(graph)
}
func layoutSingleRowGraph(graph protocol.Graph) graphLayout {
r := graphLayout{edges: graphEdges(graph), positions: map[string]int{}}
count := len(graph.Stages)
index, incoming, outgoing := map[string]int{}, map[string][]int{}, map[string][]int{}
widths := map[string]int{}
for i, s := range graph.Stages {
index[s.Key] = i
widths[s.Key] = max(14, len(s.Key)+4)
}
gapCount := make([]int, count+1)
arcs := 0
for i, e := range r.edges {
incoming[e.target] = append(incoming[e.target], i)
outgoing[e.source] = append(outgoing[e.source], i)
r.edges[i].bottomExit = e.outcome == "error" && e.target == protocol.FailureEnd
if !r.edges[i].bottomExit {
gapCount[index[e.source]+1]++
}
widths[e.source] = max(widths[e.source], len(fmt.Sprintf("%s [%d]", e.label, e.id))+4)
if !protocol.IsTerminal(e.target) {
widths[e.target] = max(widths[e.target], len(fmt.Sprintf("[%d]", e.id))+4)
}
if !protocol.IsTerminal(e.target) && index[e.target] != index[e.source]+1 {
arcs++
gapCount[index[e.target]]++
}
}
gapStarts := make([]int, count+1)
x, mainY, height := 0, arcs, 4
for i, s := range graph.Stages {
inputs, outputs, bottom := 0, 0, 0
for _, id := range incoming[s.Key] {
inputs = max(inputs, len(fmt.Sprintf("[%d]", r.edges[id].id)))
}
sideOutputs := 0
for _, id := range outgoing[s.Key] {
e := r.edges[id]
if e.bottomExit {
bottom = 1
if len(incoming[s.Key]) > 0 {
outputs = max(outputs, len(fmt.Sprintf("%s [%d]", e.label, e.id)))
}
continue
}
sideOutputs++
outputs = max(outputs, len(fmt.Sprintf("%s [%d]", e.label, e.id)))
}
if inputs > 0 && outputs > 0 {
widths[s.Key] = max(widths[s.Key], inputs+outputs+5)
}
gapStarts[i] = x
x += gapCount[i] + 3
h := max(len(incoming[s.Key]), sideOutputs+bottom) + 3
height = max(height, h)
r.nodes = append(r.nodes, graphNode{key: s.Key, x: x, y: mainY, width: widths[s.Key], height: h})
r.positions[s.Key] = mainY
x += widths[s.Key]
}
gapStarts[count] = x
x += gapCount[count] + 3
r.width = x
nodes := map[string]graphNode{}
for _, n := range r.nodes {
nodes[n.key] = n
}
// Incoming IDs and outgoing labels occupy opposite sides of shared rows.
// Upper arcs precede adjacent routes; failure exits get their own bottom row.
type port struct {
edge int
input bool
rank int
}
for _, stage := range graph.Stages {
var ports []port
for _, id := range incoming[stage.Key] {
e := r.edges[id]
rank := 0
if index[e.source]+1 == index[e.target] {
rank = 2
}
ports = append(ports, port{id, true, rank})
}
for _, id := range outgoing[stage.Key] {
e := r.edges[id]
rank := 3
if e.bottomExit {
continue
}
if protocol.IsTerminal(e.target) {
rank = 5
} else if index[e.target] == index[e.source]+1 {
rank = 4
}
ports = append(ports, port{id, false, rank})
}
sort.SliceStable(ports, func(i, j int) bool { return ports[i].rank < ports[j].rank })
inputRow, outputRow := 0, 0
for _, p := range ports {
if p.input {
r.edges[p.edge].to = mainY + 2 + inputRow
inputRow++
} else {
r.edges[p.edge].from = mainY + 2 + outputRow
outputRow++
}
}
for _, id := range outgoing[stage.Key] {
if r.edges[id].bottomExit {
r.edges[id].from = mainY + nodes[stage.Key].height - 2
}
}
}
usedOut, usedIn := make([]int, count+1), make([]int, count+1)
sourceTrack := func(gap int) int { value := gapStarts[gap] + 1 + usedOut[gap]; usedOut[gap]++; return value }
targetTrack := func(gap int) int {
outCount := 0
if gap > 0 {
for _, id := range outgoing[graph.Stages[gap-1].Key] {
if !r.edges[id].bottomExit {
outCount++
}
}
}
value := gapStarts[gap] + 1 + outCount + usedIn[gap]
usedIn[gap]++
return value
}
arcY := 0
for i := range r.edges {
e := &r.edges[i]
if e.bottomExit {
e.x = nodes[e.source].x + nodes[e.source].width/2
} else {
e.x = sourceTrack(index[e.source] + 1)
}
if protocol.IsTerminal(e.target) {
e.topEntry = true
continue
}
source, target := nodes[e.source], nodes[e.target]
e.path = []graphPoint{{source.x + source.width - 1, e.from}, {e.x, e.from}}
if index[e.target] == index[e.source]+1 {
e.path = append(e.path, graphPoint{e.x, e.to}, graphPoint{target.x, e.to})
continue
}
trackX := targetTrack(index[e.target])
e.path = append(e.path, graphPoint{e.x, arcY}, graphPoint{trackX, arcY}, graphPoint{trackX, e.to}, graphPoint{target.x, e.to})
e.lane = arcY
arcY++
}
stride := len(fmt.Sprintf("[%d]", len(r.edges))) + 1
successWidth := max(14, len(incoming[protocol.SuccessEnd])*(stride+2)+4)
failureWidth := max(14, len(incoming[protocol.FailureEnd])*(stride+2)+4)
endWidth := successWidth + failureWidth + 4
r.width = max(r.width, endWidth)
endX := (r.width - endWidth) / 2
terminalX := map[string]int{protocol.FailureEnd: endX, protocol.SuccessEnd: endX + failureWidth + 4}
// Pair incoming ports with source order to reduce crossings on the way down.
for _, key := range []string{protocol.SuccessEnd, protocol.FailureEnd} {
sort.SliceStable(incoming[key], func(i, j int) bool { return r.edges[incoming[key][i]].x < r.edges[incoming[key][j]].x })
}
reserved := map[int]bool{}
for _, edge := range r.edges {
if edge.topEntry {
reserved[edge.x] = true
}
}
portX := map[int]int{}
for _, key := range []string{protocol.SuccessEnd, protocol.FailureEnd} {
candidate := terminalX[key] + 2
for _, id := range incoming[key] {
for reserved[candidate] {
candidate++
}
portX[id] = candidate
candidate += stride
}
}
type interval struct{ start, end int }
var lanes [][]interval
for i := range r.edges {
e := &r.edges[i]
if !e.topEntry {
continue
}
destinationX := portX[i]
span := interval{min(e.x, destinationX), max(e.x, destinationX)}
lane := 0
for ; lane < len(lanes); lane++ {
free := true
for _, other := range lanes[lane] {
if span.start <= other.end && other.start <= span.end {
free = false
break
}
}
if free {
break
}
}
if lane == len(lanes) {
lanes = append(lanes, nil)
}
lanes[lane] = append(lanes[lane], span)
e.lane = lane
row := mainY + height + lane
source := nodes[e.source]
e.path = []graphPoint{{source.x + source.width - 1, e.from}, {e.x, e.from}, {e.x, row}, {destinationX, row}}
if e.bottomExit {
e.path = []graphPoint{{e.x, source.y + source.height - 1}, {e.x, row}, {destinationX, row}}
}
}
endY := mainY + height + len(lanes) + 1
for _, key := range []string{protocol.FailureEnd, protocol.SuccessEnd} {
width := successWidth
if key == protocol.FailureEnd {
width = failureWidth
}
r.nodes = append(r.nodes, graphNode{key: key, x: terminalX[key], y: endY, width: width, height: 3})
r.positions[key] = endY
}
for i := range r.edges {
e := &r.edges[i]
if !e.topEntry {
continue
}
e.to = endY
last := e.path[len(e.path)-1]
e.path = append(e.path, graphPoint{last.x, endY})
}
r.height = endY + 3
return r
}