Adds Table in backend datasource contract.

This commit is contained in:
Leonard Gram
2018-01-11 17:29:25 +01:00
parent 9e942f1a6d
commit b63c834a4b
5 changed files with 357 additions and 54 deletions
@@ -1,15 +1,23 @@
package tsdb
import (
"fmt"
"github.com/grafana/grafana/pkg/components/null"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/tsdb"
proto "github.com/grafana/grafana/pkg/tsdb/models"
"golang.org/x/net/context"
)
func NewDatasourcePluginWrapper(log log.Logger, plugin TsdbPlugin) *DatasourcePluginWrapper {
return &DatasourcePluginWrapper{TsdbPlugin: plugin, logger: log}
}
type DatasourcePluginWrapper struct {
TsdbPlugin
logger log.Logger
}
func (tw *DatasourcePluginWrapper) Query(ctx context.Context, ds *models.DataSource, query *tsdb.TsdbQuery) (*tsdb.Response, error) {
@@ -63,7 +71,7 @@ func (tw *DatasourcePluginWrapper) Query(ctx context.Context, ds *models.DataSou
Series: []*tsdb.TimeSeries{},
}
for _, s := range r.Series {
for _, s := range r.GetSeries() {
points := tsdb.TimeSeriesPoints{}
for _, p := range s.Points {
@@ -77,7 +85,66 @@ func (tw *DatasourcePluginWrapper) Query(ctx context.Context, ds *models.DataSou
Points: points,
})
}
mappedTables, err := tw.mapTables(r)
if err != nil {
return nil, err
}
res.Results[r.RefId].Tables = mappedTables
}
return res, nil
}
func (tw *DatasourcePluginWrapper) mapTables(r *proto.QueryResult) ([]*tsdb.Table, error) {
var tables []*tsdb.Table
for _, t := range r.GetTables() {
mappedTable, err := tw.mapTable(t)
if err != nil {
return nil, err
}
tables = append(tables, mappedTable)
}
return tables, nil
}
func (tw *DatasourcePluginWrapper) mapTable(t *proto.Table) (*tsdb.Table, error) {
table := &tsdb.Table{}
for _, c := range t.GetColumns() {
table.Columns = append(table.Columns, tsdb.TableColumn{
Text: c.Name,
})
}
for _, r := range t.GetRows() {
row := tsdb.RowValues{}
for _, rv := range r.Values {
mappedRw, err := tw.mapRowValue(rv)
if err != nil {
return nil, err
}
row = append(row, mappedRw)
}
table.Rows = append(table.Rows, row)
}
return table, nil
}
func (tw *DatasourcePluginWrapper) mapRowValue(rv *proto.RowValue) (interface{}, error) {
switch rv.Kind {
case proto.RowValue_TYPE_NULL:
return nil, nil
case proto.RowValue_TYPE_INT64:
return rv.Int64Value, nil
case proto.RowValue_TYPE_BOOL:
return rv.BoolValue, nil
case proto.RowValue_TYPE_STRING:
return rv.StringValue, nil
case proto.RowValue_TYPE_DOUBLE:
return rv.DoubleValue, nil
case proto.RowValue_TYPE_BYTES:
return rv.BytesValue, nil
default:
return nil, fmt.Errorf("Unsupported row value %v from plugin", rv.Kind)
}
}