BackendPlugin: Change QueryData response format (#23234)

* BackendPlugin: (wip) change response format
goes with https://github.com/grafana/grafana-plugin-sdk-go/pull/109

* fix error mapping in wrapper

* latest of my sdk branch

* latest of my sdk branch

* TransformWrapper fixes

* latest of sdk branch (removes extra meta)

* add metadata in wrappers

* also set error string

* sdk: v0.35.0
This commit is contained in:
Kyle Brandt
2020-04-07 10:13:14 -04:00
committed by GitHub
parent afd8ffde69
commit f6cbb26167
9 changed files with 255 additions and 179 deletions

View File

@@ -2,12 +2,13 @@ package wrapper
import (
"context"
"fmt"
"time"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/plugins/backendplugin"
"github.com/grafana/grafana-plugin-sdk-go/genproto/pluginv2"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/tsdb"
@@ -81,12 +82,24 @@ func (tw *DatasourcePluginWrapperV2) Query(ctx context.Context, ds *models.DataS
return nil, err
}
return &tsdb.Response{
Results: map[string]*tsdb.QueryResult{
"": {
Dataframes: pbRes.Frames,
Meta: simplejson.NewFromAny(pbRes.Metadata),
},
},
}, nil
tR := &tsdb.Response{
Results: make(map[string]*tsdb.QueryResult, len(pbRes.Responses)),
}
for refID, pRes := range pbRes.Responses {
qr := &tsdb.QueryResult{
RefId: refID,
Dataframes: pRes.Frames,
}
if len(pRes.JsonMeta) != 0 {
qr.Meta = simplejson.NewFromAny(pRes.JsonMeta)
}
if pRes.Error != "" {
qr.Error = fmt.Errorf(pRes.Error)
qr.ErrorString = pRes.Error
}
tR.Results[refID] = qr
}
return tR, nil
}

View File

@@ -103,14 +103,25 @@ func (tw *TransformWrapper) Transform(ctx context.Context, query *tsdb.TsdbQuery
return nil, err
}
return &tsdb.Response{
Results: map[string]*tsdb.QueryResult{
"": {
Dataframes: pbRes.Frames,
Meta: simplejson.NewFromAny(pbRes.Metadata),
},
},
}, nil
tR := &tsdb.Response{
Results: make(map[string]*tsdb.QueryResult, len(pbRes.Responses)),
}
for refID, res := range pbRes.Responses {
tRes := &tsdb.QueryResult{
RefId: refID,
Dataframes: res.Frames,
}
if len(res.JsonMeta) != 0 {
tRes.Meta = simplejson.NewFromAny(res.JsonMeta)
}
if res.Error != "" {
tRes.Error = fmt.Errorf(res.Error)
tRes.ErrorString = res.Error
}
tR.Results[refID] = tRes
}
return tR, nil
}
type transformCallback struct {
@@ -168,18 +179,16 @@ func (s *transformCallback) QueryData(ctx context.Context, req *pluginv2.QueryDa
}
// Convert tsdb results (map) to plugin-model/datasource (slice) results.
// Only error, tsdb.Series, and encoded Dataframes responses are mapped.
encodedFrames := [][]byte{}
responses := make(map[string]*pluginv2.DataResponse, len(tsdbRes.Results))
for refID, res := range tsdbRes.Results {
pRes := &pluginv2.DataResponse{}
if res.Error != nil {
// TODO add Errors property to Frame
encodedFrames = append(encodedFrames, nil)
continue
pRes.Error = res.Error.Error()
}
if res.Dataframes != nil {
encodedFrames = append(encodedFrames, res.Dataframes...)
pRes.Frames = res.Dataframes
responses[refID] = pRes
continue
}
@@ -193,8 +202,18 @@ func (s *transformCallback) QueryData(ctx context.Context, req *pluginv2.QueryDa
if err != nil {
return nil, err
}
encodedFrames = append(encodedFrames, encFrame)
pRes.Frames = append(pRes.Frames, encFrame)
}
if res.Meta != nil {
b, err := res.Meta.MarshalJSON()
if err != nil {
s.logger.Error("failed to marhsal json metadata", err)
}
pRes.JsonMeta = b
}
responses[refID] = pRes
}
return &pluginv2.QueryDataResponse{Frames: encodedFrames}, nil
return &pluginv2.QueryDataResponse{
Responses: responses,
}, nil
}