Commit b63c834a by Leonard Gram

Adds Table in backend datasource contract.

parent 9e942f1a
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)
}
}
package tsdb
import (
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/tsdb"
"github.com/grafana/grafana/pkg/tsdb/models"
"testing"
)
func TestMapTables(t *testing.T) {
dpw := NewDatasourcePluginWrapper(log.New("test-logger"), nil)
var qr = &proto.QueryResult{}
qr.Tables = append(qr.Tables, &proto.Table{
Columns: []*proto.TableColumn{},
Rows: nil,
})
want := []*tsdb.Table{{}}
have, err := dpw.mapTables(qr)
if err != nil {
t.Errorf("failed to map tables. error: %v", err)
}
if len(want) != len(have) {
t.Errorf("could not map all tables")
}
}
func TestMapTable(t *testing.T) {
dpw := NewDatasourcePluginWrapper(log.New("test-logger"), nil)
source := &proto.Table{
Columns: []*proto.TableColumn{{Name: "column1"}, {Name: "column2"}},
Rows: []*proto.TableRow{{
Values: []*proto.RowValue{
{
Kind: proto.RowValue_TYPE_BOOL,
BoolValue: true,
},
{
Kind: proto.RowValue_TYPE_INT64,
Int64Value: 42,
},
},
}},
}
want := &tsdb.Table{
Columns: []tsdb.TableColumn{{Text: "column1"}, {Text: "column2"}},
}
have, err := dpw.mapTable(source)
if err != nil {
t.Fatalf("failed to map table. error: %v", err)
}
for i := range have.Columns {
if want.Columns[i] != have.Columns[i] {
t.Fatalf("have column: %s, want %s", have, want)
}
}
if len(have.Rows) != 1 {
t.Fatalf("Expects one row but got %d", len(have.Rows))
}
rowValuesCount := len(have.Rows[0])
if rowValuesCount != 2 {
t.Fatalf("Expects two row values, got %d", rowValuesCount)
}
}
func TestMappingRowValue(t *testing.T) {
dpw := NewDatasourcePluginWrapper(log.New("test-logger"), nil)
boolRowValue, _ := dpw.mapRowValue(&proto.RowValue{Kind: proto.RowValue_TYPE_BOOL, BoolValue: true})
haveBool, ok := boolRowValue.(bool)
if !ok || haveBool != true {
t.Fatalf("Expected true, was %s", haveBool)
}
intRowValue, _ := dpw.mapRowValue(&proto.RowValue{Kind: proto.RowValue_TYPE_INT64, Int64Value: 42})
haveInt, ok := intRowValue.(int64)
if !ok || haveInt != 42 {
t.Fatalf("Expected %d, was %d", 42, haveInt)
}
stringRowValue, _ := dpw.mapRowValue(&proto.RowValue{Kind: proto.RowValue_TYPE_STRING, StringValue: "grafana"})
haveString, ok := stringRowValue.(string)
if !ok || haveString != "grafana" {
t.Fatalf("Expected %s, was %s", "grafana", haveString)
}
doubleRowValue, _ := dpw.mapRowValue(&proto.RowValue{Kind: proto.RowValue_TYPE_DOUBLE, DoubleValue: 1.5})
haveDouble, ok := doubleRowValue.(float64)
if !ok || haveDouble != 1.5 {
t.Fatalf("Expected %v, was %v", 1.5, haveDouble)
}
bytesRowValue, _ := dpw.mapRowValue(&proto.RowValue{Kind: proto.RowValue_TYPE_BYTES, BytesValue: []byte{66}})
haveBytes, ok := bytesRowValue.([]byte)
if !ok || len(haveBytes) != 1 || haveBytes[0] != 66 {
t.Fatalf("Expected %v, was %v", []byte{66}, haveBytes)
}
haveNil, _ := dpw.mapRowValue(&proto.RowValue{Kind: proto.RowValue_TYPE_NULL})
if haveNil != nil {
t.Fatalf("Expected %v, was %v", nil, haveNil)
}
}
......@@ -111,7 +111,7 @@ func (p *DataSourcePlugin) spawnSubProcess() error {
plugin := raw.(shared.TsdbPlugin)
tsdb.RegisterTsdbQueryEndpoint(p.Id, func(dsInfo *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
return &shared.DatasourcePluginWrapper{TsdbPlugin: plugin}, nil
return shared.NewDatasourcePluginWrapper(p.log, plugin), nil
})
return nil
......
......@@ -3,8 +3,6 @@ option go_package = "proto";
package plugins;
import "google/protobuf/any.proto";
message TsdbQuery {
TimeRange timeRange = 1;
DatasourceInfo datasource = 2;
......@@ -47,7 +45,31 @@ message TableColumn {
}
message TableRow {
repeated google.protobuf.Any values = 1;
repeated RowValue values = 1;
}
message RowValue {
enum Kind {
// Field type null.
TYPE_NULL = 0;
// Field type double.
TYPE_DOUBLE = 1;
// Field type int64.
TYPE_INT64 = 2;
// Field type bool.
TYPE_BOOL = 3;
// Field type string.
TYPE_STRING = 4;
// Field type bytes.
TYPE_BYTES = 5;
};
Kind kind = 1;
double doubleValue = 2;
int64 int64Value = 3;
bool boolValue = 4;
string stringValue = 5;
bytes bytesValue = 6;
}
message DatasourceInfo {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment