/
query.go
292 lines (250 loc) · 6.24 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
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package queryv1
import (
"fmt"
"strings"
"time"
"github.com/olivere/elastic"
"github.com/erda-project/erda-infra/providers/i18n"
"github.com/erda-project/erda/modules/core/monitor/metric/query/chartmeta"
tsql "github.com/erda-project/erda/modules/core/monitor/metric/query/es-tsql"
"github.com/erda-project/erda/modules/core/monitor/metric/query/query"
)
// Queryer .
type Queryer interface {
QueryWithFormatV1(qlang, statement, format string, langCodes i18n.LanguageCodes) (*Response, error)
}
// Response .
type Response struct {
Total int64 `json:"total"`
Metrics []string `json:"metrics"`
Elapsed struct {
Search time.Duration `json:"search"`
} `json:"elapsed"`
Data interface{} `json:"data"`
Interval float64 `json:"interval"`
details string
req *Request
}
// Request .
func (r *Response) Request() *Request { return r.req }
// Unmarshal .
func (r *Response) Unmarshal(out interface{}) error {
err := Unmarshal(r.Data, out)
if err != nil {
return fmt.Errorf("fail to unmarshal: %s", err)
}
return nil
}
// Details .
func (r *Response) Details() string { return r.details }
// Request .
type Request struct {
Name string
Metrics []string
Start, End int64 // ms
TimeAlign TimeAlign
Select []*Column
Where []*query.Filter
GroupBy []*Group
OrderBy []*Order
Limit []int
Debug bool
Aggregate *Column
ExistKeys map[string]struct{}
Columns map[string]*Column
TimeKey string // Specify the time field.
OriginalTimeUnit tsql.TimeUnit // The unit of the time field.
EndOffset int64
Interval float64
Points float64
AlignEnd bool
ClusterNames []string
LegendMap map[string]*chartmeta.DataMeta // Legend name -> Legend display name
ChartType string
Trans bool
TransGroup bool
DefaultNullValue interface{}
}
// Range .
func (r *Request) Range(conv bool) (int64, int64) {
if conv && r.OriginalTimeUnit != tsql.UnsetTimeUnit {
return r.Start * int64(time.Millisecond) / int64(r.OriginalTimeUnit),
r.End * int64(time.Millisecond) / int64(r.OriginalTimeUnit)
}
return r.Start * int64(time.Millisecond), r.End * int64(time.Millisecond)
}
// InitTimestamp .
func (r *Request) InitTimestamp(start, end, timestamp, latest string) (err error) {
st, et, err := query.ParseTimeRange(start, end, timestamp, latest)
if err != nil {
return err
}
r.Start, r.End = st, et
return nil
}
// TimeAlign .
type TimeAlign string
// TimeAlign .
const (
TimeAlignUnset TimeAlign = ""
TimeAlignNone TimeAlign = "none"
TimeAlignAuto TimeAlign = "auto"
)
// Group .
type Group struct {
ID string
Property Property //
Limit int
Sort *Order
Filters []*GroupFilter
ColumnAggs map[string]bool
}
// Order .
type Order struct {
ID string
Property Property //
FuncName string //
Params []interface{} //
Sort string //
}
// Ascending .
func (o *Order) Ascending() bool { return o.Sort == "ASC" }
// Column .
type Column struct {
ID string
Property Property
FuncName string //
Params []interface{} //
Function Function
}
// Property .
type Property struct {
Name string //
Key string
Script string
}
// Normalize .
func (p *Property) Normalize(typ string) {
if !p.IsScript() {
if len(p.Key) <= 0 {
p.Key = NormalizeKey(p.Name, typ)
}
p.Name = NormalizeName(p.Name)
} else {
// 去除最外层的 括号
for strings.HasPrefix(p.Script, "(") && strings.HasSuffix(p.Script, ")") {
p.Script = p.Script[1 : len(p.Script)-1]
}
}
}
// GetExpression .
func (p *Property) GetExpression() string {
if strings.HasPrefix(p.Key, "{") && strings.HasSuffix(p.Key, "}") {
return p.Key[1 : len(p.Key)-1]
}
return ""
}
// IsScript .
func (p *Property) IsScript() bool { return len(p.Script) > 0 }
// GroupFilter .
type GroupFilter struct {
Column //
Operator string //
Value interface{} //
}
// Function .
type Function interface {
Aggregations(ctx *Context, flags ...Flag) ([]*Aggregation, error)
Handle(ctx *Context, aggs elastic.Aggregations) (interface{}, error)
SupportOrderBy() bool
SupportReduce() bool
}
// Flag .
type Flag int32
// Flag .
const (
FlagColumnFunc = Flag(0)
FlagOrderBy = Flag(1)
FlagReduce = Flag(2)
)
// IsOrderBy .
func (f *Flag) IsOrderBy() bool {
return *f == FlagOrderBy
}
// IsReduce .
func (f *Flag) IsReduce() bool {
return *f == FlagReduce
}
// Flags .
type Flags []Flag
// IsOrderBy .
func (fs Flags) IsOrderBy() bool {
for _, f := range fs {
if f.IsOrderBy() {
return true
}
}
return false
}
// IsReduce .
func (fs Flags) IsReduce() bool {
for _, f := range fs {
if f.IsReduce() {
return true
}
}
return false
}
// Aggregation .
type Aggregation struct {
ID string
Aggregation elastic.Aggregation
}
// Context .
type Context struct {
Req *Request
Source *elastic.SearchSource
Resp *elastic.SearchResult
Attributes map[string]interface{}
ChartMeta *chartmeta.ChartMeta
T i18n.Translator
Lang i18n.LanguageCodes
}
// QLParser query language parser
type QLParser interface {
Parse(statement string) (*Request, error)
}
// Parsers .
var Parsers = map[string]QLParser{}
// RegisterQueryParser .
func RegisterQueryParser(name string, parser QLParser) {
Parsers[name] = parser
}
// Formater response formater
type Formater interface {
Format(ctx *Context, param string) (interface{}, error)
}
// Formats .
var Formats = map[string]Formater{}
// RegisterResponseFormater .
func RegisterResponseFormater(name string, formater Formater) {
Formats[name] = formater
}
// ValueRange .
type ValueRange struct {
From interface{}
To interface{}
}