Skip to content

Commit

Permalink
Fix cumulative histogram generation and test.
Browse files Browse the repository at this point in the history
Issue #214
  • Loading branch information
jaqx0r committed Mar 23, 2019
1 parent 041e773 commit 4f35296
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 5 additions & 0 deletions internal/metrics/datum/buckets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package datum_test

import (
"math"
"testing"
"testing/quick"
"time"
Expand Down Expand Up @@ -36,4 +37,8 @@ func TestMakeBucket(t *testing.T) {
if r := datum.GetBucketsCount(b); r != 1 {
t.Errorf("count not 1, got %v", r)
}
bs := datum.GetBucketsByMax(b)
if r := datum.GetBucketsCount(b); r != bs[math.Inf(+1)] {
t.Errorf("Inf bucket des not equal total observation count: %v vs %v", r, bs[math.Inf(+1)])
}
}
7 changes: 5 additions & 2 deletions internal/metrics/datum/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,16 @@ func GetBucketsSum(d Datum) float64 {
}
}

// GetBucketsByMax returns a map of bucket observations by their upper bonds, or panics if d is not a BucketsDatum
// GetBucketsByMax returns a map of cumulative bucket observations by their
// upper bonds, or panics if d is not a BucketsDatum.
func GetBucketsByMax(d Datum) map[float64]uint64 {
switch d := d.(type) {
case *BucketsDatum:
buckets := make(map[float64]uint64)
cum := uint64(0)
for r, c := range d.Buckets() {
buckets[r.Max] = c
cum += c
buckets[r.Max] = cum
}
return buckets
default:
Expand Down

0 comments on commit 4f35296

Please sign in to comment.