Merge pull request #3266 from weaveworks/slim-metrics

Remove First and Last data members from Metrics structs
This commit is contained in:
Bryan Boreham
2018-08-24 17:39:21 +01:00
committed by GitHub
5 changed files with 13 additions and 106 deletions
-2
View File
@@ -84,8 +84,6 @@ func TestSummaries(t *testing.T) {
Samples: nil,
Min: metric.Min,
Max: metric.Max,
First: metric.First,
Last: metric.Last,
},
}
if !reflect.DeepEqual(want, row) {
-4
View File
@@ -82,8 +82,6 @@ func (m *MetricRow) CodecEncodeSelf(encoder *codec.Encoder) {
Samples: in.Samples,
Min: in.Min,
Max: in.Max,
First: in.First,
Last: in.Last,
})
}
@@ -95,8 +93,6 @@ func (m *MetricRow) CodecDecodeSelf(decoder *codec.Decoder) {
Samples: in.Samples,
Min: in.Min,
Max: in.Max,
First: in.First,
Last: in.Last,
}
metric := w.FromIntermediate()
*m = MetricRow{
+7 -37
View File
@@ -48,11 +48,13 @@ func (m Metrics) Copy() Metrics {
// Metric is a list of timeseries data with some metadata. Clients must use the
// Add method to add values. Metrics are immutable.
type Metric struct {
Samples []Sample
Min, Max float64
First, Last time.Time
Samples []Sample
Min, Max float64
}
func (m Metric) first() time.Time { return m.Samples[0].Timestamp }
func (m Metric) last() time.Time { return m.Samples[len(m.Samples)-1].Timestamp }
// Sample is a single datapoint of a metric.
type Sample struct {
Timestamp time.Time `json:"date"`
@@ -65,8 +67,6 @@ func MakeSingletonMetric(t time.Time, v float64) Metric {
Samples: []Sample{{t, v}},
Min: v,
Max: v,
First: t,
Last: t,
}
}
@@ -97,8 +97,6 @@ func MakeMetric(samples []Sample) Metric {
Samples: samples,
Min: min,
Max: max,
First: samples[0].Timestamp,
Last: samples[len(samples)-1].Timestamp,
}
}
@@ -108,8 +106,6 @@ func (m Metric) WithMax(max float64) Metric {
Samples: m.Samples,
Max: max,
Min: m.Min,
First: m.First,
Last: m.Last,
}
}
@@ -118,20 +114,6 @@ func (m Metric) Len() int {
return len(m.Samples)
}
func first(t1, t2 time.Time) time.Time {
if t2.IsZero() || (!t1.IsZero() && t1.Before(t2)) {
return t1
}
return t2
}
func last(t1, t2 time.Time) time.Time {
if t2.IsZero() || (!t1.IsZero() && t1.After(t2)) {
return t1
}
return t2
}
// Merge combines the two Metrics and returns a new result.
func (m Metric) Merge(other Metric) Metric {
@@ -141,7 +123,7 @@ func (m Metric) Merge(other Metric) Metric {
return other
case len(other.Samples) == 0:
return m
case other.First.After(m.Last):
case other.first().After(m.last()):
samplesOut := make([]Sample, len(m.Samples)+len(other.Samples))
copy(samplesOut, m.Samples)
copy(samplesOut[len(m.Samples):], other.Samples)
@@ -149,10 +131,8 @@ func (m Metric) Merge(other Metric) Metric {
Samples: samplesOut,
Max: math.Max(m.Max, other.Max),
Min: math.Min(m.Min, other.Min),
First: m.First,
Last: other.Last,
}
case m.First.After(other.Last):
case m.first().After(other.last()):
samplesOut := make([]Sample, len(m.Samples)+len(other.Samples))
copy(samplesOut, other.Samples)
copy(samplesOut[len(other.Samples):], m.Samples)
@@ -160,8 +140,6 @@ func (m Metric) Merge(other Metric) Metric {
Samples: samplesOut,
Max: math.Max(m.Max, other.Max),
Min: math.Min(m.Min, other.Min),
First: other.First,
Last: m.Last,
}
}
@@ -194,8 +172,6 @@ func (m Metric) Merge(other Metric) Metric {
Samples: samplesOut,
Max: math.Max(m.Max, other.Max),
Min: math.Min(m.Min, other.Min),
First: first(m.First, other.First),
Last: last(m.Last, other.Last),
}
}
@@ -214,8 +190,6 @@ type WireMetrics struct {
Samples []Sample `json:"samples,omitempty"`
Min float64 `json:"min"`
Max float64 `json:"max"`
First string `json:"first,omitempty"`
Last string `json:"last,omitempty"`
dummySelfer
}
@@ -241,8 +215,6 @@ func (m Metric) ToIntermediate() WireMetrics {
Samples: m.Samples,
Max: m.Max,
Min: m.Min,
First: renderTime(m.First),
Last: renderTime(m.Last),
}
}
@@ -253,8 +225,6 @@ func (m WireMetrics) FromIntermediate() Metric {
Samples: m.Samples,
Max: m.Max,
Min: m.Min,
First: parseTime(m.First),
Last: parseTime(m.Last),
}
}
-29
View File
@@ -1,29 +0,0 @@
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)
}
}
}
+6 -34
View File
@@ -49,13 +49,7 @@ func TestMetricsCopy(t *testing.T) {
}
}
func checkMetric(t *testing.T, metric report.Metric, first, last time.Time, min, max float64) {
if !metric.First.Equal(first) {
t.Errorf("Expected metric.First == %q, but was: %q", first, metric.First)
}
if !metric.Last.Equal(last) {
t.Errorf("Expected metric.Last == %q, but was: %q", last, metric.Last)
}
func checkMetric(t *testing.T, metric report.Metric, min, max float64) {
if metric.Min != min {
t.Errorf("Expected metric.Min == %f, but was: %f", min, metric.Min)
}
@@ -65,24 +59,21 @@ func checkMetric(t *testing.T, metric report.Metric, first, last time.Time, min,
}
func TestMetricFirstLastMinMax(t *testing.T) {
checkMetric(t, report.MakeMetric(nil), time.Time{}, time.Time{}, 0.0, 0.0)
t1 := time.Now()
t2 := time.Now().Add(1 * time.Minute)
metric1 := report.MakeMetric([]report.Sample{{Timestamp: t1, Value: -0.1}, {Timestamp: t2, Value: 0.2}})
checkMetric(t, metric1, t1, t2, -0.1, 0.2)
checkMetric(t, metric1.Merge(metric1), t1, t2, -0.1, 0.2)
checkMetric(t, metric1, -0.1, 0.2)
checkMetric(t, metric1.Merge(metric1), -0.1, 0.2)
t3 := time.Now().Add(2 * time.Minute)
t4 := time.Now().Add(3 * time.Minute)
metric2 := report.MakeMetric([]report.Sample{{Timestamp: t3, Value: 0.31}, {Timestamp: t4, Value: 0.4}})
checkMetric(t, metric2, t3, t4, 0.31, 0.4)
checkMetric(t, metric1.Merge(metric2), t1, t4, -0.1, 0.4)
checkMetric(t, metric2.Merge(metric1), t1, t4, -0.1, 0.4)
checkMetric(t, metric2, 0.31, 0.4)
checkMetric(t, metric1.Merge(metric2), -0.1, 0.4)
checkMetric(t, metric2.Merge(metric1), -0.1, 0.4)
}
func TestMetricMerge(t *testing.T) {
@@ -101,25 +92,6 @@ func TestMetricMerge(t *testing.T) {
if !reflect.DeepEqual(want, have) {
t.Errorf("diff: %s", test.Diff(want, have))
}
// Check it didn't modify metric1
if !metric1.First.Equal(t2) {
t.Errorf("Expected metric1.First == %q, but was: %q", t2, metric1.First)
}
if !metric1.Last.Equal(t3) {
t.Errorf("Expected metric1.Last == %q, but was: %q", t3, metric1.Last)
}
if metric1.Min != 0.2 {
t.Errorf("Expected metric1.Min == %f, but was: %f", 0.2, metric1.Min)
}
if metric1.Max != 0.31 {
t.Errorf("Expected metric1.Max == %f, but was: %f", 0.31, metric1.Max)
}
// Check the result is not the same instance as metric1
if &metric1 == &have {
t.Errorf("Expected different pointers for metric1 and have, but both were: %p", &have)
}
}
func TestMetricMarshalling(t *testing.T) {