diff --git a/report/detailed_node.go b/report/detailed_node.go index c9b8aa9349..a841e7a98b 100644 --- a/report/detailed_node.go +++ b/report/detailed_node.go @@ -1,9 +1,6 @@ package report -import ( - "reflect" - "strconv" -) +import "strconv" // MakeDetailedNode transforms a renderable node to a detailed node. It uses // aggregate metadata, plus the set of origin node IDs, to produce tables. @@ -29,19 +26,12 @@ func MakeDetailedNode(r Report, n RenderableNode) DetailedNode { // multiple origins. The ultimate goal here is to generate tables to view // in the UI, so we skip the intermediate representations, but we could // add them later. -outer: for _, id := range n.Origins { - table, ok := r.OriginTable(id) - if !ok { - continue + if table, ok := OriginTable(r, id); ok { + // Origin node IDs are unique, so we'll be optimistic, here, and + // assume they'll also produce unique tables. + tables = append(tables, table) } - // Naïve equivalence-based deduplication. - for _, existing := range tables { - if reflect.DeepEqual(existing, table) { - continue outer - } - } - tables = append(tables, table) } return DetailedNode{ @@ -52,3 +42,66 @@ outer: Tables: tables, } } + +// OriginTable produces a table (to be consumed directly by the UI) based on +// an origin ID, which is (optimistically) a node ID in one of our topologies. +func OriginTable(r Report, originID string) (Table, bool) { + if nmd, ok := r.Endpoint.NodeMetadatas[originID]; ok { + return endpointOriginTable(nmd) + } + if nmd, ok := r.Address.NodeMetadatas[originID]; ok { + return addressOriginTable(nmd) + } + if nmd, ok := r.Host.NodeMetadatas[originID]; ok { + return hostOriginTable(nmd) + } + return Table{}, false +} + +func endpointOriginTable(nmd NodeMetadata) (Table, bool) { + rows := []Row{} + if val, ok := nmd["endpoint"]; ok { + rows = append(rows, Row{"Endpoint", val, ""}) + } + if val, ok := nmd["host_name"]; ok { + rows = append(rows, Row{"Host name", val, ""}) + } + return Table{ + Title: "Origin Endpoint", + Numeric: false, + Rows: rows, + }, len(rows) > 0 +} + +func addressOriginTable(nmd NodeMetadata) (Table, bool) { + rows := []Row{} + if val, ok := nmd["address"]; ok { + rows = append(rows, Row{"Address", val, ""}) + } + if val, ok := nmd["host_name"]; ok { + rows = append(rows, Row{"Host name", val, ""}) + } + return Table{ + Title: "Origin Address", + Numeric: false, + Rows: rows, + }, len(rows) > 0 +} + +func hostOriginTable(nmd NodeMetadata) (Table, bool) { + rows := []Row{} + if val, ok := nmd["host_name"]; ok { + rows = append(rows, Row{"Host name", val, ""}) + } + if val, ok := nmd["load"]; ok { + rows = append(rows, Row{"Load", val, ""}) + } + if val, ok := nmd["os"]; ok { + rows = append(rows, Row{"Operating system", val, ""}) + } + return Table{ + Title: "Origin Host", + Numeric: false, + Rows: rows, + }, len(rows) > 0 +} diff --git a/report/report.go b/report/report.go index 64405be672..c3b0827ebb 100644 --- a/report/report.go +++ b/report/report.go @@ -110,72 +110,3 @@ func (r Report) LocalNetworks() []*net.IPNet { } return ipNets } - -// OriginTable produces a table (to be consumed directly by the UI) based on -// an origin ID, which is (optimistically) a node ID in one of our topologies. -func (r Report) OriginTable(originID string) (Table, bool) { - for nodeID, nodeMetadata := range r.Endpoint.NodeMetadatas { - if originID == nodeID { - return endpointOriginTable(nodeMetadata) - } - } - for nodeID, nodeMetadata := range r.Address.NodeMetadatas { - if originID == nodeID { - return addressOriginTable(nodeMetadata) - } - } - for nodeID, nodeMetadata := range r.Host.NodeMetadatas { - if originID == nodeID { - return hostOriginTable(nodeMetadata) - } - } - return Table{}, false -} - -func endpointOriginTable(nmd NodeMetadata) (Table, bool) { - rows := []Row{} - if val, ok := nmd["endpoint"]; ok { - rows = append(rows, Row{"Endpoint", val, ""}) - } - if val, ok := nmd["host_name"]; ok { - rows = append(rows, Row{"Host name", val, ""}) - } - return Table{ - Title: "Origin Endpoint", - Numeric: false, - Rows: rows, - }, len(rows) > 0 -} - -func addressOriginTable(nmd NodeMetadata) (Table, bool) { - rows := []Row{} - if val, ok := nmd["address"]; ok { - rows = append(rows, Row{"Address", val, ""}) - } - if val, ok := nmd["host_name"]; ok { - rows = append(rows, Row{"Host name", val, ""}) - } - return Table{ - Title: "Origin Address", - Numeric: false, - Rows: rows, - }, len(rows) > 0 -} - -func hostOriginTable(nmd NodeMetadata) (Table, bool) { - rows := []Row{} - if val, ok := nmd["host_name"]; ok { - rows = append(rows, Row{"Host name", val, ""}) - } - if val, ok := nmd["load"]; ok { - rows = append(rows, Row{"Load", val, ""}) - } - if val, ok := nmd["os"]; ok { - rows = append(rows, Row{"Operating system", val, ""}) - } - return Table{ - Title: "Origin Host", - Numeric: false, - Rows: rows, - }, len(rows) > 0 -}