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.
This commit is contained in:
Matthias Radestock
2017-12-28 16:21:33 +00:00
parent a7b6bb09a1
commit c8ea7ba49e
5 changed files with 22 additions and 36 deletions

View File

@@ -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)

View File

@@ -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)

View File

@@ -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 {

View File

@@ -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)

View File

@@ -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