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