-
Notifications
You must be signed in to change notification settings - Fork 4
/
series.go
49 lines (44 loc) · 1.15 KB
/
series.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
package common
import (
"crypto/md5"
"fmt"
"io"
"sort"
)
type IntSeries struct {
Name string `json:"name"`
Tags map[string]string `json:"tags"`
Points []IntPoint `json:"points"`
}
type DoubleSeries struct {
Name string `json:"name"`
Tags map[string]string `json:"tags"`
Points []DoublePoint `json:"points"`
}
func NewIntSeries(name string) *IntSeries {
return &IntSeries{
Name: name,
Tags: make(map[string]string, 1),
}
}
// Hash returns one result for series have same name and tags
func (series *IntSeries) Hash() string {
// TODO: more efficient way for hashing, every time we hash, we sort it, and using []byte
// should be more efficient than string
h := md5.New()
io.WriteString(h, series.Name)
keys := make([]string, len(series.Tags))
i := 0
// NOTE: use range on map has different order of keys on every run, except you only have one key,
// thus we need to sort the keys when we calculate the hash
for k := range series.Tags {
keys[i] = k
i++
}
sort.Strings(keys)
for _, k := range keys {
io.WriteString(h, k)
io.WriteString(h, series.Tags[k])
}
return fmt.Sprintf("%x", h.Sum(nil))
}