Skip to content

Commit d4c37c4

Browse files
kwapikjmichalak-fluxninja
authored andcommitted
Fix headers in Druid DS (#97)
Co-authored-by: Jakub Michalak <[email protected]>
1 parent 719e663 commit d4c37c4

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

pkg/tsdb/druid/druid.go

+30-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212
"time"
1313

14+
"github.com/Knetic/govaluate"
1415
"github.com/bitly/go-simplejson"
1516
"github.com/grafadruid/go-druid"
1617
druidquerybuilder "github.com/grafadruid/go-druid/builder"
@@ -183,7 +184,10 @@ func (ds *Service) CallResource(ctx context.Context, req *backend.CallResourceRe
183184
default:
184185
body = "Path not supported"
185186
}
186-
resp := &backend.CallResourceResponse{Status: code}
187+
resp := &backend.CallResourceResponse{
188+
Headers: map[string][]string{},
189+
Status: code,
190+
}
187191
resp.Body, err = json.Marshal(body)
188192
sender.Send(resp)
189193
return nil
@@ -440,6 +444,9 @@ func (ds *Service) prepareQuery(qry []byte, s *druidInstanceSettings) (druidquer
440444
defaultQueryContext,
441445
ds.prepareQueryContext(queryContextParameters.([]interface{})))
442446
}
447+
if g, ok := q.Builder["granularity"].(map[string]any); ok {
448+
q.Builder["granularity"] = resolveGranularity(g)
449+
}
443450
jsonQuery, err := json.Marshal(q.Builder)
444451
if err != nil {
445452
return nil, nil, err
@@ -449,6 +456,28 @@ func (ds *Service) prepareQuery(qry []byte, s *druidInstanceSettings) (druidquer
449456
return query, mergeSettings(s.defaultQuerySettings, q.Settings), err
450457
}
451458

459+
func resolveGranularity(m map[string]any) map[string]any {
460+
// granularity is optional, so return early if not set and is of wrong type
461+
if m == nil || m["type"] != "duration" {
462+
return m
463+
}
464+
expr, ok := m["duration"].(string)
465+
if !ok {
466+
return m
467+
}
468+
469+
eval, err := govaluate.NewEvaluableExpression(expr)
470+
if err != nil {
471+
return m
472+
}
473+
result, err := eval.Evaluate(nil)
474+
if err != nil {
475+
return m
476+
}
477+
m["duration"] = result
478+
return m
479+
}
480+
452481
func (ds *Service) prepareQueryContext(parameters []interface{}) map[string]interface{} {
453482
ctx := make(map[string]interface{})
454483
for _, parameter := range parameters {

0 commit comments

Comments
 (0)