/
query.go
419 lines (369 loc) · 9.65 KB
/
query.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package main
import (
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"log"
"math"
"sort"
"strconv"
"strings"
"time"
"github.com/Cistern/cistern/internal/query"
)
type QueryResult struct {
Summary []Event `json:"summary,omitempty"`
Series []Event `json:"series,omitempty"`
Events []Event `json:"events,omitempty"`
Query interface{} `json:"query"`
}
type ByTimestamp []Event
func (t ByTimestamp) Len() int { return len(t) }
func (t ByTimestamp) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t ByTimestamp) Less(i, j int) bool {
return t[i]["_ts"].(time.Time).Before(t[j]["_ts"].(time.Time))
}
type OrderBy struct {
columns []string
events []Event
}
func (o OrderBy) Len() int { return len(o.events) }
func (o OrderBy) Swap(i, j int) { o.events[i], o.events[j] = o.events[j], o.events[i] }
func (o OrderBy) Less(i, j int) bool {
for _, col := range o.columns {
if compareInterfaces(o.events[i][col], o.events[j][col]) >= 0 {
return false
}
}
return true
}
func (c *EventCollection) Query(desc query.Desc) (*QueryResult, error) {
c.lock.RLock()
defer c.lock.RUnlock()
if desc.TimeRange.Start.Before(minTimestamp) {
desc.TimeRange.Start = minTimestamp
}
if desc.TimeRange.End.Before(minTimestamp) {
desc.TimeRange.End = minTimestamp
}
if desc.TimeRange.Start == minTimestamp && desc.TimeRange.End == minTimestamp {
desc.TimeRange.End = fromMicrosecondTime(math.MaxInt64)
}
cur, err := c.col.NewCursor()
if err != nil {
return nil, err
}
formattedStartTs := formatTs(toMicrosecondTime(desc.TimeRange.Start))
formattedEndTs := formatTs(toMicrosecondTime(desc.TimeRange.End))
startKey := string(eventKeyPrefix) + string(formattedStartTs[:])
endKey := string(eventKeyPrefix) + string(formattedEndTs[:]) + "\xff"
summaryRows := map[string][]float64{}
summaryRowsByTime := map[int64]map[string][]float64{}
resultEvents := []Event{}
cur.Seek(startKey)
CursorLoop:
for cur.Next() {
if cur.Key() > endKey {
break
}
if (cur.Key())[0] == '_' {
continue
}
// Extract event
id := cur.Key()
val := cur.Value()
ts, keyTag, hash, err := splitCollectionID(id)
if err != nil {
log.Println(err)
return nil, err
}
if ts < toMicrosecondTime(desc.TimeRange.Start) {
continue CursorLoop
}
event := Event{}
valBytes := []byte(val)
err = json.Unmarshal(valBytes, &event)
if err != nil {
log.Println(err)
return nil, err
}
eventID := strconv.FormatInt(ts, 10) + "|" + keyTag
event["_ts"] = ts
event["_tag"] = keyTag
if len(hash) > 0 {
event["_hash"] = hash
eventID += "|" + hash
}
event["_id"] = eventID
// Apply filters
for _, filter := range desc.Filters {
if colValue, ok := event[filter.Column]; ok {
filterResult := false
switch filter.Condition {
case "eq":
filterResult = checkEquals(colValue, filter.Value)
case "neq":
filterResult = !checkEquals(colValue, filter.Value)
default:
return nil, errors.New("invalid filter condition")
}
if !filterResult {
continue CursorLoop
}
} else {
continue CursorLoop
}
}
if len(desc.GroupBy) == 0 && len(desc.Columns) == 0 && desc.PointSize <= 0 {
// No group by or aggregates
event["_ts"] = fromMicrosecondTime(ts)
resultEvents = append(resultEvents, event)
if desc.Limit > 0 && len(resultEvents) == desc.Limit {
break
}
continue
}
// Figure out the row key for grouping
rowKey := ""
if len(desc.GroupBy) > 0 {
rowKeyParts := []string{}
for _, groupCol := range desc.GroupBy {
groupColVal := event[groupCol.Name] // TODO: support aggregates on grouped columns
if groupColVal == nil {
continue CursorLoop
}
marshaledColVal, err := json.Marshal(groupColVal)
if err != nil {
continue CursorLoop
}
rowKeyParts = append(rowKeyParts, string(marshaledColVal))
}
rowKey = strings.Join(rowKeyParts, "\x00")
}
// Do the aggregations.
updateRows := func(rowKey string, rows map[string][]float64) {
rowAggregates, ok := rows[rowKey]
if !ok {
rowAggregates = make([]float64, len(desc.Columns))
for i := range rowAggregates {
rowAggregates[i] = math.NaN()
}
}
for i, columnDesc := range desc.Columns {
floatVal := 0.0
columnVal := event[columnDesc.Name]
switch columnVal.(type) {
case int:
floatVal = float64(columnVal.(int))
case float64:
floatVal = columnVal.(float64)
}
switch columnDesc.Aggregate {
case "sum":
if math.IsNaN(rowAggregates[i]) {
rowAggregates[i] = 0
}
rowAggregates[i] += floatVal
case "count":
if math.IsNaN(rowAggregates[i]) {
rowAggregates[i] = 0
}
rowAggregates[i] += 1
case "min":
if rowAggregates[i] > floatVal || math.IsNaN(rowAggregates[i]) {
rowAggregates[i] = floatVal
}
case "max":
if rowAggregates[i] < floatVal || math.IsNaN(rowAggregates[i]) {
rowAggregates[i] = floatVal
}
}
}
rows[rowKey] = rowAggregates
}
if len(desc.Columns) > 0 {
updateRows(rowKey, summaryRows)
}
if desc.PointSize > 0 {
timeGroup := ts / desc.PointSize
var rows map[string][]float64
var ok bool
if rows, ok = summaryRowsByTime[timeGroup]; !ok {
rows = map[string][]float64{}
summaryRowsByTime[timeGroup] = rows
}
updateRows(rowKey, rows)
}
} // Event cursor loop
if err = cur.Err(); err != nil {
return nil, err
}
summaryEvents := []Event{}
for rowKey, rowAggregates := range summaryRows {
event := Event{}
if len(desc.GroupBy) > 0 {
parts := strings.Split(rowKey, "\x00")
for i, part := range parts {
if desc.GroupBy[i].Name == "_ts" { // TODO: support aggregates on grouped columns
ts, _ := strconv.Atoi(part)
event["_ts"] = fromMicrosecondTime(int64(ts))
continue
}
var val interface{}
dec := json.NewDecoder(strings.NewReader(part))
dec.UseNumber()
dec.Decode(&val)
event[desc.GroupBy[i].Name] = val // TODO: support aggregates on grouped columns
}
}
for i, columnDesc := range desc.Columns {
fieldName := columnDesc.Aggregate + "(" + columnDesc.Name + ")"
event[fieldName] = rowAggregates[i]
}
rowKeyHash := md5.Sum([]byte(rowKey))
event["_group_id"] = fmt.Sprintf("%x", rowKeyHash[:8])
summaryEvents = append(summaryEvents, event)
}
if len(desc.OrderBy) != 0 {
orderByColumns := []string{}
for _, desc := range desc.OrderBy {
name := desc.Name
if desc.Aggregate != "" {
name = desc.Aggregate + "(" + name + ")"
}
orderByColumns = append(orderByColumns, name)
}
var ordering sort.Interface = OrderBy{
columns: orderByColumns,
events: summaryEvents,
}
if desc.Descending {
ordering = sort.Reverse(ordering)
}
sort.Stable(ordering)
}
if desc.Limit > 0 && len(summaryEvents) > desc.Limit {
summaryEvents = summaryEvents[:desc.Limit]
}
validGroupIDs := map[string]bool{}
for _, e := range summaryEvents {
validGroupIDs[e["_group_id"].(string)] = true
}
seriesEvents := []Event{}
if desc.PointSize > 0 {
for ts, rows := range summaryRowsByTime {
for rowKey, rowAggregates := range rows {
rowKeyHash := md5.Sum([]byte(rowKey))
groupID := fmt.Sprintf("%x", rowKeyHash[:8])
if !validGroupIDs[groupID] {
continue
}
event := Event{
"_ts": fromMicrosecondTime(ts * desc.PointSize),
}
if len(desc.GroupBy) > 0 {
parts := strings.Split(rowKey, "\x00")
for i, part := range parts {
if desc.GroupBy[i].Name == "_ts" { // TODO: support aggregates on grouped columns
continue
}
var val interface{}
dec := json.NewDecoder(strings.NewReader(part))
dec.UseNumber()
dec.Decode(&val)
event[desc.GroupBy[i].Name] = val // TODO: support aggregates on grouped columns
}
}
for i, columnDesc := range desc.Columns {
fieldName := columnDesc.Aggregate + "(" + columnDesc.Name + ")"
event[fieldName] = rowAggregates[i]
}
event["_group_id"] = groupID
seriesEvents = append(seriesEvents, event)
}
}
sort.Sort(ByTimestamp(seriesEvents))
}
return &QueryResult{Summary: summaryEvents, Series: seriesEvents, Events: resultEvents, Query: desc}, nil
}
func splitCollectionID(id string) (int64, string, string, error) {
if len(id) < 1 {
return 0, "", "", errors.New("invalid ID 2")
}
if id[0] != eventKeyPrefix {
return 0, "", "", errors.New("invalid ID prefix")
}
id = id[1:]
if len(id) < 8+2 {
return 0, "", "", errors.New("invalid ID 3")
}
formattedTs := [8]byte{
id[0],
id[1],
id[2],
id[3],
id[4],
id[5],
id[6],
id[7],
}
ts := parseTs(formattedTs)
id = id[8:]
if id[0] != '|' {
return 0, "", "", errors.New("invalid ID 4")
}
id = id[1:]
parts := strings.Split(id, "|")
if len(parts) != 2 {
return 0, "", "", errors.New("invalid ID 5")
}
return ts, parts[0], parts[1], nil
}
func checkEquals(a, b interface{}) bool {
return compareInterfaces(a, b) == 0
}
func compareInterfaces(a, b interface{}) int {
switch a.(type) {
case int:
aInt := a.(int)
if bInt, ok := b.(int); ok {
return aInt - bInt
}
case float64:
aFloat := a.(float64)
if bFloat, ok := b.(float64); ok {
if aFloat == bFloat {
return 0
} else if aFloat < bFloat {
return -1
} else {
return 1
}
}
case string:
aString := a.(string)
if bString, ok := b.(string); ok {
if aString == bString {
return 0
} else if aString < bString {
return -1
} else {
return 1
}
}
case json.Number:
aFloat, _ := strconv.ParseFloat(string(a.(json.Number)), 64)
if bNumber, ok := b.(json.Number); ok {
bFloat, _ := strconv.ParseFloat(string(bNumber), 64)
if aFloat == bFloat {
return 0
} else if aFloat < bFloat {
return -1
} else {
return 1
}
}
}
return -1
}