@@ -3,7 +3,6 @@ package elasticsearch
3
3
import (
4
4
"context"
5
5
"encoding/json"
6
- "fmt"
7
6
"strconv"
8
7
"time"
9
8
@@ -36,8 +35,7 @@ func (e *EsExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, conte
36
35
//convert dashboard query datastructure to helper objects
37
36
esQuerys := e .convertQueries (queries )
38
37
for _ , esQuery := range esQuerys {
39
- //debug output
40
- fmt .Printf ("inspect esquery %+v\n " , esQuery )
38
+ e .log .Debug ("inspect esquery" , "query" , esQuery )
41
39
42
40
//build and execute search
43
41
searchResult := e .search (esQuery , context )
@@ -73,7 +71,7 @@ func (e *EsExecutor) buildClient(dataSourceInfo *tsdb.DataSourceInfo) *elastic.C
73
71
}
74
72
client , err := elastic .NewClient (clientOptions ... )
75
73
if err != nil {
76
- fmt . Printf ( " \n ERROR: creating elastic client \n %#v " , err )
74
+ e . log . Error ( "Creating elastic client" , "error " , err )
77
75
}
78
76
return client
79
77
}
@@ -83,14 +81,12 @@ func (e *EsExecutor) convertQueries(queries tsdb.QuerySlice) []EsQuery {
83
81
84
82
for _ , query := range queries { //TODO allow more then one query
85
83
str , _ := query .Model .EncodePretty ()
86
- //debug print query
87
- fmt .Printf ("\n Elastic query json model: \n %s" , str )
88
- e .log .Info ("Elastic query" )
84
+ e .log .Info ("Elastic query" , "query" , query .Model )
89
85
90
86
var esQuery EsQuery
91
87
jerr := json .Unmarshal (str , & esQuery )
92
88
if jerr != nil {
93
- fmt . Println ("json parser error %s" , jerr )
89
+ e . log . Error ("json parser error %s" , "error " , jerr )
94
90
} else {
95
91
esQuery .DataSource = query .DataSource
96
92
esQuerys = append (esQuerys , esQuery )
@@ -113,11 +109,9 @@ func (e *EsExecutor) search(esQuery EsQuery, context *tsdb.QueryContext) *elasti
113
109
searchService = e .buildQuery (searchService , esQuery , context )
114
110
searchService = e .buildAggregations (searchService , esQuery , context )
115
111
116
- // execute
117
112
searchResult , err := searchService .Do ()
118
113
if err != nil {
119
- // Handle error
120
- fmt .Printf ("\n ERROR: executing elastic query \n %#v\n %s" , err , err )
114
+ e .log .Error ("Executing elastic query" , "error" , err )
121
115
return nil
122
116
}
123
117
@@ -137,21 +131,21 @@ func (e *EsExecutor) processAggregation(esQuery EsQuery, index int32, aggregatio
137
131
if bAgg .AggType == "date_histogram" {
138
132
bucketAgg , found := aggregation .DateHistogram (bAgg .Id )
139
133
if found != true {
140
- fmt . Printf ("Can not find Aggregation with id %s \n " , bAgg .Id )
134
+ e . log . Info ("Can not find Aggregation with id" , "id " , bAgg .Id )
141
135
}
142
136
result := e .processDateHistogram (esQuery , bucketAgg )
143
137
results = append (results , result )
144
138
} else if bAgg .AggType == "terms" {
145
139
bucketAgg , found := aggregation .Terms (bAgg .Id )
146
140
if found != true {
147
- fmt . Printf ("Can not find Aggregation with id %s \n " , bAgg .Id )
141
+ e . log . Info ("Can not find Aggregation with id" , "id " , bAgg .Id )
148
142
}
149
143
aggResults := e .processTerms (esQuery , index , bucketAgg )
150
144
for _ , result := range aggResults {
151
145
results = append (results , result )
152
146
}
153
147
} else {
154
- fmt . Printf ("Aggregation type currently not supported: %s \n " , bAgg .AggType )
148
+ e . log . Error ("Aggregation type currently not supported" , "type " , bAgg .AggType )
155
149
}
156
150
return results
157
151
}
@@ -191,15 +185,9 @@ func (e *EsExecutor) processDateHistogram(esQuery EsQuery, bucketAgg *elastic.Ag
191
185
var values tsdb.TimeSeriesPoints
192
186
for _ , bucket := range bucketAgg .Buckets {
193
187
if mAgg .MetricType == "extended_stats" {
194
- //extended, found := bucket.Aggregations.ExtendedStats(mAgg.Id)
195
- //if mAgg.Meta.Count {
196
- //TODO
197
- //}
198
- fmt .Printf ("Aggregation type currently not supported: %s\n " , mAgg .MetricType )
188
+ e .log .Info ("Aggregation type currently not supported" , "type" , mAgg .MetricType )
199
189
} else if mAgg .MetricType == "percentiles" {
200
- //TODO
201
- //Percentiles
202
- fmt .Printf ("Aggregation type currently not supported: %s\n " , mAgg .MetricType )
190
+ e .log .Info ("Aggregation type currently not supported" , "type" , mAgg .MetricType )
203
191
} else {
204
192
//everything with json key value should work with this
205
193
derivative , found := bucket .Aggregations .Derivative (mAgg .Id ) //TODO use correct type
@@ -223,15 +211,13 @@ func (e *EsExecutor) processDateHistogram(esQuery EsQuery, bucketAgg *elastic.Ag
223
211
}
224
212
queryName = queryName + mAgg .Id //TODO we should also append the term
225
213
226
- //append time series to results
227
214
queryRes .RefId = esQuery .RefId
228
215
queryRes .Series = append (queryRes .Series , & tsdb.TimeSeries {
229
216
Name : queryName ,
230
217
Points : values ,
231
218
})
232
219
233
- //debug print
234
- fmt .Printf ("query result series %s: %+v\n " , queryRes .Series [0 ].Name , queryRes .Series [0 ].Points )
220
+ e .log .Debug ("query result series" , "name" , queryRes .Series [0 ].Name , "points" , queryRes .Series [0 ].Points )
235
221
}
236
222
}
237
223
@@ -325,7 +311,7 @@ func (e *EsExecutor) buildMetricAggregation(agg EsMetric, context *tsdb.QueryCon
325
311
//TODO change model
326
312
aggregation = elastic .NewMovAvgAggregation ().BucketsPath (agg .Field ).Window (agg .Settings .Window )
327
313
} else { //TODO add more
328
- fmt . Printf ("Aggregation type currently not supported: %s \n " , agg .MetricType )
314
+ e . log . Info ("Aggregation type currently not supported" , "type " , agg .MetricType )
329
315
}
330
316
331
317
return aggregation
@@ -347,7 +333,7 @@ func (e *EsExecutor) buildBucketAggregation(agg EsBucketAgg, context *tsdb.Query
347
333
}
348
334
aggregation = elastic .NewTermsAggregation ().Field (agg .Field ).Size (int (i ))
349
335
} else { //TODO add more
350
- fmt . Printf ("Aggregation type currently not supported: %s \n " , agg .AggType )
336
+ e . log . Info ("Aggregation type currently not supported" , "type " , agg .AggType )
351
337
}
352
338
353
339
return aggregation
@@ -362,7 +348,7 @@ func (e *EsExecutor) formatTimeRange(input string) string {
362
348
363
349
duration , err := time .ParseDuration ("-" + input )
364
350
if err != nil {
365
- fmt . Printf ("Something failed on duration parsing %s \n " , err )
351
+ e . log . Error ("Something failed on duration parsing" , "error " , err )
366
352
}
367
353
368
354
return strconv .FormatInt (time .Now ().Add (duration ).UnixNano ()/ 1000 / 1000 , 10 )
0 commit comments