Skip to content

Commit

Permalink
Make bins public
Browse files Browse the repository at this point in the history
  • Loading branch information
elv-nate committed Mar 15, 2024
1 parent 38ca96d commit 7f460eb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
11 changes: 5 additions & 6 deletions util/histogram/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,10 @@ func NewDurationHistogram(t DurationHistogramType) *DurationHistogram {
{Label: "2h-", Max: time.Hour * 10000},
}
}
return newDurationHistogramBins(bins)
return NewDurationHistogramBins(bins)
}

func DurationHistogramFromValues(t DurationHistogramType, values []*SerializedDurationBin) (*DurationHistogram, error) {
h := NewDurationHistogram(t)
func (h *DurationHistogram) LoadValues(values []*SerializedDurationBin) error {
for _, v := range values {
seen := false
for _, b := range h.bins {
Expand All @@ -102,10 +101,10 @@ func DurationHistogramFromValues(t DurationHistogramType, values []*SerializedDu
}
}
if !seen {
return nil, errors.E("DurationHistogramFromValues", "reason", "mismatched histogram types", "label", v.Label)
return errors.E("DurationHistogramFromValues", "reason", "mismatched histogram types", "label", v.Label)
}
}
return h, nil
return nil
}

type DurationBin struct {
Expand All @@ -121,7 +120,7 @@ type SerializedDurationBin struct {
DSum int64 `json:"dsum"`
}

func newDurationHistogramBins(bins []*DurationBin) *DurationHistogram {
func NewDurationHistogramBins(bins []*DurationBin) *DurationHistogram {
return &DurationHistogram{
bins: bins,
}
Expand Down
7 changes: 4 additions & 3 deletions util/histogram/histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestStandardDeviation(t *testing.T) {
{Label: "50-90", Max: 90},
{Label: "90-100", Max: 100},
}
h := newDurationHistogramBins(bins)
h := NewDurationHistogramBins(bins)

data := []time.Duration{
3, 7, 12, 13, 14, 15, 15, 15, 16, 17, 25, 30, 60, 70, 80, 85, 93,
Expand All @@ -87,7 +87,7 @@ func TestQuantileEstimation(t *testing.T) {
{Label: "30-40", Max: 40},
{Label: "40-50", Max: 50},
}
h := newDurationHistogramBins(bins)
h := NewDurationHistogramBins(bins)

// Uniformly distributed data from 0 to 50
data := []time.Duration{
Expand Down Expand Up @@ -132,7 +132,8 @@ func TestMarshalUnmarshal(t *testing.T) {

var vals []*SerializedDurationBin
json.Unmarshal(s, &vals)
h2, err := DurationHistogramFromValues(DefaultDurationHistogram, vals)
h2 := NewDurationHistogram(DefaultDurationHistogram)
err = h2.LoadValues(vals)
require.NoError(t, err)

s2, err := h2.MarshalJSON()
Expand Down

0 comments on commit 7f460eb

Please sign in to comment.