package engine import ( "fmt" "sort" "strings" "yetano.dev/yetano/core/solutions/catalog" ) // Graph is the resolved architecture of a state (BP-03/BP-04). Node IDs: // "pkg:", "mod:/", "cap:", "ext:" and the // solution's own node IDs ("n:"). type Graph struct { Nodes []GraphNode `json:"nodes"` Edges []GraphEdge `json:"edges"` } // GraphNode is one node with its state (required, selected, ...). type GraphNode struct { ID string `json:"id"` Type string `json:"type"` // package | module | capability | external | client | storage | network | identity | target | solution Title string `json:"title"` Status string `json:"status"` Parent string `json:"parent,omitempty"` // module -> package Ref string `json:"ref,omitempty"` // yetano: reference to open } // GraphEdge is a typed relation (requires, provides, connectsTo, ...). type GraphEdge struct { From string `json:"from"` To string `json:"to"` Type string `json:"type"` Label string `json:"label,omitempty"` Status string `json:"status,omitempty"` } func (ev *evaluation) graph() { g := Graph{Nodes: []GraphNode{}, Edges: []GraphEdge{}} have := map[string]bool{} node := func(n GraphNode) { if !have[n.ID] { have[n.ID] = true g.Nodes = append(g.Nodes, n) } } edge := func(e GraphEdge) { g.Edges = append(g.Edges, e) } solID := "sol:" + ev.s.Meta.ID node(GraphNode{ID: solID, Type: "solution", Title: ev.st.Solution.Title, Status: StSelected, Ref: "yetano:solution/" + ev.s.Meta.ID}) for _, p := range ev.st.Packages { status := p.Status switch { case !p.Selected: status = StRecommended case status == StOptional: status = StSelected // an optional package the answers chose } pid := "pkg:" + p.ID node(GraphNode{ID: pid, Type: "package", Title: p.ID, Status: status, Ref: "yetano:package/" + p.ID}) if p.Selected { edge(GraphEdge{From: solID, To: pid, Type: "requires", Status: status}) } else { edge(GraphEdge{From: solID, To: pid, Type: "optional", Label: "recommended", Status: StRecommended}) } for _, m := range p.Modules { st := StDisabled if m.Enabled { st = StSelected } if !m.Support.Selectable() { st = StUnsupported } if !m.Enabled { continue // disabled modules stay out of the picture; the package detail lists them } mid := "mod:" + p.ID + "/" + m.ID node(GraphNode{ID: mid, Type: "module", Title: firstNonEmpty(m.Program, m.ID), Status: st, Parent: pid, Ref: "yetano:module/" + p.ID + "/" + m.ID}) } } for _, c := range ev.st.Capabilities { cid := "cap:" + c.ID node(GraphNode{ID: cid, Type: "capability", Title: c.Title, Status: c.Status, Ref: "yetano:capability/" + c.ID}) switch { case strings.HasPrefix(c.Provider, "external:"): xid := "ext:" + c.ID node(GraphNode{ID: xid, Type: "external", Title: "existing " + c.Title, Status: StExternal}) edge(GraphEdge{From: xid, To: cid, Type: "provides", Status: StExternal}) case c.Provider != "": pkg, _, _ := strings.Cut(c.Provider, "/") from := "mod:" + c.Provider if !have[from] { from = "pkg:" + pkg } edge(GraphEdge{From: from, To: cid, Type: "provides", Status: c.Status}) } if c.Status == StRecommended || c.Status == StOptional || c.Status == StMissing { for _, alt := range c.Alternatives { if strings.HasPrefix(alt, "external:") { continue } pkg, _, _ := strings.Cut(alt, "/") if !have["pkg:"+pkg] { node(GraphNode{ID: "pkg:" + pkg, Type: "package", Title: pkg, Status: StAlternative, Ref: "yetano:package/" + pkg}) } edge(GraphEdge{From: "pkg:" + pkg, To: cid, Type: "alternativeTo", Status: StAlternative}) } } if c.Status == StRequired || c.Status == StMissing || c.Status == StConflicting { edge(GraphEdge{From: solID, To: cid, Type: "requires", Status: c.Status}) } } // not implemented options of visible questions: shown as unsupported alternatives for _, q := range ev.st.Questions { if !q.Visible { continue } for _, o := range q.Options { if o.Support.Selectable() { continue } for _, a := range o.Adds { if p, ok := strings.CutPrefix(a, "package:"); ok && !have["pkg:"+p] { node(GraphNode{ID: "pkg:" + p, Type: "package", Title: p, Status: StUnsupported, Ref: "yetano:package/" + p}) } } } } // solution-specific nodes and edges for _, n := range ev.s.Spec.Architecture.Nodes { if len(n.When) > 0 && !n.When.Holds(ev.env) { continue } st := StSelected if n.Type == "external" || n.Type == "client" { st = StExternal } title, err := expand(n.Title, ev.vars()) if err != nil { title = n.Title } node(GraphNode{ID: "n:" + n.ID, Type: n.Type, Title: strings.Trim(title, "'"), Status: st}) } resolve := func(id string) string { if strings.Contains(id, ":") { return id } return "n:" + id } for _, e := range ev.s.Spec.Architecture.Edges { if len(e.When) > 0 && !e.When.Holds(ev.env) { continue } from, to := resolve(e.From), resolve(e.To) if !have[from] || !have[to] || !inUse(g, from) || !inUse(g, to) { continue // an end that is not part of this resolution } edge(GraphEdge{From: from, To: to, Type: e.Type, Label: e.Label}) } // confirmed Big Picture connections between selected packages if bp := ev.e.Cat.BigPicture; bp != nil { byProduct := map[string]string{} for _, p := range ev.st.Packages { if cp := ev.e.Cat.Packages[p.ID]; cp != nil && p.Selected { byProduct[cp.Spec.BigPicture] = "pkg:" + p.ID } } for _, c := range bp.Connections { f, okf := byProduct[c.From] t, okt := byProduct[c.To] if okf && okt && c.Review == "confirmed" { edge(GraphEdge{From: f, To: t, Type: c.Type, Label: c.Label}) } } } sort.SliceStable(g.Edges, func(i, j int) bool { a, b := g.Edges[i], g.Edges[j] return fmt.Sprint(a.From, a.To, a.Type) < fmt.Sprint(b.From, b.To, b.Type) }) ev.st.Graph = g } // inUse: the node is part of the solution (not only an alternative). func inUse(g Graph, id string) bool { for _, n := range g.Nodes { if n.ID == id { switch n.Status { case StRequired, StSelected, StExternal: return true } return false } } return false } // GraphText renders a graph as indented text for the CLI (BP-06). func GraphText(g Graph) string { var b strings.Builder byID := map[string]GraphNode{} for _, n := range g.Nodes { byID[n.ID] = n } mark := func(status string) string { switch status { case StRequired, StSelected: return "+" case StRecommended, StOptional, StAlternative: return "?" case StExternal: return "~" case StMissing, StConflicting, StUnsupported: return "!" } return " " } for _, n := range g.Nodes { if n.Type != "package" { continue } fmt.Fprintf(&b, "%s %s [%s]\n", mark(n.Status), n.Title, n.Status) for _, m := range g.Nodes { if m.Parent == n.ID { fmt.Fprintf(&b, " %s %s\n", mark(m.Status), m.Title) for _, e := range g.Edges { if e.From == m.ID && e.Type == "provides" { fmt.Fprintf(&b, " provides %s\n", byID[e.To].Title) } } } } } var other []string for _, n := range g.Nodes { switch n.Type { case "external", "client", "storage", "network", "identity", "target": other = append(other, fmt.Sprintf("%s %s (%s)", mark(n.Status), n.Title, n.Type)) } } if len(other) > 0 { b.WriteString("around it:\n") for _, o := range other { fmt.Fprintf(&b, " %s\n", o) } } var rel []string for _, e := range g.Edges { f, t := byID[e.From], byID[e.To] if e.Type == "provides" || e.Type == "requires" || e.Type == "optional" || f.Type == "solution" { continue } label := e.Type if e.Label != "" { label += " (" + e.Label + ")" } rel = append(rel, fmt.Sprintf(" %s --%s--> %s", f.Title, label, t.Title)) } if len(rel) > 0 { b.WriteString("connections:\n") b.WriteString(strings.Join(rel, "\n")) b.WriteString("\n") } b.WriteString("legend: + used ? recommended/optional ~ external ! missing/unsupported\n") return b.String() } var _ = catalog.Supported