-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket.go
56 lines (47 loc) · 1 KB
/
bucket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package snatch
import (
"strings"
"time"
)
// Type constants.
const (
Count Type = "count"
Sample Type = "sample"
Measure Type = "measure"
)
// Type represents a metric type
type Type string
// ID represents a Buckets identity.
type ID struct {
Time time.Time
Name string
Tags []string
Type Type
}
// Keys returns the timestamp and key of an ID.
func (id *ID) Keys() (int64, string) {
s := string(id.Type) + ":" + id.Name + ":" + strings.Join(id.Tags, ",")
return id.Time.Unix(), s
}
// Bucket represents a metric bucket.
type Bucket struct {
// ID is the Bucket identity.
ID *ID
// Units is the type of yhe values.
Units string
// Vals is the slice of values in the bucket.
Vals []float64
// Sum is the sum of the values.
Sum float64
}
// Append adds a metric value to the bucket.
func (b *Bucket) Append(v float64) {
b.Sum += v
b.Vals = append(b.Vals, v)
}
// Merge merges a Bucket in to the current Bucket.
func (b *Bucket) Merge(v *Bucket) {
for _, v := range v.Vals {
b.Append(v)
}
}