forked from shunfei/godruid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_aggregations.go
95 lines (85 loc) · 2.48 KB
/
post_aggregations.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
package godruid
import (
"encoding/json"
)
type PostAggregation struct {
Type string `json:"type"`
Name string `json:"name,omitempty"`
Value interface{} `json:"value,omitempty"`
Fn string `json:"fn,omitempty"`
Fields []PostAggregation `json:"fields,omitempty"`
FieldName string `json:"fieldName,omitempty"`
FieldNames []string `json:"fieldNames,omitempty"`
Function string `json:"function,omitempty"`
}
// The agg reference.
type AggRefer struct {
Name string
Refer string // The refer of Name, empty means Name has no refer.
}
// Return the aggregations or post aggregations which this post aggregation used.
// It could be helpful while automatically filling the aggregations or post aggregations base on this.
func (pa PostAggregation) GetReferAggs(parentName ...string) (refers []AggRefer) {
switch pa.Type {
case "arithmetic":
if len(parentName) != 0 {
refers = append(refers, AggRefer{parentName[0], pa.Name})
} else {
refers = append(refers, AggRefer{pa.Name, ""})
}
for _, spa := range pa.Fields {
refers = append(refers, spa.GetReferAggs(pa.Name)...)
}
case "fieldAccess":
refers = append(refers, AggRefer{parentName[0], pa.FieldName})
case "constant":
// no need refers.
case "javascript":
for _, f := range pa.FieldNames {
refers = append(refers, AggRefer{pa.Name, f})
}
case "hyperUniqueCardinality":
refers = append(refers, AggRefer{parentName[0], pa.FieldName})
}
return
}
func PostAggRawJson(rawJson string) PostAggregation {
pa := &PostAggregation{}
json.Unmarshal([]byte(rawJson), pa)
return *pa
}
func PostAggArithmetic(name, fn string, fields []PostAggregation) PostAggregation {
return PostAggregation{
Type: "arithmetic",
Name: name,
Fn: fn,
Fields: fields,
}
}
func PostAggFieldAccessor(fieldName string) PostAggregation {
return PostAggregation{
Type: "fieldAccess",
FieldName: fieldName,
}
}
func PostAggConstant(name string, value interface{}) PostAggregation {
return PostAggregation{
Type: "constant",
Name: name,
Value: value,
}
}
func PostAggJavaScript(name, function string, fieldNames []string) PostAggregation {
return PostAggregation{
Type: "javascript",
Name: name,
FieldNames: fieldNames,
Function: function,
}
}
func PostAggFieldHyperUnique(fieldName string) PostAggregation {
return PostAggregation{
Type: "hyperUniqueCardinality",
FieldName: fieldName,
}
}