forked from grafana/metrictank
-
Notifications
You must be signed in to change notification settings - Fork 7
/
series.go
196 lines (173 loc) · 4.85 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"context"
"fmt"
"math"
"os"
"github.com/grafana/metrictank/api"
"github.com/grafana/metrictank/mdata/chunk"
"github.com/grafana/metrictank/store/cassandra"
"github.com/raintank/schema"
)
// printPoints prints points in the store corresponding to the given requirements
func printPoints(ctx context.Context, store *cassandra.CassandraStore, tables []cassandra.Table, metrics []Metric, fromUnix, toUnix, fix uint32) {
for _, metric := range metrics {
fmt.Println("## Metric", metric)
for _, table := range tables {
fmt.Println("### Table", table.Name)
if fix != 0 {
points := getSeries(ctx, store, table, metric.AMKey, fromUnix, toUnix, fix)
printPointsNormal(points, fromUnix, toUnix)
} else {
igens, err := store.SearchTable(ctx, metric.AMKey, table, fromUnix, toUnix)
if err != nil {
panic(err)
}
printNormal(igens, fromUnix, toUnix)
}
}
}
}
// printPointSummary prints a summarized view of the points in the store corresponding to the given requirements
func printPointSummary(ctx context.Context, store *cassandra.CassandraStore, tables []cassandra.Table, metrics []Metric, fromUnix, toUnix, fix uint32) {
for _, metric := range metrics {
fmt.Println("## Metric", metric)
for _, table := range tables {
fmt.Println("### Table", table.Name)
if fix != 0 {
points := getSeries(ctx, store, table, metric.AMKey, fromUnix, toUnix, fix)
printPointsSummary(points, fromUnix, toUnix)
} else {
igens, err := store.SearchTable(ctx, metric.AMKey, table, fromUnix, toUnix)
if err != nil {
panic(err)
}
printSummary(igens, fromUnix, toUnix)
}
}
}
}
func getSeries(ctx context.Context, store *cassandra.CassandraStore, table cassandra.Table, amkey schema.AMKey, fromUnix, toUnix, interval uint32) []schema.Point {
var points []schema.Point
itgens, err := store.SearchTable(ctx, amkey, table, fromUnix, toUnix)
if err != nil {
panic(err)
}
for i, itgen := range itgens {
iter, err := itgen.Get()
if err != nil {
fmt.Fprintf(os.Stderr, "chunk %d itergen.Get: %s", i, err)
continue
}
for iter.Next() {
ts, val := iter.Values()
if ts >= fromUnix && ts < toUnix {
points = append(points, schema.Point{Val: val, Ts: ts})
}
}
}
return api.Fix(points, fromUnix, toUnix, interval)
}
func printNormal(igens []chunk.IterGen, from, to uint32) {
fmt.Println("number of chunks:", len(igens))
for i, ig := range igens {
fmt.Printf("#### chunk %d (t0:%s, span:%d, format:%s, size:%d)\n", i, printTime(ig.T0), ig.Span(), ig.Format(), ig.Size())
iter, err := ig.Get()
if err != nil {
fmt.Fprintf(os.Stderr, "chunk %d itergen.Get: %s", i, err)
continue
}
for iter.Next() {
ts, val := iter.Values()
printRecord(ts, val, ts >= from && ts < to, math.IsNaN(val))
}
}
}
func printPointsNormal(points []schema.Point, from, to uint32) {
fmt.Println("number of points:", len(points))
for _, p := range points {
printRecord(p.Ts, p.Val, p.Ts >= from && p.Ts < to, math.IsNaN(p.Val))
}
}
func printRecord(ts uint32, val float64, in, nan bool) {
prefix := "- "
if in {
prefix = "> "
}
if nan {
fmt.Println(prefix, printTime(ts), "NAN")
} else {
fmt.Println(prefix, printTime(ts), val)
}
}
func printSummary(igens []chunk.IterGen, from, to uint32) {
var count int
first := true
var prevIn, prevNaN bool
var ts uint32
var val float64
var followup = func(count int, in, nan bool) {
fmt.Printf("... and %d more of in_range=%t nan=%t ...\n", count, in, nan)
}
for i, ig := range igens {
iter, err := ig.Get()
if err != nil {
fmt.Fprintf(os.Stderr, "chunk %d itergen.Get: %s", i, err)
continue
}
for iter.Next() {
ts, val = iter.Values()
nan := math.IsNaN(val)
in := (ts >= from && ts < to)
if first {
printRecord(ts, val, in, nan)
} else if nan == prevNaN && in == prevIn {
count++
} else {
followup(count, prevIn, prevNaN)
printRecord(ts, val, in, nan)
count = 0
}
prevNaN = nan
prevIn = in
first = false
}
}
if count > 0 {
followup(count, prevIn, prevNaN)
fmt.Println("last value was:")
printRecord(ts, val, prevIn, prevNaN)
}
}
func printPointsSummary(points []schema.Point, from, to uint32) {
var count int
first := true
var prevIn, prevNaN bool
var ts uint32
var val float64
var followup = func(count int, in, nan bool) {
fmt.Printf("... and %d more of in_range=%t nan=%t ...\n", count, in, nan)
}
for _, p := range points {
ts, val = p.Ts, p.Val
nan := math.IsNaN(val)
in := (ts >= from && ts < to)
if first {
printRecord(ts, val, in, nan)
} else if nan == prevNaN && in == prevIn {
count++
} else {
followup(count, prevIn, prevNaN)
printRecord(ts, val, in, nan)
count = 0
}
prevNaN = nan
prevIn = in
first = false
}
if count > 0 {
followup(count, prevIn, prevNaN)
fmt.Println("last value was:")
printRecord(ts, val, prevIn, prevNaN)
}
}