Skip to content

Commit

Permalink
minor refactor of backend metadata and metric rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbellamy committed Feb 5, 2016
1 parent 00cd193 commit 8e9983d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 80 deletions.
104 changes: 48 additions & 56 deletions render/detailed/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,48 @@ import (
)

var (
processNodeMetadata = renderMetadata(
ltst(process.PID),
ltst(process.PPID),
ltst(process.Cmdline),
ltst(process.Threads),
)
containerNodeMetadata = renderMetadata(
ltst(docker.ContainerID),
ltst(docker.ImageID),
ltst(docker.ContainerState),
ltst(docker.ContainerUptime),
ltst(docker.ContainerRestartCount),
sets(docker.ContainerIPs),
sets(docker.ContainerPorts),
ltst(docker.ContainerCreated),
ltst(docker.ContainerCommand),
ltst(overlay.WeaveMACAddress),
ltst(overlay.WeaveDNSHostname),
)
containerImageNodeMetadata = renderMetadata(
ltst(docker.ImageID),
)
podNodeMetadata = renderMetadata(
ltst(kubernetes.PodID),
ltst(kubernetes.Namespace),
ltst(kubernetes.PodCreated),
)
hostNodeMetadata = renderMetadata(
ltst(host.HostName),
ltst(host.OS),
ltst(host.KernelVersion),
ltst(host.Uptime),
sets(host.LocalNetworks),
)
processNodeMetadata = []MetadataRow{
{ID: process.PID, from: latest},
{ID: process.PPID, from: latest},
{ID: process.Cmdline, from: latest},
{ID: process.Threads, from: latest},
}
containerNodeMetadata = []MetadataRow{
{ID: docker.ContainerID, from: latest},
{ID: docker.ImageID, from: latest},
{ID: docker.ContainerState, from: latest},
{ID: docker.ContainerUptime, from: latest},
{ID: docker.ContainerRestartCount, from: latest},
{ID: docker.ContainerIPs, from: sets},
{ID: docker.ContainerPorts, from: sets},
{ID: docker.ContainerCreated, from: latest},
{ID: docker.ContainerCommand, from: latest},
{ID: overlay.WeaveMACAddress, from: latest},
{ID: overlay.WeaveDNSHostname, from: latest},
}
containerImageNodeMetadata = []MetadataRow{
{ID: docker.ImageID, from: latest},
}
podNodeMetadata = []MetadataRow{
{ID: kubernetes.PodID, from: latest},
{ID: kubernetes.Namespace, from: latest},
{ID: kubernetes.PodCreated, from: latest},
}
hostNodeMetadata = []MetadataRow{
{ID: host.HostName, from: latest},
{ID: host.OS, from: latest},
{ID: host.KernelVersion, from: latest},
{ID: host.Uptime, from: latest},
{ID: host.LocalNetworks, from: sets},
}
)

// MetadataRow is a row for the metadata table.
type MetadataRow struct {
ID string
Value string

from func(report.Node, string) []MetadataRow
}

