feat(gitlab-sim): 🚀 ajout graph
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
package graph
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Includes returns a Mermaid flowchart showing include file dependencies.
|
||||
// sourcePath is the path to the main pipeline file; rawIncludes is Pipeline.Include.
|
||||
func Includes(sourcePath string, rawIncludes []any) string {
|
||||
var sb strings.Builder
|
||||
w := func(s string) { sb.WriteString(s + "\n") }
|
||||
wf := func(f string, a ...any) { fmt.Fprintf(&sb, f+"\n", a...) }
|
||||
|
||||
w("---")
|
||||
w("title: Include Dependencies")
|
||||
w("---")
|
||||
w("flowchart LR")
|
||||
w(" classDef main fill:#e24329,stroke:#c73a1e,color:#fff,font-weight:bold")
|
||||
w(" classDef project fill:#6b4fbb,stroke:#5a3fa0,color:#fff")
|
||||
w(" classDef component fill:#1aaa55,stroke:#148a43,color:#fff")
|
||||
w(" classDef local fill:#428fdc,stroke:#1068bf,color:#fff")
|
||||
w(" classDef remote fill:#868686,stroke:#686868,color:#fff")
|
||||
w(" classDef template fill:#fc6d26,stroke:#e56b1f,color:#fff")
|
||||
w("")
|
||||
wf(" root[\"%s\"]:::main", mermaidLabel(sourcePath))
|
||||
|
||||
if len(rawIncludes) == 0 {
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
w("")
|
||||
|
||||
counter := 0
|
||||
for _, entry := range rawIncludes {
|
||||
for _, n := range parseIncludeEntry(entry, &counter) {
|
||||
wf(" %s[\"%s\"]:::%s", n.id, n.label, n.class)
|
||||
wf(" root --> %s", n.id)
|
||||
}
|
||||
}
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
type incNode struct {
|
||||
id, label, class string
|
||||
}
|
||||
|
||||
func parseIncludeEntry(entry any, counter *int) []incNode {
|
||||
newID := func() string {
|
||||
*counter++
|
||||
return fmt.Sprintf("inc%d", *counter)
|
||||
}
|
||||
switch v := entry.(type) {
|
||||
case string:
|
||||
return []incNode{{id: newID(), label: mermaidLabel(v), class: "local"}}
|
||||
case map[string]any:
|
||||
return parseIncludeMap(v, newID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseIncludeMap(m map[string]any, newID func() string) []incNode {
|
||||
if comp, ok := m["component"].(string); ok {
|
||||
return []incNode{{
|
||||
id: newID(),
|
||||
label: "component:<br>" + mermaidLabel(comp),
|
||||
class: "component",
|
||||
}}
|
||||
}
|
||||
if proj, ok := m["project"].(string); ok {
|
||||
ref, _ := m["ref"].(string)
|
||||
if ref == "" {
|
||||
ref = "HEAD"
|
||||
}
|
||||
files := includeFileList(m["file"])
|
||||
if len(files) == 0 {
|
||||
return []incNode{{
|
||||
id: newID(),
|
||||
label: fmt.Sprintf("project: %s @ %s", mermaidLabel(proj), ref),
|
||||
class: "project",
|
||||
}}
|
||||
}
|
||||
var nodes []incNode
|
||||
for _, f := range files {
|
||||
nodes = append(nodes, incNode{
|
||||
id: newID(),
|
||||
label: fmt.Sprintf("project: %s<br>%s @ %s", mermaidLabel(proj), mermaidLabel(f), ref),
|
||||
class: "project",
|
||||
})
|
||||
}
|
||||
return nodes
|
||||
}
|
||||
if local, ok := m["local"].(string); ok {
|
||||
return []incNode{{id: newID(), label: "local: " + mermaidLabel(local), class: "local"}}
|
||||
}
|
||||
if remote, ok := m["remote"].(string); ok {
|
||||
return []incNode{{id: newID(), label: "remote:<br>" + mermaidLabel(remote), class: "remote"}}
|
||||
}
|
||||
if tmpl, ok := m["template"].(string); ok {
|
||||
return []incNode{{id: newID(), label: "template:<br>" + mermaidLabel(tmpl), class: "template"}}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func includeFileList(v any) []string {
|
||||
switch f := v.(type) {
|
||||
case string:
|
||||
return []string{f}
|
||||
case []any:
|
||||
var out []string
|
||||
for _, item := range f {
|
||||
if s, ok := item.(string); ok {
|
||||
out = append(out, s)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// mermaidLabel escapes s for use inside a Mermaid double-quoted node label.
|
||||
func mermaidLabel(s string) string {
|
||||
s = strings.ReplaceAll(s, `"`, `'`)
|
||||
s = strings.ReplaceAll(s, `#`, `#`)
|
||||
return s
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
package graph
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"git.k3nny.fr/gitlab-sim/internal/model"
|
||||
)
|
||||
|
||||
// Pipeline returns a Mermaid flowchart of pipeline jobs grouped by stage.
|
||||
//
|
||||
// Hidden template jobs (names starting with ".") are excluded.
|
||||
// When any job has a needs: list, DAG mode is used and individual needs arrows
|
||||
// are drawn. Otherwise, classic mode draws stage-ordering arrows between
|
||||
// stage subgraphs.
|
||||
func Pipeline(p *model.Pipeline) string {
|
||||
var sb strings.Builder
|
||||
w := func(s string) { sb.WriteString(s + "\n") }
|
||||
wf := func(f string, a ...any) { fmt.Fprintf(&sb, f+"\n", a...) }
|
||||
|
||||
w("---")
|
||||
w("title: Pipeline")
|
||||
w("---")
|
||||
w("flowchart LR")
|
||||
w(" classDef regular fill:#428fdc,stroke:#1068bf,color:#fff")
|
||||
w(" classDef manual fill:#fc6d26,stroke:#e56b1f,color:#fff")
|
||||
w(" classDef trigger fill:#6b4fbb,stroke:#5a3fa0,color:#fff")
|
||||
w(" classDef delayed fill:#fca326,stroke:#d98a1e,color:#333")
|
||||
w("")
|
||||
|
||||
// Collect visible (non-template) job names; sort for stable output.
|
||||
var visible []string
|
||||
for name := range p.Jobs {
|
||||
if !strings.HasPrefix(name, ".") {
|
||||
visible = append(visible, name)
|
||||
}
|
||||
}
|
||||
sort.Strings(visible)
|
||||
|
||||
if len(visible) == 0 {
|
||||
w(" empty[\"no jobs defined\"]")
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// Assign stable numeric IDs so graph output is deterministic.
|
||||
jobID := make(map[string]string, len(visible))
|
||||
for i, name := range visible {
|
||||
jobID[name] = fmt.Sprintf("j%d", i+1)
|
||||
}
|
||||
|
||||
// Group jobs by stage; fall back to "test" (GitLab default) when unset.
|
||||
byStage := make(map[string][]string)
|
||||
for _, name := range visible {
|
||||
stage := p.Jobs[name].Stage
|
||||
if stage == "" {
|
||||
stage = "test"
|
||||
}
|
||||
byStage[stage] = append(byStage[stage], name)
|
||||
}
|
||||
|
||||
// Build ordered stage list: declared stages first, then undeclared extras.
|
||||
seen := make(map[string]bool)
|
||||
var stages []string
|
||||
for _, s := range p.Stages {
|
||||
if len(byStage[s]) > 0 && !seen[s] {
|
||||
stages = append(stages, s)
|
||||
seen[s] = true
|
||||
}
|
||||
}
|
||||
for _, name := range visible {
|
||||
stage := p.Jobs[name].Stage
|
||||
if stage == "" {
|
||||
stage = "test"
|
||||
}
|
||||
if !seen[stage] {
|
||||
stages = append(stages, stage)
|
||||
seen[stage] = true
|
||||
}
|
||||
}
|
||||
|
||||
// Emit one subgraph per stage.
|
||||
for _, stage := range stages {
|
||||
jobs := byStage[stage]
|
||||
if len(jobs) == 0 {
|
||||
continue
|
||||
}
|
||||
sort.Strings(jobs)
|
||||
wf(" subgraph %s[\"%s\"]", stageID(stage), stage)
|
||||
for _, name := range jobs {
|
||||
wf(" %s[\"%s\"]:::%s", jobID[name], mermaidLabel(name), jobClass(p.Jobs[name]))
|
||||
}
|
||||
w(" end")
|
||||
w("")
|
||||
}
|
||||
|
||||
// DAG mode: at least one visible job has a needs: list.
|
||||
dagMode := false
|
||||
for _, name := range visible {
|
||||
if len(p.Jobs[name].Needs) > 0 {
|
||||
dagMode = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if dagMode {
|
||||
w(" %% DAG: needs dependencies")
|
||||
for _, name := range visible {
|
||||
for _, need := range p.Jobs[name].Needs {
|
||||
dep := needsJobName(need)
|
||||
if dep == "" {
|
||||
continue
|
||||
}
|
||||
if depID, ok := jobID[dep]; ok {
|
||||
wf(" %s --> %s", depID, jobID[name])
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if len(stages) > 1 {
|
||||
w(" %% Classic: stage ordering")
|
||||
ids := make([]string, len(stages))
|
||||
for i, s := range stages {
|
||||
ids[i] = stageID(s)
|
||||
}
|
||||
wf(" %s", strings.Join(ids, " --> "))
|
||||
}
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func stageID(s string) string { return "stage_" + sanitizeID(s) }
|
||||
|
||||
func sanitizeID(s string) string {
|
||||
var b strings.Builder
|
||||
for _, r := range s {
|
||||
switch {
|
||||
case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9', r == '_':
|
||||
b.WriteRune(r)
|
||||
default:
|
||||
b.WriteByte('_')
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func jobClass(job model.Job) string {
|
||||
if job.Trigger != nil {
|
||||
return "trigger"
|
||||
}
|
||||
switch job.When {
|
||||
case "manual":
|
||||
return "manual"
|
||||
case "delayed":
|
||||
return "delayed"
|
||||
}
|
||||
return "regular"
|
||||
}
|
||||
|
||||
func needsJobName(need any) string {
|
||||
switch v := need.(type) {
|
||||
case string:
|
||||
return v
|
||||
case map[string]any:
|
||||
if _, cross := v["pipeline"]; cross {
|
||||
return "" // cross-pipeline needs have no local counterpart
|
||||
}
|
||||
if j, ok := v["job"].(string); ok {
|
||||
return j
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,363 @@
|
||||
package graph
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.k3nny.fr/gitlab-sim/internal/model"
|
||||
)
|
||||
|
||||
// Layout constants (pixels) – tuned to resemble GitLab's full pipeline graph view.
|
||||
const (
|
||||
chipW = 178 // job chip width
|
||||
chipH = 34 // job chip height
|
||||
chipGap = 6 // vertical gap between chips in a column
|
||||
iconCX = 14 // status circle: x offset from chip left edge to circle centre
|
||||
iconR = 7 // status circle radius (14 px diameter)
|
||||
textLeft = 27 // job name text: x offset from chip left edge
|
||||
labelH = 30 // stage-name label area height (text + bottom gap)
|
||||
topPad = 40 // outer top padding
|
||||
sidePad = 40 // outer left / right padding
|
||||
stageGap = 50 // horizontal gap between stage columns (connector space)
|
||||
botPad = 48 // outer bottom padding (legend lives here)
|
||||
)
|
||||
|
||||
type svgPt struct{ x, y int }
|
||||
|
||||
// RenderPipeline writes a PNG (or SVG fallback) file with a GitLab CI-style
|
||||
// pipeline layout and returns the path to the generated file.
|
||||
func RenderPipeline(p *model.Pipeline, outDir string) (string, error) {
|
||||
if err := os.MkdirAll(outDir, 0o755); err != nil {
|
||||
return "", fmt.Errorf("creating output directory %s: %w", outDir, err)
|
||||
}
|
||||
|
||||
svg, err := pipelineSVG(p)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
ts := time.Now().Format("20060102-150405")
|
||||
svgPath := filepath.Join(outDir, "pipeline-"+ts+".svg")
|
||||
if err := os.WriteFile(svgPath, []byte(svg), 0o644); err != nil {
|
||||
return "", fmt.Errorf("writing SVG: %w", err)
|
||||
}
|
||||
|
||||
pngPath := filepath.Join(outDir, "pipeline-"+ts+".png")
|
||||
if convertToPNG(svgPath, pngPath) {
|
||||
_ = os.Remove(svgPath)
|
||||
return pngPath, nil
|
||||
}
|
||||
return svgPath, nil
|
||||
}
|
||||
|
||||
// convertToPNG tries rsvg-convert, Inkscape, magick, and convert (not on Windows).
|
||||
func convertToPNG(svgPath, pngPath string) bool {
|
||||
type cand struct {
|
||||
args []string
|
||||
skipWin bool
|
||||
}
|
||||
for _, c := range []cand{
|
||||
{[]string{"rsvg-convert", "--output", pngPath, svgPath}, false},
|
||||
{[]string{"inkscape", "--export-filename=" + pngPath, svgPath}, false},
|
||||
{[]string{"magick", svgPath, pngPath}, false},
|
||||
{[]string{"convert", svgPath, pngPath}, true},
|
||||
} {
|
||||
if c.skipWin && runtime.GOOS == "windows" {
|
||||
continue
|
||||
}
|
||||
if bin, err := exec.LookPath(c.args[0]); err == nil {
|
||||
if exec.Command(bin, c.args[1:]...).Run() == nil {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func pipelineSVG(p *model.Pipeline) (string, error) {
|
||||
// Collect visible (non-template) job names in sorted order.
|
||||
var visible []string
|
||||
for name := range p.Jobs {
|
||||
if !strings.HasPrefix(name, ".") {
|
||||
visible = append(visible, name)
|
||||
}
|
||||
}
|
||||
sort.Strings(visible)
|
||||
|
||||
if len(visible) == 0 {
|
||||
return svgEmpty(), nil
|
||||
}
|
||||
|
||||
// Group by stage; fall back to "test" (GitLab default) when stage is unset.
|
||||
byStage := make(map[string][]string)
|
||||
for _, name := range visible {
|
||||
s := p.Jobs[name].Stage
|
||||
if s == "" {
|
||||
s = "test"
|
||||
}
|
||||
byStage[s] = append(byStage[s], name)
|
||||
}
|
||||
|
||||
// Build ordered stage list: declared stages first, then undeclared extras.
|
||||
seen := make(map[string]bool)
|
||||
var stages []string
|
||||
for _, s := range p.Stages {
|
||||
if len(byStage[s]) > 0 && !seen[s] {
|
||||
stages = append(stages, s)
|
||||
seen[s] = true
|
||||
}
|
||||
}
|
||||
for _, name := range visible {
|
||||
s := p.Jobs[name].Stage
|
||||
if s == "" {
|
||||
s = "test"
|
||||
}
|
||||
if !seen[s] {
|
||||
stages = append(stages, s)
|
||||
seen[s] = true
|
||||
}
|
||||
}
|
||||
|
||||
// Compute per-stage column heights and job anchor points for connectors.
|
||||
colH := make([]int, len(stages)) // height of the chip stack (no label)
|
||||
colCtY := make([]int, len(stages)) // y centre of the chip stack
|
||||
maxColH := 0
|
||||
rightMid := make(map[string]svgPt)
|
||||
leftMid := make(map[string]svgPt)
|
||||
|
||||
for i, stage := range stages {
|
||||
jobs := byStage[stage]
|
||||
sort.Strings(jobs)
|
||||
n := len(jobs)
|
||||
h := n*chipH + max(0, n-1)*chipGap
|
||||
colH[i] = h
|
||||
if h > maxColH {
|
||||
maxColH = h
|
||||
}
|
||||
cx := sidePad + i*(chipW+stageGap)
|
||||
for j, name := range jobs {
|
||||
chy := topPad + labelH + j*(chipH+chipGap)
|
||||
rightMid[name] = svgPt{cx + chipW, chy + chipH/2}
|
||||
leftMid[name] = svgPt{cx, chy + chipH/2}
|
||||
}
|
||||
colCtY[i] = topPad + labelH + h/2
|
||||
}
|
||||
|
||||
svgW := sidePad*2 + len(stages)*chipW + max(0, len(stages)-1)*stageGap
|
||||
svgH := topPad + labelH + maxColH + botPad
|
||||
|
||||
// DAG mode: any visible job with a needs: list triggers job-to-job arrows.
|
||||
dagMode := false
|
||||
for _, name := range visible {
|
||||
if len(p.Jobs[name].Needs) > 0 {
|
||||
dagMode = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
var sb strings.Builder
|
||||
w := func(s string) { sb.WriteString(s + "\n") }
|
||||
wf := func(f string, a ...any) { fmt.Fprintf(&sb, f+"\n", a...) }
|
||||
|
||||
// ── Document ──────────────────────────────────────────────────────────────
|
||||
wf(`<svg xmlns="http://www.w3.org/2000/svg" width="%d" height="%d" viewBox="0 0 %d %d">`,
|
||||
svgW, svgH, svgW, svgH)
|
||||
|
||||
w(` <defs>`)
|
||||
// Subtle drop shadow for job chips.
|
||||
w(` <filter id="chip-shadow" x="-4%" y="-10%" width="108%" height="130%">`)
|
||||
w(` <feDropShadow dx="0" dy="1" stdDeviation="1.5" flood-color="#000000" flood-opacity="0.07"/>`)
|
||||
w(` </filter>`)
|
||||
w(` </defs>`)
|
||||
|
||||
// White page background.
|
||||
wf(` <rect width="%d" height="%d" fill="#ffffff"/>`, svgW, svgH)
|
||||
|
||||
// ── Stage columns ─────────────────────────────────────────────────────────
|
||||
for i, stage := range stages {
|
||||
cx := sidePad + i*(chipW+stageGap)
|
||||
jobs := byStage[stage]
|
||||
sort.Strings(jobs)
|
||||
|
||||
// Stage name – small, gray, uppercase, centered above the chip stack.
|
||||
wf(` <text x="%d" y="%d" text-anchor="middle" `+
|
||||
`font-family="'GitLab Sans','Segoe UI',-apple-system,BlinkMacSystemFont,sans-serif" `+
|
||||
`font-size="11" font-weight="600" letter-spacing="0.8" fill="#868686">%s</text>`,
|
||||
cx+chipW/2, topPad+13, svgEsc(strings.ToUpper(stage)))
|
||||
|
||||
// Subtle separator line below stage name.
|
||||
wf(` <line x1="%d" y1="%d" x2="%d" y2="%d" stroke="#eaeaea" stroke-width="1"/>`,
|
||||
cx, topPad+21, cx+chipW, topPad+21)
|
||||
|
||||
// Job chips.
|
||||
for j, name := range jobs {
|
||||
job := p.Jobs[name]
|
||||
chy := topPad + labelH + j*(chipH+chipGap)
|
||||
color := chipColor(job)
|
||||
|
||||
// Chip card (white, rounded, subtle border + shadow).
|
||||
wf(` <rect x="%d" y="%d" width="%d" height="%d" rx="4" `+
|
||||
`fill="#ffffff" stroke="#dde1e7" stroke-width="1" filter="url(#chip-shadow)"/>`,
|
||||
cx, chy, chipW, chipH)
|
||||
|
||||
// Colored status indicator circle.
|
||||
wf(` <circle cx="%d" cy="%d" r="%d" fill="%s"/>`,
|
||||
cx+iconCX, chy+chipH/2, iconR, color)
|
||||
|
||||
// Icon symbol inside the circle.
|
||||
drawChipIcon(&sb, job, cx+iconCX, chy+chipH/2)
|
||||
|
||||
// Job name text.
|
||||
wf(` <text x="%d" y="%d" dominant-baseline="middle" `+
|
||||
`font-family="'GitLab Sans','Segoe UI',-apple-system,BlinkMacSystemFont,sans-serif" `+
|
||||
`font-size="13" fill="#303030">%s</text>`,
|
||||
cx+textLeft, chy+chipH/2, svgEsc(svgTrunc(name, 20)))
|
||||
}
|
||||
}
|
||||
|
||||
// ── Connectors ────────────────────────────────────────────────────────────
|
||||
const connStroke = "#dbdbdb"
|
||||
|
||||
if dagMode {
|
||||
// Job-to-job bezier curves from needs:.
|
||||
for _, name := range visible {
|
||||
for _, need := range p.Jobs[name].Needs {
|
||||
dep := needsJobName(need)
|
||||
if dep == "" {
|
||||
continue
|
||||
}
|
||||
src, okS := rightMid[dep]
|
||||
dst, okD := leftMid[name]
|
||||
if !okS || !okD {
|
||||
continue
|
||||
}
|
||||
cpX := (src.x + dst.x) / 2
|
||||
wf(` <path d="M%d,%d C%d,%d %d,%d %d,%d" stroke="%s" stroke-width="2" fill="none"/>`,
|
||||
src.x, src.y, cpX, src.y, cpX, dst.y, dst.x-7, dst.y, connStroke)
|
||||
// Small right-pointing triangle arrowhead.
|
||||
wf(` <polygon points="%d,%d %d,%d %d,%d" fill="%s"/>`,
|
||||
dst.x-7, dst.y-4, dst.x, dst.y, dst.x-7, dst.y+4, connStroke)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Classic: one connector per adjacent stage pair.
|
||||
for i := 0; i < len(stages)-1; i++ {
|
||||
x1 := sidePad + i*(chipW+stageGap) + chipW
|
||||
x2 := sidePad + (i+1)*(chipW+stageGap)
|
||||
y1 := colCtY[i]
|
||||
y2 := colCtY[i+1]
|
||||
midX := (x1 + x2) / 2
|
||||
|
||||
if y1 == y2 {
|
||||
// Straight horizontal line.
|
||||
wf(` <line x1="%d" y1="%d" x2="%d" y2="%d" stroke="%s" stroke-width="2"/>`,
|
||||
x1, y1, x2-7, y2, connStroke)
|
||||
} else {
|
||||
// L-shaped elbow: right → vertical → right.
|
||||
wf(` <polyline points="%d,%d %d,%d %d,%d %d,%d" `+
|
||||
`stroke="%s" stroke-width="2" fill="none" stroke-linejoin="round"/>`,
|
||||
x1, y1, midX, y1, midX, y2, x2-7, y2, connStroke)
|
||||
}
|
||||
// Arrowhead at the destination.
|
||||
wf(` <polygon points="%d,%d %d,%d %d,%d" fill="%s"/>`,
|
||||
x2-7, y2-4, x2, y2, x2-7, y2+4, connStroke)
|
||||
}
|
||||
}
|
||||
|
||||
// ── Legend ────────────────────────────────────────────────────────────────
|
||||
legend := []struct{ color, label string }{
|
||||
{"#1f75cb", "regular"},
|
||||
{"#fc6d26", "manual"},
|
||||
{"#6b4fbb", "trigger"},
|
||||
{"#fca326", "delayed"},
|
||||
}
|
||||
const legendItemW = 82
|
||||
legendY := topPad + labelH + maxColH + botPad/2
|
||||
lx0 := (svgW - len(legend)*legendItemW) / 2
|
||||
for k, item := range legend {
|
||||
lx := lx0 + k*legendItemW
|
||||
wf(` <circle cx="%d" cy="%d" r="6" fill="%s"/>`, lx+6, legendY, item.color)
|
||||
wf(` <text x="%d" y="%d" dominant-baseline="middle" `+
|
||||
`font-family="'GitLab Sans','Segoe UI',-apple-system,BlinkMacSystemFont,sans-serif" `+
|
||||
`font-size="11" fill="#868686">%s</text>`,
|
||||
lx+16, legendY, item.label)
|
||||
}
|
||||
|
||||
w(`</svg>`)
|
||||
return sb.String(), nil
|
||||
}
|
||||
|
||||
// drawChipIcon writes an SVG symbol inside the status circle to help identify
|
||||
// the job type at a glance.
|
||||
func drawChipIcon(sb *strings.Builder, job model.Job, cx, cy int) {
|
||||
if job.Trigger != nil {
|
||||
// Right-pointing chevron for trigger jobs.
|
||||
fmt.Fprintf(sb, " <polyline points=\"%d,%d %d,%d %d,%d\" "+
|
||||
"stroke=\"#fff\" stroke-width=\"1.5\" fill=\"none\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>\n",
|
||||
cx-3, cy-3, cx+3, cy, cx-3, cy+3)
|
||||
return
|
||||
}
|
||||
switch job.When {
|
||||
case "manual":
|
||||
// Filled play triangle.
|
||||
fmt.Fprintf(sb, " <polygon points=\"%d,%d %d,%d %d,%d\" fill=\"#fff\"/>\n",
|
||||
cx-3, cy-4, cx+5, cy, cx-3, cy+4)
|
||||
case "delayed":
|
||||
// Clock hands: vertical + horizontal.
|
||||
fmt.Fprintf(sb, " <circle cx=\"%d\" cy=\"%d\" r=\"5\" stroke=\"#fff\" stroke-width=\"1.2\" fill=\"none\"/>\n", cx, cy)
|
||||
fmt.Fprintf(sb, " <line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke=\"#fff\" stroke-width=\"1.2\" stroke-linecap=\"round\"/>\n",
|
||||
cx, cy, cx, cy-3)
|
||||
fmt.Fprintf(sb, " <line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke=\"#fff\" stroke-width=\"1.2\" stroke-linecap=\"round\"/>\n",
|
||||
cx, cy, cx+2, cy+1)
|
||||
default:
|
||||
// Regular job: small white checkmark outline.
|
||||
fmt.Fprintf(sb, " <polyline points=\"%d,%d %d,%d %d,%d\" "+
|
||||
"stroke=\"#fff\" stroke-width=\"1.5\" fill=\"none\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>\n",
|
||||
cx-3, cy, cx-1, cy+3, cx+4, cy-3)
|
||||
}
|
||||
}
|
||||
|
||||
func chipColor(job model.Job) string {
|
||||
if job.Trigger != nil {
|
||||
return "#6b4fbb"
|
||||
}
|
||||
switch job.When {
|
||||
case "manual":
|
||||
return "#fc6d26"
|
||||
case "delayed":
|
||||
return "#fca326"
|
||||
}
|
||||
return "#1f75cb"
|
||||
}
|
||||
|
||||
func svgEsc(s string) string {
|
||||
s = strings.ReplaceAll(s, "&", "&")
|
||||
s = strings.ReplaceAll(s, "<", "<")
|
||||
s = strings.ReplaceAll(s, ">", ">")
|
||||
return s
|
||||
}
|
||||
|
||||
func svgTrunc(s string, maxLen int) string {
|
||||
runes := []rune(s)
|
||||
if len(runes) <= maxLen {
|
||||
return s
|
||||
}
|
||||
return string(runes[:maxLen-1]) + "…"
|
||||
}
|
||||
|
||||
func svgEmpty() string {
|
||||
const w, h = 300, 80
|
||||
return fmt.Sprintf(
|
||||
`<svg xmlns="http://www.w3.org/2000/svg" width="%d" height="%d">`+
|
||||
`<rect width="%d" height="%d" fill="#fff"/>`+
|
||||
`<text x="%d" y="%d" text-anchor="middle" dominant-baseline="middle" `+
|
||||
`font-family="sans-serif" font-size="13" fill="#868686">no jobs defined</text>`+
|
||||
`</svg>`,
|
||||
w, h, w, h, w/2, h/2)
|
||||
}
|
||||
Reference in New Issue
Block a user