From c8ea7ba49ee8a191b09b07847e34646af23c1ef1 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Thu, 28 Dec 2017 16:21:33 +0000 Subject: [PATCH] refactor: simplify joinResults.add* After dropping extra metadata in the rest of this PR, our usage of joinResults.add* only ever ends creating minimal nodes, from just an id and topology. Hence joinResults.add* can be invoked with simply an id and topology instead of a generic node creation function. --- render/container.go | 7 ++++--- render/host.go | 12 ++++-------- render/id.go | 4 ---- render/process.go | 14 +++----------- render/render.go | 21 +++++++++++---------- 5 files changed, 22 insertions(+), 36 deletions(-) diff --git a/render/container.go b/render/container.go index a79051a1ae..71b889d854 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.addChild(m, id, newPseudoNode) + ret.addChild(m, id, Pseudo) continue } } @@ -94,8 +94,9 @@ 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 - // We are guaranteed to find the id, so no need to pass a node constructor. - ret.addChild(m, id, nil) + // We are guaranteed to find the id, so really this should + // never end up creating a node. + ret.addChild(m, id, report.Container) } } return ret.result(endpoints) diff --git a/render/host.go b/render/host.go index 10b8dc80d2..f368ff1810 100644 --- a/render/host.go +++ b/render/host.go @@ -16,10 +16,6 @@ var HostRenderer = MakeReduce( endpoints2Hosts{}, ) -func newHostNode(id string) report.Node { - return report.MakeNode(id).WithTopology(report.Host) -} - // nodes2Hosts maps any Nodes to host Nodes. // // If this function is given a node without a hostname @@ -45,9 +41,9 @@ func nodes2Hosts(nodes Nodes) Nodes { // hosts, and hence mapping these adjacencies to host // adjacencies would produce edges that aren't present // in reality. - ret.addUnmappedChild(n, id, newHostNode) + ret.addUnmappedChild(n, id, report.Host) } else { - ret.addChild(n, id, newHostNode) + ret.addChild(n, id, report.Host) } } } @@ -69,10 +65,10 @@ func (e endpoints2Hosts) 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.addChild(n, id, newPseudoNode) + ret.addChild(n, id, Pseudo) } } else { - ret.addChild(n, hostNodeID, newHostNode) + ret.addChild(n, hostNodeID, report.Host) } } return ret.result(endpoints) diff --git a/render/id.go b/render/id.go index 5fa34f8f24..3b8eda0a69 100644 --- a/render/id.go +++ b/render/id.go @@ -62,10 +62,6 @@ func NewDerivedPseudoNode(id string, node report.Node) report.Node { return output } -func newPseudoNode(id string) report.Node { - return report.MakeNode(id).WithTopology(Pseudo) -} - func pseudoNodeID(n report.Node, local report.Networks) (string, bool) { _, addr, _, ok := report.ParseEndpointNodeID(n.ID) if !ok { diff --git a/render/process.go b/render/process.go index 72a01b44a5..1fdd08b631 100644 --- a/render/process.go +++ b/render/process.go @@ -76,10 +76,6 @@ var ProcessNameRenderer = CustomRenderer{RenderFunc: processes2Names, Renderer: type endpoints2Processes struct { } -func newProcessNode(id string) report.Node { - return report.MakeNode(id).WithTopology(report.Process) -} - func (e endpoints2Processes) Render(rpt report.Report) Nodes { if len(rpt.Process.Nodes) == 0 { return Nodes{} @@ -93,7 +89,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.addChild(n, id, newPseudoNode) + ret.addChild(n, id, Pseudo) } } else { pid, ok := n.Latest.Lookup(process.PID) @@ -106,7 +102,7 @@ func (e endpoints2Processes) Render(rpt report.Report) Nodes { hostID, _ := report.ParseHostNodeID(hostNodeID) id := report.MakeProcessNodeID(hostID, pid) - ret.addChild(n, id, newProcessNode) + ret.addChild(n, id, report.Process) } } return ret.result(endpoints) @@ -141,10 +137,6 @@ func hasMoreThanOneConnection(n report.Node, endpoints report.Nodes) bool { var processNameTopology = MakeGroupNodeTopology(report.Process, process.Name) -func newProcessNameNode(id string) report.Node { - return report.MakeNode(id).WithTopology(processNameTopology) -} - // processes2Names maps process Nodes to Nodes for each process name. func processes2Names(processes Nodes) Nodes { ret := newJoinResults(nil) @@ -153,7 +145,7 @@ func processes2Names(processes Nodes) Nodes { if n.Topology == Pseudo { ret.passThrough(n) } else if name, ok := n.Latest.Lookup(process.Name); ok { - ret.addChildAndChildren(n, name, newProcessNameNode) + ret.addChildAndChildren(n, name, processNameTopology) } } return ret.result(processes) diff --git a/render/render.go b/render/render.go index c8b80138f5..ff4e57924b 100644 --- a/render/render.go +++ b/render/render.go @@ -184,12 +184,12 @@ func (ret *joinResults) mapChild(from, to string) { } } -// Add m as a child of the node at id, creating a new result node if -// not already there. -func (ret *joinResults) addUnmappedChild(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 in +// the specified topology if not already there. +func (ret *joinResults) addUnmappedChild(m report.Node, id string, topology string) { result, exists := ret.nodes[id] if !exists { - result = create(id) + result = report.MakeNode(id).WithTopology(topology) } result.Children = result.Children.Add(m) if m.Topology != report.Endpoint { // optimisation: we never look at endpoint counts @@ -198,16 +198,17 @@ func (ret *joinResults) addUnmappedChild(m report.Node, id string, create func(s ret.nodes[id] = result } -// 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) { - ret.addUnmappedChild(m, id, create) +// Add m as a child of the node at id, creating a new result node in +// the specified topology if not already there, and updating the +// mapping from old ID to new ID. +func (ret *joinResults) addChild(m report.Node, id string, topology string) { + ret.addUnmappedChild(m, id, topology) ret.mapChild(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) { - ret.addUnmappedChild(m, id, create) +func (ret *joinResults) addChildAndChildren(m report.Node, id string, topology string) { + ret.addUnmappedChild(m, id, topology) result := ret.nodes[id] result.Children = result.Children.Merge(m.Children) ret.nodes[id] = result