From 346a0360ef849d0a25c7555dca4d4ac9d2a57e1b Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Sat, 11 Nov 2017 11:47:01 +0000 Subject: [PATCH] Refactor: split addToResults into two functions This is preparatory to future refactorings: all existing calls are to Endpoints which have no children and where we don't want a Counter. We make addChildAndChildren an obvious extension of addChild even though it adds a dead code path (we never call addChildAndChildren with an endpoint). --- render/container.go | 4 ++-- render/host.go | 4 ++-- render/process.go | 4 ++-- render/render.go | 25 ++++++++++++++++++++----- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/render/container.go b/render/container.go index 34760b216..0dda44286 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, newPseudoNode) + ret.addChild(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, func(id string) report.Node { + ret.addChild(m, id, func(id string) report.Node { return inputNodes.Nodes[id] }) } diff --git a/render/host.go b/render/host.go index 484142260..c40970818 100644 --- a/render/host.go +++ b/render/host.go @@ -79,10 +79,10 @@ func (e endpoints2Hosts) Render(rpt report.Report) Nodes { if !ok { continue } - ret.addToResults(n, id, newPseudoNode) + ret.addChild(n, id, newPseudoNode) } else { id := report.MakeHostNodeID(report.ExtractHostID(n)) - ret.addToResults(n, id, func(id string) report.Node { + ret.addChild(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 ad174a9ad..eacf95f91 100644 --- a/render/process.go +++ b/render/process.go @@ -99,7 +99,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, newPseudoNode) + ret.addChild(n, id, newPseudoNode) } } else { pid, timestamp, ok := n.Latest.LookupEntry(process.PID) @@ -116,7 +116,7 @@ func (e endpoints2Processes) Render(rpt report.Report) Nodes { hostID, _, _ := report.ParseNodeID(hostNodeID) id := report.MakeProcessNodeID(hostID, pid) - ret.addToResults(n, id, func(id string) report.Node { + ret.addChild(n, id, func(id string) report.Node { if processNode, found := processes.Nodes[id]; found { return processNode } diff --git a/render/render.go b/render/render.go index 1d7983229..b67e84e64 100644 --- a/render/render.go +++ b/render/render.go @@ -158,17 +158,32 @@ 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 -// and updating the mapping from old ID to new ID -// Note we do not update any counters for child topologies here, because addToResults -// is only ever called when m is an endpoint and we never look at endpoint counts -func (ret *joinResults) addToResults(m report.Node, id string, create func(string) report.Node) { +// Add m as a child of the node at id, creating a new result node if +// not already there, 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) + } + result.Children = result.Children.Add(m) + if m.Topology != report.Endpoint { // optimisation: we never look at endpoint counts + result.Counters = result.Counters.Add(m.Topology, 1) + } + ret.nodes[id] = result + ret.mapped[m.ID] = id +} + +// Like addChild, but also add m's children. +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) + if m.Topology != report.Endpoint { // optimisation: we never look at endpoint counts + result.Counters = result.Counters.Add(m.Topology, 1) + } ret.nodes[id] = result ret.mapped[m.ID] = id }