Plugins: Transform plugin support (#20036)
currently temporary separate http api
This commit is contained in:
@@ -3,96 +3,15 @@ package wrapper
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
sdk "github.com/grafana/grafana-plugin-sdk-go"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/dataframe"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/genproto/datasource"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
sdk "github.com/grafana/grafana-plugin-sdk-go/datasource"
|
||||
"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"
|
||||
)
|
||||
|
||||
type grafanaAPI struct {
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
func (s *grafanaAPI) QueryDatasource(ctx context.Context, req *datasource.QueryDatasourceRequest) (*datasource.QueryDatasourceResponse, error) {
|
||||
if len(req.Queries) == 0 {
|
||||
return nil, fmt.Errorf("zero queries found in datasource request")
|
||||
}
|
||||
getDsInfo := &models.GetDataSourceByIdQuery{
|
||||
Id: req.DatasourceId,
|
||||
OrgId: req.OrgId,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(getDsInfo); err != nil {
|
||||
return nil, fmt.Errorf("Could not find datasource %v", err)
|
||||
}
|
||||
|
||||
// Convert plugin-model (datasource) queries to tsdb queries
|
||||
queries := make([]*tsdb.Query, len(req.Queries))
|
||||
for i, query := range req.Queries {
|
||||
sj, err := simplejson.NewJson([]byte(query.ModelJson))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
queries[i] = &tsdb.Query{
|
||||
RefId: query.RefId,
|
||||
IntervalMs: query.IntervalMs,
|
||||
MaxDataPoints: query.MaxDataPoints,
|
||||
DataSource: getDsInfo.Result,
|
||||
Model: sj,
|
||||
}
|
||||
}
|
||||
|
||||
timeRange := tsdb.NewTimeRange(req.TimeRange.FromRaw, req.TimeRange.ToRaw)
|
||||
tQ := &tsdb.TsdbQuery{
|
||||
TimeRange: timeRange,
|
||||
Queries: queries,
|
||||
}
|
||||
|
||||
// Execute the converted queries
|
||||
tsdbRes, err := tsdb.HandleRequest(ctx, getDsInfo.Result, tQ)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Convert tsdb results (map) to plugin-model/datasource (slice) results
|
||||
// Only error and Series responses mapped.
|
||||
results := make([]*datasource.QueryResult, len(tsdbRes.Results))
|
||||
resIdx := 0
|
||||
for refID, res := range tsdbRes.Results {
|
||||
qr := &datasource.QueryResult{
|
||||
RefId: refID,
|
||||
}
|
||||
if res.Error != nil {
|
||||
qr.Error = res.ErrorString
|
||||
results[resIdx] = qr
|
||||
resIdx++
|
||||
continue
|
||||
}
|
||||
|
||||
encodedFrames := make([][]byte, len(res.Series))
|
||||
for sIdx, series := range res.Series {
|
||||
frame, err := tsdb.SeriesToFrame(series)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
encodedFrames[sIdx], err = dataframe.MarshalArrow(frame)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
qr.Dataframes = encodedFrames
|
||||
results[resIdx] = qr
|
||||
|
||||
resIdx++
|
||||
}
|
||||
return &datasource.QueryDatasourceResponse{Results: results}, nil
|
||||
}
|
||||
|
||||
func NewDatasourcePluginWrapperV2(log log.Logger, plugin sdk.DatasourcePlugin) *DatasourcePluginWrapperV2 {
|
||||
return &DatasourcePluginWrapperV2{DatasourcePlugin: plugin, logger: log}
|
||||
}
|
||||
@@ -108,8 +27,8 @@ func (tw *DatasourcePluginWrapperV2) Query(ctx context.Context, ds *models.DataS
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pbQuery := &datasource.DatasourceRequest{
|
||||
Datasource: &datasource.DatasourceInfo{
|
||||
pbQuery := &pluginv2.DatasourceRequest{
|
||||
Datasource: &pluginv2.DatasourceInfo{
|
||||
Name: ds.Name,
|
||||
Type: ds.Type,
|
||||
Url: ds.Url,
|
||||
@@ -118,13 +37,13 @@ func (tw *DatasourcePluginWrapperV2) Query(ctx context.Context, ds *models.DataS
|
||||
JsonData: string(jsonData),
|
||||
DecryptedSecureJsonData: ds.SecureJsonData.Decrypt(),
|
||||
},
|
||||
TimeRange: &datasource.TimeRange{
|
||||
TimeRange: &pluginv2.TimeRange{
|
||||
FromRaw: query.TimeRange.From,
|
||||
ToRaw: query.TimeRange.To,
|
||||
ToEpochMs: query.TimeRange.GetToAsMsEpoch(),
|
||||
FromEpochMs: query.TimeRange.GetFromAsMsEpoch(),
|
||||
},
|
||||
Queries: []*datasource.Query{},
|
||||
Queries: []*pluginv2.DatasourceQuery{},
|
||||
}
|
||||
|
||||
for _, q := range query.Queries {
|
||||
@@ -132,7 +51,7 @@ func (tw *DatasourcePluginWrapperV2) Query(ctx context.Context, ds *models.DataS
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pbQuery.Queries = append(pbQuery.Queries, &datasource.Query{
|
||||
pbQuery.Queries = append(pbQuery.Queries, &pluginv2.DatasourceQuery{
|
||||
ModelJson: string(modelJSON),
|
||||
IntervalMs: q.IntervalMs,
|
||||
RefId: q.RefId,
|
||||
@@ -140,7 +59,7 @@ func (tw *DatasourcePluginWrapperV2) Query(ctx context.Context, ds *models.DataS
|
||||
})
|
||||
}
|
||||
|
||||
pbres, err := tw.DatasourcePlugin.Query(ctx, pbQuery, &grafanaAPI{logger: tw.logger})
|
||||
pbres, err := tw.DatasourcePlugin.Query(ctx, pbQuery)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user