moves plugin proxy to plugin package

This commit is contained in:
bergquist
2018-01-10 12:11:57 +01:00
parent acb7d802d1
commit ebd8677607
5 changed files with 6 additions and 7 deletions
@@ -0,0 +1,93 @@
package tsdb
import (
"github.com/golang/protobuf/ptypes"
"github.com/grafana/grafana/pkg/components/null"
"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"
)
type DatasourcePluginWrapper struct {
TsdbPlugin
}
func (tw *DatasourcePluginWrapper) Query(ctx context.Context, ds *models.DataSource, query *tsdb.TsdbQuery) (*tsdb.Response, error) {
jsonData, err := ds.JsonData.MarshalJSON()
if err != nil {
return nil, err
}
now, err := ptypes.TimestampProto(query.TimeRange.Now)
if err != nil {
return nil, err
}
pbQuery := &proto.TsdbQuery{
Datasource: &proto.DatasourceInfo{
Access: string(ds.Access),
BasicAuth: ds.BasicAuth,
BasicAuthUser: ds.BasicAuthUser,
BasicAuthPassword: ds.BasicAuthPassword,
JsonData: string(jsonData),
Name: ds.Name,
Type: ds.Type,
Url: ds.Url,
Id: ds.Id,
OrgId: ds.OrgId,
},
Timerange: &proto.Timerange{
From: query.TimeRange.From,
To: query.TimeRange.To,
Now: now,
},
Queries: []*proto.Query{},
}
for _, q := range query.Queries {
modelJson, _ := q.Model.MarshalJSON()
pbQuery.Queries = append(pbQuery.Queries, &proto.Query{
ModelJson: string(modelJson),
IntervalMs: q.IntervalMs,
RefId: q.RefId,
MaxDataPoints: q.MaxDataPoints,
})
}
pbres, err := tw.TsdbPlugin.Query(ctx, pbQuery)
if err != nil {
return nil, err
}
res := &tsdb.Response{
Message: pbres.Message,
Results: map[string]*tsdb.QueryResult{},
}
for _, r := range pbres.Results {
res.Results[r.RefId] = &tsdb.QueryResult{
RefId: r.RefId,
Series: []*tsdb.TimeSeries{},
}
for _, s := range r.Series {
points := tsdb.TimeSeriesPoints{}
for _, p := range s.Points {
po := tsdb.NewTimePoint(null.FloatFrom(p.Value), float64(p.Timestamp))
points = append(points, po)
}
res.Results[r.RefId].Series = append(res.Results[r.RefId].Series, &tsdb.TimeSeries{
Name: s.Name,
Tags: s.Tags,
Points: points,
})
}
}
return res, nil
}
+22
View File
@@ -0,0 +1,22 @@
package tsdb
import (
proto "github.com/grafana/grafana/pkg/tsdb/models"
"golang.org/x/net/context"
)
type GRPCClient struct {
proto.TsdbPluginClient
}
func (m *GRPCClient) Query(ctx context.Context, req *proto.TsdbQuery) (*proto.Response, error) {
return m.TsdbPluginClient.Query(ctx, req)
}
type GRPCServer struct {
TsdbPlugin
}
func (m *GRPCServer) Query(ctx context.Context, req *proto.TsdbQuery) (*proto.Response, error) {
return m.TsdbPlugin.Query(ctx, req)
}
+27
View File
@@ -0,0 +1,27 @@
package tsdb
import (
"golang.org/x/net/context"
proto "github.com/grafana/grafana/pkg/tsdb/models"
plugin "github.com/hashicorp/go-plugin"
"google.golang.org/grpc"
)
type TsdbPlugin interface {
Query(ctx context.Context, req *proto.TsdbQuery) (*proto.Response, error)
}
type TsdbPluginImpl struct {
plugin.NetRPCUnsupportedPlugin
Plugin TsdbPlugin
}
func (p *TsdbPluginImpl) GRPCServer(s *grpc.Server) error {
proto.RegisterTsdbPluginServer(s, &GRPCServer{p.Plugin})
return nil
}
func (p *TsdbPluginImpl) GRPCClient(c *grpc.ClientConn) (interface{}, error) {
return &GRPCClient{proto.NewTsdbPluginClient(c)}, nil
}