From 3ebd215b6a8329f1d240a4c5366d5d310cd59500 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Mon, 20 Nov 2017 10:05:50 +0000 Subject: [PATCH] Split addToResults into multiple functions --- render/container.go | 4 ++-- render/host.go | 8 ++++---- render/process.go | 8 ++++---- render/render.go | 36 +++++++++++++++++++++++++++++------- 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/render/container.go b/render/container.go index daee2262d..63a95a363 100644 --- a/render/container.go +++ b/render/container.go @@ -82,7 +82,7 @@ func (c connectionJoin) Render(rpt report.Report) Nodes { // Nodes without a hostid may be pseudo nodes - if so, pass through to result if _, ok := m.Latest.Lookup(report.HostNodeID); !ok { if id, ok := externalNodeID(m, addr, local); ok { - ret.addToResults(m, id, true, newPseudoNode) + ret.addEndpointChild(m, id, newPseudoNode) continue } } @@ -94,7 +94,7 @@ func (c connectionJoin) Render(rpt report.Report) Nodes { id, found = ipNodes[report.MakeScopedEndpointNodeID(scope, addr, port)] } if found && id != "" { // not one we blanked out earlier - ret.addToResults(m, id, true, func(id string) report.Node { + ret.addEndpointChild(m, id, func(id string) report.Node { return inputNodes.Nodes[id] }) } diff --git a/render/host.go b/render/host.go index ec50dc03e..9accf21e6 100644 --- a/render/host.go +++ b/render/host.go @@ -35,9 +35,9 @@ func nodes2Hosts(nodes Nodes) Nodes { } else { hostIDs, _ := n.Parents.Lookup(report.Host) for _, id := range hostIDs { - ret.addToResults(n, id, false, func(id string) report.Node { + ret.addChild(n, id, func(id string) report.Node { return report.MakeNode(id).WithTopology(report.Host) - }, n.Topology) + }) } } } @@ -63,10 +63,10 @@ func (e endpoints2Hosts) Render(rpt report.Report) Nodes { if !ok { continue } - ret.addToResults(n, id, true, newPseudoNode) + ret.addEndpointChild(n, id, newPseudoNode) } else { id := report.MakeHostNodeID(report.ExtractHostID(n)) - ret.addToResults(n, id, true, func(id string) report.Node { + ret.addEndpointChild(n, id, func(id string) report.Node { return report.MakeNode(id).WithTopology(report.Host). WithLatest(report.HostNodeID, timestamp, hostNodeID) }) diff --git a/render/process.go b/render/process.go index 541d1dc14..bae7640da 100644 --- a/render/process.go +++ b/render/process.go @@ -94,7 +94,7 @@ func (e endpoints2Processes) Render(rpt report.Report) Nodes { // Nodes without a hostid are treated as pseudo nodes if hostNodeID, ok := n.Latest.Lookup(report.HostNodeID); !ok { if id, ok := pseudoNodeID(n, local); ok { - ret.addToResults(n, id, true, newPseudoNode) + ret.addEndpointChild(n, id, newPseudoNode) } } else { pid, timestamp, ok := n.Latest.LookupEntry(process.PID) @@ -111,7 +111,7 @@ func (e endpoints2Processes) Render(rpt report.Report) Nodes { hostID, _, _ := report.ParseNodeID(hostNodeID) id := report.MakeProcessNodeID(hostID, pid) - ret.addToResults(n, id, true, func(id string) report.Node { + ret.addEndpointChild(n, id, func(id string) report.Node { if processNode, found := processes.Nodes[id]; found { return processNode } @@ -137,10 +137,10 @@ func processes2Names(processes Nodes) Nodes { } else { name, timestamp, ok := n.Latest.LookupEntry(process.Name) if ok { - ret.addToResults(n, name, true, func(id string) report.Node { + ret.addChildAndChildren(n, name, func(id string) report.Node { return report.MakeNode(id).WithTopology(MakeGroupNodeTopology(n.Topology, process.Name)). WithLatest(process.Name, timestamp, name) - }, n.Topology) + }) } } } diff --git a/render/render.go b/render/render.go index abf35519c..d27da3536 100644 --- a/render/render.go +++ b/render/render.go @@ -158,20 +158,42 @@ func newJoinResults() joinResults { return joinResults{nodes: make(report.Nodes), mapped: map[string]string{}} } -// Add Node M under id, creating a new result node if not already there -// incrementing a counter if nonblank, and updating the mapping from old ID to new ID -func (ret *joinResults) addToResults(m report.Node, id string, addChildren bool, create func(string) report.Node, counters ...string) { +// Add Endpoint Node m under id, creating a new result node if not already there +// and updating the mapping from old ID to new ID +// Note we do not update any counters for child topologies here because we never look at endpoint counts +func (ret *joinResults) addEndpointChild(m report.Node, id string, create func(string) report.Node) { result, exists := ret.nodes[id] if !exists { result = create(id) } result.Children = result.Children.Add(m) - if addChildren { - result.Children = result.Children.Merge(m.Children) + ret.nodes[id] = result + ret.mapped[m.ID] = id +} + +// Add Node m as a child of the node at id, creating a new result node if not already there +// incrementing a count of the topology, and updating the mapping from old ID to new ID +func (ret *joinResults) addChild(m report.Node, id string, create func(string) report.Node) { + result, exists := ret.nodes[id] + if !exists { + result = create(id) } - for _, c := range counters { - result.Counters = result.Counters.Add(c, 1) + result.Children = result.Children.Add(m) + result.Counters = result.Counters.Add(m.Topology, 1) + ret.nodes[id] = result + ret.mapped[m.ID] = id +} + +// Add m and its children as children under id, creating a new result node if not already there, +// incrementing a count of the topology, and updating the mapping from old ID to new ID +func (ret *joinResults) addChildAndChildren(m report.Node, id string, create func(string) report.Node) { + result, exists := ret.nodes[id] + if !exists { + result = create(id) } + result.Children = result.Children.Add(m) + result.Children = result.Children.Merge(m.Children) + result.Counters = result.Counters.Add(m.Topology, 1) ret.nodes[id] = result ret.mapped[m.ID] = id }