@@ -12,6 +12,7 @@ import (
12
12
13
13
"github.com/grafana/grafana-plugin-sdk-go/backend"
14
14
"github.com/grafana/grafana-plugin-sdk-go/data"
15
+ "golang.org/x/exp/slices"
15
16
16
17
es "github.com/quickwit-oss/quickwit-datasource/pkg/quickwit/client"
17
18
"github.com/quickwit-oss/quickwit-datasource/pkg/quickwit/simplejson"
@@ -246,7 +247,7 @@ func processDocsToDataFrameFields(docs []map[string]interface{}, propNames []str
246
247
if propName == configuredFields .TimeField {
247
248
timeVector := make ([]* time.Time , size )
248
249
for i , doc := range docs {
249
- timeValue , err := ParseToTime (doc ["sort" ].([] any )[ 0 ] )
250
+ timeValue , err := ParseToTime (doc [configuredFields . TimeField ], configuredFields . TimeOutputFormat )
250
251
if err != nil {
251
252
continue
252
253
}
@@ -293,13 +294,39 @@ func processDocsToDataFrameFields(docs []map[string]interface{}, propNames []str
293
294
// Parses a value into Time given a timeOutputFormat. The conversion
294
295
// only works with float64 as this is what we get when parsing a response.
295
296
// TODO: understand why we get a float64?
296
- func ParseToTime (value interface {}) (time.Time , error ) {
297
- typed_value , ok := value .(float64 )
298
- if ! ok {
299
- return time.Time {}, errors .New ("parse time only accepts float64 with timestamp based format" )
297
+ func ParseToTime (value interface {}, timeOutputFormat string ) (time.Time , error ) {
298
+
299
+ if timeOutputFormat == Iso8601 || timeOutputFormat == Rfc3339 {
300
+ value_string := value .(string )
301
+ timeValue , err := time .Parse (time .RFC3339 , value_string )
302
+ if err != nil {
303
+ return time.Time {}, err
304
+ }
305
+ return timeValue , nil
306
+ } else if timeOutputFormat == Rfc2822 {
307
+ value_string := value .(string )
308
+ timeValue , err := time .Parse (time .RFC822Z , value_string )
309
+ if err != nil {
310
+ return time.Time {}, err
311
+ }
312
+ return timeValue , nil
313
+ } else if slices .Contains ([]string {TimestampSecs , TimestampMillis , TimestampMicros , TimestampNanos }, timeOutputFormat ) {
314
+ typed_value , ok := value .(float64 )
315
+ if ! ok {
316
+ return time.Time {}, errors .New ("parse time only accepts float64 with timestamp based format" )
317
+ }
318
+ int64_value := int64 (typed_value )
319
+ if timeOutputFormat == TimestampSecs {
320
+ return time .Unix (int64_value , 0 ), nil
321
+ } else if timeOutputFormat == TimestampMillis {
322
+ return time .Unix (0 , int64_value * 1_000_000 ), nil
323
+ } else if timeOutputFormat == TimestampMicros {
324
+ return time .Unix (0 , int64_value * 1_000 ), nil
325
+ } else if timeOutputFormat == TimestampNanos {
326
+ return time .Unix (0 , int64_value ), nil
327
+ }
300
328
}
301
- int64_value := int64 (typed_value )
302
- return time .Unix (0 , int64_value ), nil
329
+ return time.Time {}, fmt .Errorf ("timeOutputFormat not supported yet %s" , timeOutputFormat )
303
330
}
304
331
305
332
func processBuckets (aggs map [string ]interface {}, target * Query ,
0 commit comments