mirror of
https://github.com/weaveworks/scope.git
synced 2026-07-18 12:59:31 +00:00
Remove First and Last data members from Metrics structs
They can be trivially computed when required.
This commit is contained in:
@@ -84,8 +84,6 @@ func TestSummaries(t *testing.T) {
|
|||||||
Samples: nil,
|
Samples: nil,
|
||||||
Min: metric.Min,
|
Min: metric.Min,
|
||||||
Max: metric.Max,
|
Max: metric.Max,
|
||||||
First: metric.First,
|
|
||||||
Last: metric.Last,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(want, row) {
|
if !reflect.DeepEqual(want, row) {
|
||||||
|
|||||||
@@ -82,8 +82,6 @@ func (m *MetricRow) CodecEncodeSelf(encoder *codec.Encoder) {
|
|||||||
Samples: in.Samples,
|
Samples: in.Samples,
|
||||||
Min: in.Min,
|
Min: in.Min,
|
||||||
Max: in.Max,
|
Max: in.Max,
|
||||||
First: in.First,
|
|
||||||
Last: in.Last,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,8 +93,6 @@ func (m *MetricRow) CodecDecodeSelf(decoder *codec.Decoder) {
|
|||||||
Samples: in.Samples,
|
Samples: in.Samples,
|
||||||
Min: in.Min,
|
Min: in.Min,
|
||||||
Max: in.Max,
|
Max: in.Max,
|
||||||
First: in.First,
|
|
||||||
Last: in.Last,
|
|
||||||
}
|
}
|
||||||
metric := w.FromIntermediate()
|
metric := w.FromIntermediate()
|
||||||
*m = MetricRow{
|
*m = MetricRow{
|
||||||
|
|||||||
@@ -50,9 +50,15 @@ func (m Metrics) Copy() Metrics {
|
|||||||
type Metric struct {
|
type Metric struct {
|
||||||
Samples []Sample
|
Samples []Sample
|
||||||
Min, Max float64
|
Min, Max float64
|
||||||
First, Last time.Time
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Following two functions are exported for testing only: make sure there are samples before calling.
|
||||||
|
// First gives the timestamp of the first sample
|
||||||
|
func (m Metric) First() time.Time { return m.Samples[0].Timestamp }
|
||||||
|
|
||||||
|
// Last gives the timestamp of the last sample
|
||||||
|
func (m Metric) Last() time.Time { return m.Samples[len(m.Samples)-1].Timestamp }
|
||||||
|
|
||||||
// Sample is a single datapoint of a metric.
|
// Sample is a single datapoint of a metric.
|
||||||
type Sample struct {
|
type Sample struct {
|
||||||
Timestamp time.Time `json:"date"`
|
Timestamp time.Time `json:"date"`
|
||||||
@@ -65,8 +71,6 @@ func MakeSingletonMetric(t time.Time, v float64) Metric {
|
|||||||
Samples: []Sample{{t, v}},
|
Samples: []Sample{{t, v}},
|
||||||
Min: v,
|
Min: v,
|
||||||
Max: v,
|
Max: v,
|
||||||
First: t,
|
|
||||||
Last: t,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -97,8 +101,6 @@ func MakeMetric(samples []Sample) Metric {
|
|||||||
Samples: samples,
|
Samples: samples,
|
||||||
Min: min,
|
Min: min,
|
||||||
Max: max,
|
Max: max,
|
||||||
First: samples[0].Timestamp,
|
|
||||||
Last: samples[len(samples)-1].Timestamp,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,8 +110,6 @@ func (m Metric) WithMax(max float64) Metric {
|
|||||||
Samples: m.Samples,
|
Samples: m.Samples,
|
||||||
Max: max,
|
Max: max,
|
||||||
Min: m.Min,
|
Min: m.Min,
|
||||||
First: m.First,
|
|
||||||
Last: m.Last,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,20 +118,6 @@ func (m Metric) Len() int {
|
|||||||
return len(m.Samples)
|
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.
|
// Merge combines the two Metrics and returns a new result.
|
||||||
func (m Metric) Merge(other Metric) Metric {
|
func (m Metric) Merge(other Metric) Metric {
|
||||||
|
|
||||||
@@ -141,7 +127,7 @@ func (m Metric) Merge(other Metric) Metric {
|
|||||||
return other
|
return other
|
||||||
case len(other.Samples) == 0:
|
case len(other.Samples) == 0:
|
||||||
return m
|
return m
|
||||||
case other.First.After(m.Last):
|
case other.First().After(m.Last()):
|
||||||
samplesOut := make([]Sample, len(m.Samples)+len(other.Samples))
|
samplesOut := make([]Sample, len(m.Samples)+len(other.Samples))
|
||||||
copy(samplesOut, m.Samples)
|
copy(samplesOut, m.Samples)
|
||||||
copy(samplesOut[len(m.Samples):], other.Samples)
|
copy(samplesOut[len(m.Samples):], other.Samples)
|
||||||
@@ -149,10 +135,8 @@ func (m Metric) Merge(other Metric) Metric {
|
|||||||
Samples: samplesOut,
|
Samples: samplesOut,
|
||||||
Max: math.Max(m.Max, other.Max),
|
Max: math.Max(m.Max, other.Max),
|
||||||
Min: math.Min(m.Min, other.Min),
|
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))
|
samplesOut := make([]Sample, len(m.Samples)+len(other.Samples))
|
||||||
copy(samplesOut, other.Samples)
|
copy(samplesOut, other.Samples)
|
||||||
copy(samplesOut[len(other.Samples):], m.Samples)
|
copy(samplesOut[len(other.Samples):], m.Samples)
|
||||||
@@ -160,8 +144,6 @@ func (m Metric) Merge(other Metric) Metric {
|
|||||||
Samples: samplesOut,
|
Samples: samplesOut,
|
||||||
Max: math.Max(m.Max, other.Max),
|
Max: math.Max(m.Max, other.Max),
|
||||||
Min: math.Min(m.Min, other.Min),
|
Min: math.Min(m.Min, other.Min),
|
||||||
First: other.First,
|
|
||||||
Last: m.Last,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,8 +176,6 @@ func (m Metric) Merge(other Metric) Metric {
|
|||||||
Samples: samplesOut,
|
Samples: samplesOut,
|
||||||
Max: math.Max(m.Max, other.Max),
|
Max: math.Max(m.Max, other.Max),
|
||||||
Min: math.Min(m.Min, other.Min),
|
Min: math.Min(m.Min, other.Min),
|
||||||
First: first(m.First, other.First),
|
|
||||||
Last: last(m.Last, other.Last),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -214,8 +194,6 @@ type WireMetrics struct {
|
|||||||
Samples []Sample `json:"samples,omitempty"`
|
Samples []Sample `json:"samples,omitempty"`
|
||||||
Min float64 `json:"min"`
|
Min float64 `json:"min"`
|
||||||
Max float64 `json:"max"`
|
Max float64 `json:"max"`
|
||||||
First string `json:"first,omitempty"`
|
|
||||||
Last string `json:"last,omitempty"`
|
|
||||||
dummySelfer
|
dummySelfer
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,8 +219,6 @@ func (m Metric) ToIntermediate() WireMetrics {
|
|||||||
Samples: m.Samples,
|
Samples: m.Samples,
|
||||||
Max: m.Max,
|
Max: m.Max,
|
||||||
Min: m.Min,
|
Min: m.Min,
|
||||||
First: renderTime(m.First),
|
|
||||||
Last: renderTime(m.Last),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,8 +229,6 @@ func (m WireMetrics) FromIntermediate() Metric {
|
|||||||
Samples: m.Samples,
|
Samples: m.Samples,
|
||||||
Max: m.Max,
|
Max: m.Max,
|
||||||
Min: m.Min,
|
Min: m.Min,
|
||||||
First: parseTime(m.First),
|
|
||||||
Last: parseTime(m.Last),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -50,11 +50,11 @@ func TestMetricsCopy(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func checkMetric(t *testing.T, metric report.Metric, first, last time.Time, min, max float64) {
|
func checkMetric(t *testing.T, metric report.Metric, first, last time.Time, min, max float64) {
|
||||||
if !metric.First.Equal(first) {
|
if !metric.First().Equal(first) {
|
||||||
t.Errorf("Expected metric.First == %q, but was: %q", first, metric.First)
|
t.Errorf("Expected metric.First == %q, but was: %q", first, metric.First())
|
||||||
}
|
}
|
||||||
if !metric.Last.Equal(last) {
|
if !metric.Last().Equal(last) {
|
||||||
t.Errorf("Expected metric.Last == %q, but was: %q", last, metric.Last)
|
t.Errorf("Expected metric.Last == %q, but was: %q", last, metric.Last())
|
||||||
}
|
}
|
||||||
if metric.Min != min {
|
if metric.Min != min {
|
||||||
t.Errorf("Expected metric.Min == %f, but was: %f", min, metric.Min)
|
t.Errorf("Expected metric.Min == %f, but was: %f", min, metric.Min)
|
||||||
@@ -65,9 +65,6 @@ func checkMetric(t *testing.T, metric report.Metric, first, last time.Time, min,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMetricFirstLastMinMax(t *testing.T) {
|
func TestMetricFirstLastMinMax(t *testing.T) {
|
||||||
|
|
||||||
checkMetric(t, report.MakeMetric(nil), time.Time{}, time.Time{}, 0.0, 0.0)
|
|
||||||
|
|
||||||
t1 := time.Now()
|
t1 := time.Now()
|
||||||
t2 := time.Now().Add(1 * time.Minute)
|
t2 := time.Now().Add(1 * time.Minute)
|
||||||
|
|
||||||
@@ -103,18 +100,7 @@ func TestMetricMerge(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check it didn't modify metric1
|
// Check it didn't modify metric1
|
||||||
if !metric1.First.Equal(t2) {
|
checkMetric(t, metric1, t2, t3, 0.2, 0.31)
|
||||||
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
|
// Check the result is not the same instance as metric1
|
||||||
if &metric1 == &have {
|
if &metric1 == &have {
|
||||||
|
|||||||
Reference in New Issue
Block a user