From 4ad1ae80dfe2b5e0c4ab813b518ceb53ee22ade0 Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Wed, 20 Apr 2016 10:29:42 +0100 Subject: [PATCH] move shapes determination to the topology --- render/detailed/summary.go | 47 ++++++++++++-------------------------- report/report.go | 19 ++++++++++----- report/topology.go | 24 +++++++++++++++++++ 3 files changed, 52 insertions(+), 38 deletions(-) diff --git a/render/detailed/summary.go b/render/detailed/summary.go index 1023f322a..0892d014f 100644 --- a/render/detailed/summary.go +++ b/render/detailed/summary.go @@ -15,12 +15,6 @@ import ( // Shapes that are allowed const ( - Circle = "circle" - Square = "square" - Heptagon = "heptagon" - Hexagon = "hexagon" - Cloud = "cloud" - ImageNameNone = "" // Keys we use to render container names @@ -29,18 +23,6 @@ const ( MarathonAppIDEnv = "MARATHON_APP_ID" ) -var ( - shapesByTopology = map[string]string{ - report.Host: Circle, - report.Process: Square, - report.Pod: Heptagon, - report.Service: Heptagon, - report.Container: Hexagon, - report.ContainerImage: Hexagon, - render.Pseudo: Circle, - } -) - // NodeSummaryGroup is a topology-typed group of children for a Node. type NodeSummaryGroup struct { ID string `json:"id"` @@ -102,7 +84,7 @@ func MakeNodeSummary(r report.Report, n report.Node) (NodeSummary, bool) { return renderer(baseNodeSummary(r, n), n) } if strings.HasPrefix(n.Topology, "group:") { - return groupNodeSummary(baseNodeSummary(r, n), n) + return groupNodeSummary(baseNodeSummary(r, n), r, n) } return NodeSummary{}, false } @@ -142,13 +124,9 @@ func (n NodeSummary) Copy() NodeSummary { } func baseNodeSummary(r report.Report, n report.Node) NodeSummary { - shape, ok := shapesByTopology[n.Topology] - if !ok { - shape = Circle - } return NodeSummary{ ID: n.ID, - Shape: shape, + Shape: topologyShape(r, n.Topology), Linkable: true, Metadata: NodeMetadata(r, n), Metrics: NodeMetrics(r, n), @@ -168,7 +146,7 @@ func pseudoNodeSummary(base NodeSummary, n report.Node) (NodeSummary, bool) { }[n.ID]; ok { base.Label = template.Label base.LabelMinor = template.LabelMinor - base.Shape = Cloud + base.Shape = report.Cloud return base, true } @@ -176,7 +154,7 @@ func pseudoNodeSummary(base NodeSummary, n report.Node) (NodeSummary, bool) { if strings.HasPrefix(n.ID, render.MakePseudoNodeID(render.UncontainedID)) { base.Label = render.UncontainedMajor base.LabelMinor = report.ExtractHostID(n) - base.Shape = Square + base.Shape = report.Square base.Stack = true return base, true } @@ -184,7 +162,7 @@ func pseudoNodeSummary(base NodeSummary, n report.Node) (NodeSummary, bool) { // try rendering it as an unmanaged node if strings.HasPrefix(n.ID, render.MakePseudoNodeID(render.UnmanagedID)) { base.Label = render.UnmanagedMajor - base.Shape = Square + base.Shape = report.Square base.Stack = true base.LabelMinor = report.ExtractHostID(n) return base, true @@ -193,7 +171,7 @@ func pseudoNodeSummary(base NodeSummary, n report.Node) (NodeSummary, bool) { // try rendering it as an endpoint if addr, ok := n.Latest.Lookup(endpoint.Addr); ok { base.Label = addr - base.Shape = Circle + base.Shape = report.Circle return base, true } @@ -308,7 +286,7 @@ func hostNodeSummary(base NodeSummary, n report.Node) (NodeSummary, bool) { // groupNodeSummary renders the summary for a group node. n.Topology is // expected to be of the form: group:container:hostname -func groupNodeSummary(base NodeSummary, n report.Node) (NodeSummary, bool) { +func groupNodeSummary(base NodeSummary, r report.Report, n report.Node) (NodeSummary, bool) { parts := strings.Split(n.Topology, ":") if len(parts) != 3 { return NodeSummary{}, false @@ -331,9 +309,7 @@ func groupNodeSummary(base NodeSummary, n report.Node) (NodeSummary, bool) { } } - if base.Shape, ok = shapesByTopology[parts[1]]; !ok { - base.Shape = Circle - } + base.Shape = topologyShape(r, parts[1]) base.Stack = true return base, true } @@ -397,3 +373,10 @@ func getRenderableContainerName(nmd report.Node) string { } return "" } + +func topologyShape(r report.Report, topology string) string { + if t, ok := r.Topology(topology); ok && t.Shape != "" { + return t.Shape + } + return report.Circle +} diff --git a/report/report.go b/report/report.go index d2f8b47bb..3475efa0f 100644 --- a/report/report.go +++ b/report/report.go @@ -20,6 +20,13 @@ const ( Host = "host" Overlay = "overlay" + // Shapes used for different nodes + Circle = "circle" + Square = "square" + Heptagon = "heptagon" + Hexagon = "hexagon" + Cloud = "cloud" + // Used when counting the number of containers ContainersKey = "containers" ) @@ -97,12 +104,12 @@ type Report struct { func MakeReport() Report { return Report{ Endpoint: MakeTopology(), - Process: MakeTopology(), - Container: MakeTopology(), - ContainerImage: MakeTopology(), - Host: MakeTopology(), - Pod: MakeTopology(), - Service: MakeTopology(), + Process: MakeTopology().WithShape(Square), + Container: MakeTopology().WithShape(Hexagon), + ContainerImage: MakeTopology().WithShape(Hexagon), + Host: MakeTopology().WithShape(Circle), + Pod: MakeTopology().WithShape(Heptagon), + Service: MakeTopology().WithShape(Heptagon), Overlay: MakeTopology(), Sampling: Sampling{}, Window: 0, diff --git a/report/topology.go b/report/topology.go index da783e3b7..f648d9351 100644 --- a/report/topology.go +++ b/report/topology.go @@ -10,6 +10,7 @@ import ( // EdgeMetadatas and Nodes respectively. Edges are directional, and embedded // in the Node struct. type Topology struct { + Shape string `json:"shape,omitempty"` Nodes `json:"nodes"` Controls `json:"controls,omitempty"` MetadataTemplates `json:"metadata_templates,omitempty"` @@ -20,6 +21,7 @@ type Topology struct { // MakeTopology gives you a Topology. func MakeTopology() Topology { return Topology{ + Shape: Circle, Nodes: map[string]Node{}, Controls: Controls{}, } @@ -29,6 +31,7 @@ func MakeTopology() Topology { // returning a new topology. func (t Topology) WithMetadataTemplates(other MetadataTemplates) Topology { return Topology{ + Shape: t.Shape, Nodes: t.Nodes.Copy(), Controls: t.Controls.Copy(), MetadataTemplates: t.MetadataTemplates.Merge(other), @@ -41,6 +44,7 @@ func (t Topology) WithMetadataTemplates(other MetadataTemplates) Topology { // returning a new topology. func (t Topology) WithMetricTemplates(other MetricTemplates) Topology { return Topology{ + Shape: t.Shape, Nodes: t.Nodes.Copy(), Controls: t.Controls.Copy(), MetadataTemplates: t.MetadataTemplates.Copy(), @@ -53,6 +57,7 @@ func (t Topology) WithMetricTemplates(other MetricTemplates) Topology { // returning a new topology. func (t Topology) WithTableTemplates(other TableTemplates) Topology { return Topology{ + Shape: t.Shape, Nodes: t.Nodes.Copy(), Controls: t.Controls.Copy(), MetadataTemplates: t.MetadataTemplates.Copy(), @@ -61,6 +66,18 @@ func (t Topology) WithTableTemplates(other TableTemplates) Topology { } } +// WithShape sets the shape nodes of this topology, returning a new topology. +func (t Topology) WithShape(shape string) Topology { + return Topology{ + Shape: shape, + Nodes: t.Nodes.Copy(), + Controls: t.Controls.Copy(), + MetadataTemplates: t.MetadataTemplates.Copy(), + MetricTemplates: t.MetricTemplates.Copy(), + TableTemplates: t.TableTemplates.Copy(), + } +} + // AddNode adds node to the topology under key nodeID; if a // node already exists for this key, nmd is merged with that node. // The same topology is returned to enable chaining. @@ -77,6 +94,7 @@ func (t Topology) AddNode(node Node) Topology { // Copy returns a value copy of the Topology. func (t Topology) Copy() Topology { return Topology{ + Shape: t.Shape, Nodes: t.Nodes.Copy(), Controls: t.Controls.Copy(), MetadataTemplates: t.MetadataTemplates.Copy(), @@ -88,7 +106,13 @@ func (t Topology) Copy() Topology { // Merge merges the other object into this one, and returns the result object. // The original is not modified. func (t Topology) Merge(other Topology) Topology { + shape := t.Shape + // A circle is the lowliest shape. + if shape == Circle { + shape = other.Shape + } return Topology{ + Shape: t.Shape, Nodes: t.Nodes.Merge(other.Nodes), Controls: t.Controls.Merge(other.Controls), MetadataTemplates: t.MetadataTemplates.Merge(other.MetadataTemplates),