// Copy returns a value copy of a metadata row.
Expand Down Expand Up @@ -80,43 +82,33 @@ func (m MetadataRow) MarshalJSON() ([]byte, error) {
// NodeMetadata 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 NodeMetadata(n report.Node) []MetadataRow {
renderers := map[string]func(report.Node) []MetadataRow{
renderers := map[string][]MetadataRow{
report.Process: processNodeMetadata,
report.Container: containerNodeMetadata,
report.ContainerImage: containerImageNodeMetadata,
report.Pod: podNodeMetadata,
report.Host: hostNodeMetadata,
}
if renderer, ok := renderers[n.Topology]; ok {
return renderer(n)
}
return nil
}

func renderMetadata(templates ...func(report.Node) []MetadataRow) func(report.Node) []MetadataRow {
return func(nmd report.Node) []MetadataRow {
if templates, ok := renderers[n.Topology]; ok {
rows := []MetadataRow{}
for _, template := range templates {
rows = append(rows, template(nmd)...)
rows = append(rows, template.from(n, template.ID)...)
}
return rows
}
return nil
}

func sets(id string) func(report.Node) []MetadataRow {
return func(n report.Node) []MetadataRow {
if val, ok := n.Sets.Lookup(id); ok && len(val) > 0 {
return []MetadataRow{{ID: id, Value: strings.Join(val, ", ")}}
}
return nil
func sets(n report.Node, id string) []MetadataRow {
if val, ok := n.Sets.Lookup(id); ok && len(val) > 0 {
return []MetadataRow{{ID: id, Value: strings.Join(val, ", ")}}
}
return nil
}

func ltst(id string) func(report.Node) []MetadataRow {
return func(n report.Node) []MetadataRow {
if val, ok := n.Latest.Lookup(id); ok {
return []MetadataRow{{ID: id, Value: val}}
}
return nil
func latest(n report.Node, id string) []MetadataRow {
if val, ok := n.Latest.Lookup(id); ok {
return []MetadataRow{{ID: id, Value: val}}
}
return nil
}
42 changes: 18 additions & 24 deletions render/detailed/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ const (
)

var (
processNodeMetrics = renderMetrics(
MetricRow{ID: process.CPUUsage, Format: percentFormat},
MetricRow{ID: process.MemoryUsage, Format: filesizeFormat},
)
containerNodeMetrics = renderMetrics(
MetricRow{ID: docker.CPUTotalUsage, Format: percentFormat},
MetricRow{ID: docker.MemoryUsage, Format: filesizeFormat},
)
hostNodeMetrics = renderMetrics(
MetricRow{ID: host.CPUUsage, Format: percentFormat},
MetricRow{ID: host.MemoryUsage, Format: filesizeFormat},
MetricRow{ID: host.Load1, Format: defaultFormat, Group: "load"},
MetricRow{ID: host.Load5, Format: defaultFormat, Group: "load"},
MetricRow{ID: host.Load15, Format: defaultFormat, Group: "load"},
)
processNodeMetrics = []MetricRow{
{ID: process.CPUUsage, Format: percentFormat},
{ID: process.MemoryUsage, Format: filesizeFormat},
}
containerNodeMetrics = []MetricRow{
{ID: docker.CPUTotalUsage, Format: percentFormat},
{ID: docker.MemoryUsage, Format: filesizeFormat},
}
hostNodeMetrics = []MetricRow{
{ID: host.CPUUsage, Format: percentFormat},
{ID: host.MemoryUsage, Format: filesizeFormat},
{ID: host.Load1, Format: defaultFormat, Group: "load"},
{ID: host.Load5, Format: defaultFormat, Group: "load"},
{ID: host.Load15, Format: defaultFormat, Group: "load"},
}
)

// MetricRow is a tuple of data used to render a metric as a sparkline and
Expand Down Expand Up @@ -82,19 +82,12 @@ func (m MetricRow) MarshalJSON() ([]byte, error) {
// NodeMetrics 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 NodeMetrics(n report.Node) []MetricRow {
renderers := map[string]func(report.Node) []MetricRow{
renderers := map[string][]MetricRow{
report.Process: processNodeMetrics,
report.Container: containerNodeMetrics,
report.Host: hostNodeMetrics,
}
if renderer, ok := renderers[n.Topology]; ok {
return renderer(n)
}
return nil
}

func renderMetrics(templates ...MetricRow) func(report.Node) []MetricRow {
return func(n report.Node) []MetricRow {
if templates, ok := renderers[n.Topology]; ok {
rows := []MetricRow{}
for _, template := range templates {
metric, ok := n.Metrics[template.ID]
Expand All @@ -110,6 +103,7 @@ func renderMetrics(templates ...MetricRow) func(report.Node) []MetricRow {
}
return rows
}
return nil
}

// toFixed truncates decimals of float64 down to specified precision
Expand Down

0 comments on commit 8e9983d

Please sign in to comment.