-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathquery.go
235 lines (189 loc) · 5.54 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
package druid
import (
"encoding/json"
"time"
)
// Aggregation Queries
const (
Timeseries = "timeseries"
TopN = "topN"
GroupBy = "groupBy"
)
// Possible Granularities
// all, none, minute, fifteen_minute, thirty_minute, hour and day.
const (
GranularityAll = "all"
GranularityNone = "none"
GranularityMinute = "minute"
GranularityFifteenMinute = "fifteen_minute"
GranularityThirtyMinute = "thirty_minute"
GranularityHour = "hour"
GranularityDay = "day"
)
// Filter Types
const (
FilterSelector = "selector"
FilterRegex = "regex"
FilterAnd = "and"
FilterOr = "or"
FilterNot = "not"
FilterJavascript = "javascript"
FilterExtraction = "extraction" //TODO
FilterSearch = "search"
FilterIn = "in"
FilterBound = "bound"
)
// Search Query Types
const (
SearchInsensitiveContains = "insensitive_contains"
SearchFragment = "fragment"
SearchContains = "contains"
)
// Aggregator Types
const (
AggregatorCount = "count"
AggregatorLongSum = "longSum"
AggregatorDoubleSum = "doubleSum"
AggregatorDoubleMin = "doubleMin"
AggregatorDoubleMax = "doubleMax"
AggregatorLongMin = "longMin"
AggregatorLongMax = "longMax"
AggregatorHyperUnique = "hyperUnique"
AggregatorCardinality = "cardinality"
)
// Post Aggregator
const (
PostAggregationTypeArithmatic = "arithmetic"
PostAggregationTypeFieldAccess = "fieldAccess"
PostAggregationTypeConstant = "constant"
)
// Post Aggregator Function Types
const (
PostAggregatorFnAdd = "+"
PostAggregatorFnSubtract = "-"
PostAggregatorFnMultiply = "*"
PostAggregatorFnDivide = "/"
PostAggregatorFnQuotient = "quotient"
)
// Post Aggregator Field Types
const (
PostAggregatorFieldFieldAccess = "fieldAccess"
PostAggregatorFieldConstant = "constant"
)
type Aggregation struct {
Type string `json:"type"`
Name string `json:"name"`
FieldName string `json:"fieldName"`
}
type PostAggregatorField struct {
Type string `json:"type"`
Name string `json:"name"`
FieldName string `json:"fieldName,omitempty"`
Value string `json:"value,omitempty"`
}
type PostAggregation struct {
Type string `json:"type"`
Name string `json:"name"`
Fn string `json:"fn"`
Fields []PostAggregatorField `json:"fields"`
Ordering string `json:"ordering,omitempty"`
}
type SearchQuery struct {
Type string `json:"type"`
Value string `json:"value"`
Values []string `json:"values"`
CaseSensitive bool `json:"caseSensitive"`
}
type LimitSpec struct {
}
type Having struct {
}
type Filter struct {
Type string `json:"type"`
Dimension string `json:"dimension,omitempty"`
Value string `json:"value,omitempty"`
Fields []Filter `json:"fields,omitempty"`
// Regex Filter
Pattern string `json:"pattern,omitempty"`
// In Filter
Values []string `json:"values,omitempty"`
// Javascript Filter
Function string `json:"function,omitempty"`
// Bound Filter
Lower string `json:"lower,omitempty"`
Upper string `json:"upper,omitempty"`
LowerStrict bool `json:"lowerStrict,omitempty"`
UpperStrict bool `json:"upperStrict,omitempty"`
AlphaNumeric bool `json:"alphaNumeric,omitempty"`
// Search Filter
Query *SearchQuery `json:"query,omitempty"`
}
type AggregationQuery struct {
QueryType string `json:"queryType"`
DataSource string `json:"dataSource"`
Dimension string `json:"dimension,omitempty"`
Dimensions []string `json:"dimensions,omitempty"`
Descending bool `json:"descending"`
Threshold int `json:"threshold,omitempty"`
Metric string `json:"metric,omitempty"`
Granularity string `json:"granularity,omitempty"`
Filter *Filter `json:"filter"`
Aggregations []Aggregation `json:"aggregations"`
PostAggregations []PostAggregation `json:"postAggregations"`
Intervals []string `json:"intervals"`
LimitSpec *LimitSpec `json:"limitSpec,omitempty"`
Having *Having `json:"having,omitempty"`
}
type TimeInterval struct {
Start time.Time
End time.Time
}
func TimeseriesQuery(dataSource string, descending bool, granuarity string) *AggregationQuery {
return &AggregationQuery{
QueryType: Timeseries,
DataSource: dataSource,
Descending: descending,
Granularity: granuarity,
}
}
func TopNQuery(dataSource string, dimension string, metric string, threshold int, granuarity string) *AggregationQuery {
return &AggregationQuery{
QueryType: TopN,
DataSource: dataSource,
Dimension: dimension,
Metric: metric,
Threshold: threshold,
Granularity: granuarity,
}
}
func GroupByQuery(dataSource string, dimensions []string, granuarity string) *AggregationQuery {
return &AggregationQuery{
QueryType: GroupBy,
DataSource: dataSource,
Dimensions: dimensions,
Granularity: granuarity,
}
}
func (q *AggregationQuery) AddInterval(interval string) {
q.Intervals = append(q.Intervals, interval)
}
func (q *AggregationQuery) SetFilters(f Filter) {
q.Filter = &f
}
func (q *AggregationQuery) AddAggregator(aggregator Aggregation) {
q.Aggregations = append(q.Aggregations, aggregator)
}
func (q *AggregationQuery) AddPostAggregator(postAggregator PostAggregation) {
q.PostAggregations = append(q.PostAggregations, postAggregator)
}
func (q *AggregationQuery) GetJSON() ([]byte, error) {
j, err := json.Marshal(q)
if err != nil {
return nil, err
}
return j, nil
}
func (q *AggregationQuery) GetJSONString() (string, error) {
j, _ := q.GetJSON()
return string(j), nil
}