veyron/lib/stats/counter: Fix delta value

Before the counter history is full, use the last value as delta. This
means that for the first minute, Value() and Delta1m() will be equal,
for the first 10 minutes, Value() and Delta10m() will be equal, and the
same for 1 hour.

This is more intuitive than the previous behavior, which always
subtracted the oldest value from the newest one.

Change-Id: Ia154f073416e084d4780b8ecd00e49562113524f
diff --git a/lib/stats/counter/counter_test.go b/lib/stats/counter/counter_test.go
index 727dd56..5f3e3e2 100644
--- a/lib/stats/counter/counter_test.go
+++ b/lib/stats/counter/counter_test.go
@@ -20,12 +20,12 @@
 	now = now.Add(time.Second)
 	c.Incr(1)
 
-	if expected, got := int64(1), c.Delta1m(); got != expected {
+	if expected, got := int64(2), c.Delta1m(); got != expected {
 		t.Errorf("Unexpected value. Got %v, want %v", got, expected)
 	}
 	// One second later.
 	now = now.Add(time.Second)
-	if expected, got := int64(1), c.Delta1m(); got != expected {
+	if expected, got := int64(2), c.Delta1m(); got != expected {
 		t.Errorf("Unexpected value. Got %v, want %v", got, expected)
 	}
 	now = time.Unix(1, 0)
@@ -45,11 +45,11 @@
 	// Time 5, value=5
 	now = now.Add(time.Second)
 	c.Incr(1)
-	// Expect current value=5 and delta1m=(5-1)=4
+	// Expect current value=5 and delta1m=(5-0)=5
 	if expected, got := int64(5), c.Value(); got != expected {
 		t.Errorf("Unexpected value. Got %v, want %v", got, expected)
 	}
-	if expected, got := int64(4), c.Delta1m(); got != expected {
+	if expected, got := int64(5), c.Delta1m(); got != expected {
 		t.Errorf("Unexpected value. Got %v, want %v", got, expected)
 	}
 	now = time.Unix(0, 0)
diff --git a/lib/stats/counter/timeseries.go b/lib/stats/counter/timeseries.go
index ad4d1f7..06b5981 100644
--- a/lib/stats/counter/timeseries.go
+++ b/lib/stats/counter/timeseries.go
@@ -77,7 +77,7 @@
 // tailValue returns the oldest value from the timeseries.
 func (ts *timeseries) tailValue() int64 {
 	if ts.stepCount < int64(ts.size) {
-		return ts.slots[0]
+		return 0
 	}
 	return ts.slots[(ts.head+1)%ts.size]
 }
diff --git a/lib/stats/counter/timeseries_test.go b/lib/stats/counter/timeseries_test.go
index d057295..a05191e 100644
--- a/lib/stats/counter/timeseries_test.go
+++ b/lib/stats/counter/timeseries_test.go
@@ -33,7 +33,7 @@
 	if expected, got := int64(345), ts.headValue(); got != expected {
 		t.Errorf("unexpected value. Got %v, want %v", got, expected)
 	}
-	if expected, got := int64(234), ts.tailValue(); got != expected {
+	if expected, got := int64(0), ts.tailValue(); got != expected {
 		t.Errorf("unexpected value. Got %v, want %v", got, expected)
 	}
 	if expected, got := int64(234), ts.min(); got != expected {
@@ -50,7 +50,7 @@
 	if expected, got := int64(111), ts.headValue(); got != expected {
 		t.Errorf("unexpected value. Got %v, want %v", got, expected)
 	}
-	if expected, got := int64(234), ts.tailValue(); got != expected {
+	if expected, got := int64(0), ts.tailValue(); got != expected {
 		t.Errorf("unexpected value. Got %v, want %v", got, expected)
 	}
 	if expected, got := int64(111), ts.min(); got != expected {
diff --git a/lib/stats/stats_test.go b/lib/stats/stats_test.go
index 37a68e0..8b9d70d 100644
--- a/lib/stats/stats_test.go
+++ b/lib/stats/stats_test.go
@@ -100,9 +100,9 @@
 		libstats.KeyValue{Key: "ipc/test/bbb", Value: float64(2)},
 		libstats.KeyValue{Key: "ipc/test/ccc", Value: string("Hello")},
 		libstats.KeyValue{Key: "ipc/test/ddd", Value: int64(4)},
-		libstats.KeyValue{Key: "ipc/test/ddd/delta10m", Value: int64(0)},
-		libstats.KeyValue{Key: "ipc/test/ddd/delta1h", Value: int64(0)},
-		libstats.KeyValue{Key: "ipc/test/ddd/delta1m", Value: int64(0)},
+		libstats.KeyValue{Key: "ipc/test/ddd/delta10m", Value: int64(4)},
+		libstats.KeyValue{Key: "ipc/test/ddd/delta1h", Value: int64(4)},
+		libstats.KeyValue{Key: "ipc/test/ddd/delta1m", Value: int64(4)},
 		libstats.KeyValue{Key: "ipc/test/ddd/rate10m", Value: float64(0)},
 		libstats.KeyValue{Key: "ipc/test/ddd/rate1h", Value: float64(0)},
 		libstats.KeyValue{Key: "ipc/test/ddd/rate1m", Value: float64(0)},
@@ -156,12 +156,12 @@
 		t.Errorf("unexpected error: %v", err)
 	}
 	expected = []libstats.KeyValue{
-		libstats.KeyValue{Key: "ipc/test/ddd/delta10m", Value: int64(100)},
-		libstats.KeyValue{Key: "ipc/test/ddd/delta1h", Value: int64(0)},
-		libstats.KeyValue{Key: "ipc/test/ddd/delta1m", Value: int64(100)},
-		libstats.KeyValue{Key: "ipc/test/ddd/rate10m", Value: float64(10)},
+		libstats.KeyValue{Key: "ipc/test/ddd/delta10m", Value: int64(104)},
+		libstats.KeyValue{Key: "ipc/test/ddd/delta1h", Value: int64(104)},
+		libstats.KeyValue{Key: "ipc/test/ddd/delta1m", Value: int64(104)},
+		libstats.KeyValue{Key: "ipc/test/ddd/rate10m", Value: float64(10.4)},
 		libstats.KeyValue{Key: "ipc/test/ddd/rate1h", Value: float64(0)},
-		libstats.KeyValue{Key: "ipc/test/ddd/rate1m", Value: float64(10)},
+		libstats.KeyValue{Key: "ipc/test/ddd/rate1m", Value: float64(10.4)},
 	}
 	if !reflect.DeepEqual(result, expected) {
 		t.Errorf("unexpected result. Got %#v, want %#v", result, expected)