From 91766999418eb63c52c94ce44e0f10cb8481e2db Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Thu, 12 Nov 2015 13:01:02 +0000 Subject: [PATCH 1/2] omit metric first/last timestamps if they are zero --- report/metrics.go | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/report/metrics.go b/report/metrics.go index b8ae93c4f..3d8711ae0 100644 --- a/report/metrics.go +++ b/report/metrics.go @@ -205,11 +205,23 @@ func (m Metric) LastSample() *Sample { // WireMetrics is the on-the-wire representation of Metrics. type WireMetrics struct { - Samples []Sample `json:"samples"` // On the wire, samples are sorted oldest to newest, - Min float64 `json:"min"` // the opposite order to how we store them internally. - Max float64 `json:"max"` - First time.Time `json:"first"` - Last time.Time `json:"last"` + Samples []Sample `json:"samples"` // On the wire, samples are sorted oldest to newest, + Min float64 `json:"min"` // the opposite order to how we store them internally. + Max float64 `json:"max"` + First string `json:"first,omitempty"` + Last string `json:"last,omitempty"` +} + +func renderTime(t time.Time) string { + if t.IsZero() { + return "" + } + return t.Format(time.RFC3339Nano) +} + +func parseTime(s string) time.Time { + t, _ := time.Parse(time.RFC3339Nano, s) + return t } func (m Metric) toIntermediate() WireMetrics { @@ -223,8 +235,8 @@ func (m Metric) toIntermediate() WireMetrics { Samples: samples, Max: m.Max, Min: m.Min, - First: m.First, - Last: m.Last, + First: renderTime(m.First), + Last: renderTime(m.Last), } } @@ -237,8 +249,8 @@ func (m WireMetrics) fromIntermediate() Metric { Samples: samples, Max: m.Max, Min: m.Min, - First: m.First, - Last: m.Last, + First: parseTime(m.First), + Last: parseTime(m.Last), } } From 5dcd53e24cffe6b629d5f21a021d4d13599db48d Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Thu, 12 Nov 2015 13:01:20 +0000 Subject: [PATCH 2/2] fix bug in metrics first/last calculation if second arg was zero --- report/metrics.go | 4 ++-- report/metrics_internal_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 report/metrics_internal_test.go diff --git a/report/metrics.go b/report/metrics.go index 3d8711ae0..144f0c74f 100644 --- a/report/metrics.go +++ b/report/metrics.go @@ -79,14 +79,14 @@ func (m Metric) Len() int { } func first(t1, t2 time.Time) time.Time { - if !t1.IsZero() && t1.Before(t2) { + if t2.IsZero() || (!t1.IsZero() && t1.Before(t2)) { return t1 } return t2 } func last(t1, t2 time.Time) time.Time { - if !t1.IsZero() && t1.After(t2) { + if t2.IsZero() || (!t1.IsZero() && t1.After(t2)) { return t1 } return t2 diff --git a/report/metrics_internal_test.go b/report/metrics_internal_test.go new file mode 100644 index 000000000..0929ba098 --- /dev/null +++ b/report/metrics_internal_test.go @@ -0,0 +1,29 @@ +package report + +import ( + "testing" + "time" +) + +func TestFirstLast(t *testing.T) { + zero, t1, t2 := time.Time{}, time.Now(), time.Now().Add(1*time.Minute) + tests := []struct { + arg1, arg2, first, last time.Time + }{ + {zero, zero, zero, zero}, + {t1, zero, t1, t1}, + {zero, t1, t1, t1}, + {t1, t1, t1, t1}, + {t1, t2, t1, t2}, + {t2, t1, t1, t2}, + } + for _, test := range tests { + if got := first(test.arg1, test.arg2); !got.Equal(test.first) { + t.Errorf("first(%q, %q) => %q, Expected: %q", test.arg1, test.arg2, got, test.first) + } + + if got := last(test.arg1, test.arg2); !got.Equal(test.last) { + t.Errorf("last(%q, %q) => %q, Expected: %q", test.arg1, test.arg2, got, test.last) + } + } +}