forked from grafana/metrictank
-
Notifications
You must be signed in to change notification settings - Fork 7
/
chunk_by_ttl.go
65 lines (55 loc) · 1.19 KB
/
chunk_by_ttl.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
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"sort"
"github.com/gocql/gocql"
log "github.com/sirupsen/logrus"
)
type bucket struct {
key string
ttl int
}
type bucketWithCount struct {
key string
ttl int
c int
}
type byTTL []bucketWithCount
func (a byTTL) Len() int { return len(a) }
func (a byTTL) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byTTL) Less(i, j int) bool { return a[i].ttl < a[j].ttl }
// shows an overview of all keys and their ttls and closes the iter
// iter must return rows of key and ttl.
func showKeyTTL(iter *gocql.Iter, groupTTL string) {
roundTTL := 1
switch groupTTL {
case "m":
roundTTL = 60
case "h":
roundTTL = 60 * 60
case "d":
roundTTL = 60 * 60 * 24
}
var b bucket
bucketMap := make(map[bucket]int)
for iter.Scan(&b.key, &b.ttl) {
b.ttl /= roundTTL
bucketMap[b] += 1
}
var bucketList []bucketWithCount
for b, count := range bucketMap {
bucketList = append(bucketList, bucketWithCount{
b.key,
b.ttl,
count,
})
}
sort.Sort(byTTL(bucketList))
for _, b := range bucketList {
fmt.Printf("%s %d%s %d\n", b.key, b.ttl, groupTTL, b.c)
}
err := iter.Close()
if err != nil {
log.Errorf("cassandra query error. %s", err)
}
}