2018-01-10 05:11:57 -06:00
|
|
|
package tsdb
|
2017-09-22 09:04:06 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2017-12-22 08:40:45 -06:00
|
|
|
type DatasourcePluginWrapper struct {
|
2017-09-22 09:04:06 -05:00
|
|
|
TsdbPlugin
|
|
|
|
}
|
|
|
|
|
2017-12-22 08:40:45 -06:00
|
|
|
func (tw *DatasourcePluginWrapper) Query(ctx context.Context, ds *models.DataSource, query *tsdb.TsdbQuery) (*tsdb.Response, error) {
|
2017-12-11 06:16:16 -06:00
|
|
|
jsonData, err := ds.JsonData.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-22 09:04:06 -05:00
|
|
|
pbQuery := &proto.TsdbQuery{
|
|
|
|
Datasource: &proto.DatasourceInfo{
|
2018-01-11 08:01:49 -06:00
|
|
|
JsonData: string(jsonData),
|
|
|
|
Name: ds.Name,
|
|
|
|
Type: ds.Type,
|
|
|
|
Url: ds.Url,
|
|
|
|
Id: ds.Id,
|
|
|
|
OrgId: ds.OrgId,
|
2017-09-22 09:04:06 -05:00
|
|
|
},
|
2018-01-11 08:01:49 -06:00
|
|
|
TimeRange: &proto.TimeRange{
|
|
|
|
FromRaw: query.TimeRange.From,
|
|
|
|
ToRaw: query.TimeRange.To,
|
|
|
|
ToEpochMs: query.TimeRange.GetToAsMsEpoch(),
|
|
|
|
FromEpochMs: query.TimeRange.GetFromAsMsEpoch(),
|
2017-09-22 09:04:06 -05:00
|
|
|
},
|
|
|
|
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{
|
2017-12-13 10:58:50 -06:00
|
|
|
Results: map[string]*tsdb.QueryResult{},
|
2017-09-22 09:04:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2017-12-20 09:03:53 -06:00
|
|
|
po := tsdb.NewTimePoint(null.FloatFrom(p.Value), float64(p.Timestamp))
|
|
|
|
points = append(points, po)
|
2017-09-22 09:04:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
res.Results[r.RefId].Series = append(res.Results[r.RefId].Series, &tsdb.TimeSeries{
|
|
|
|
Name: s.Name,
|
|
|
|
Tags: s.Tags,
|
|
|
|
Points: points,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|