From 34e987323bac811710fa396042f8255968341a42 Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Mon, 9 Nov 2015 11:01:46 +0000 Subject: [PATCH] Review feedback --- render/detailed_node.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/render/detailed_node.go b/render/detailed_node.go index 398a6c9382..c1b767489c 100644 --- a/render/detailed_node.go +++ b/render/detailed_node.go @@ -303,14 +303,21 @@ func processOriginTable(nmd report.Node, addHostTag bool, addContainerTag bool) }, len(rows) > 0 || commFound || pidFound } -func sparklineRow(human, format string, scale float64, metric report.Metric) Row { +func sparklineRow(human string, metric report.Metric, format func(*report.Sample) string) Row { + if format == nil { + format = formatDefault + } lastStr := "" if last := metric.LastSample(); last != nil { - lastStr = fmt.Sprintf(format, last.Value/scale) + lastStr = format(last) } return Row{Key: human, ValueMajor: lastStr, Metric: metric, ValueType: "sparkline"} } +func formatDefault(s *report.Sample) string { return fmt.Sprintf("%0.2f", s.Value) } +func formatMB(s *report.Sample) string { return fmt.Sprintf("%0.2f MB", s.Value/1024*1024) } +func formatPercent(s *report.Sample) string { return fmt.Sprintf("%0.2f%%", s.Value) } + func containerOriginTable(nmd report.Node, addHostTag bool) (Table, bool) { rows := []Row{} for _, tuple := range []struct{ key, human string }{ @@ -337,10 +344,10 @@ func containerOriginTable(nmd report.Node, addHostTag bool) (Table, bool) { } if val, ok := nmd.Metrics[docker.MemoryUsage]; ok { - rows = append(rows, sparklineRow("Memory Usage", "%0.2f MB", 1024*1024, val)) + rows = append(rows, sparklineRow("Memory Usage", val, formatMB)) } if val, ok := nmd.Metrics[docker.CPUTotalUsage]; ok { - rows = append(rows, sparklineRow("CPU Usage", "%0.2f%%", 1, val)) + rows = append(rows, sparklineRow("CPU Usage", val, formatPercent)) } var ( @@ -430,7 +437,7 @@ func hostOriginTable(nmd report.Node) (Table, bool) { val.Max = maxLoad val.First = lastLoad.Add(-15 * time.Second) // TODO(paulbellamy): This should be based on the duration flag, maybe? or just auto-scale to data. val.Last = lastLoad - rows = append(rows, sparklineRow(tuple.human, "%0.2f", 1, val)) + rows = append(rows, sparklineRow(tuple.human, val, nil)) } } for _, tuple := range []struct{ key, human string }{