refactor: introduce ParseGroupNodeTopology

This commit is contained in:
Matthias Radestock
2017-12-22 02:39:44 +00:00
parent b8eeadda34
commit ec589e08f6
2 changed files with 14 additions and 5 deletions

View File

@@ -337,21 +337,21 @@ func weaveNodeSummary(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, r report.Report, n report.Node) (NodeSummary, bool) {
parts := strings.Split(n.Topology, ":")
if len(parts) != 3 {
topology, key, ok := render.ParseGroupNodeTopology(n.Topology)
if !ok {
return NodeSummary{}, false
}
label, ok := n.Latest.Lookup(parts[2])
label, ok := n.Latest.Lookup(key)
if !ok {
return NodeSummary{}, false
}
base.Label, base.Rank = label, label
if t, ok := r.Topology(parts[1]); ok {
if t, ok := r.Topology(topology); ok {
base.Shape = t.GetShape()
if t.Label != "" {
base.LabelMinor = pluralize(n.Counters, parts[1], t.Label, t.LabelPlural)
base.LabelMinor = pluralize(n.Counters, topology, t.Label, t.LabelPlural)
}
}
base.Stack = true

View File

@@ -42,6 +42,15 @@ func MakeGroupNodeTopology(originalTopology, key string) string {
return strings.Join([]string{"group", originalTopology, key}, ":")
}
// ParseGroupNodeTopology returns the parts of a group topology.
func ParseGroupNodeTopology(topology string) (string, string, bool) {
parts := strings.Split(topology, ":")
if len(parts) != 3 || parts[0] != "group" {
return "", "", false
}
return parts[1], parts[2], true
}
// NewDerivedNode makes a node based on node, but with a new ID
func NewDerivedNode(id string, node report.Node) report.Node {
return report.MakeNode(id).WithChildren(node.Children.Add(node))