wrapper.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package proxy
  2. import (
  3. "github.com/golang/protobuf/ptypes"
  4. "github.com/grafana/grafana/pkg/components/null"
  5. "github.com/grafana/grafana/pkg/models"
  6. "github.com/grafana/grafana/pkg/tsdb"
  7. proto "github.com/grafana/grafana/pkg/tsdb/models"
  8. "golang.org/x/net/context"
  9. )
  10. type TsdbWrapper struct {
  11. TsdbPlugin
  12. }
  13. func (tw *TsdbWrapper) Query(ctx context.Context, ds *models.DataSource, query *tsdb.TsdbQuery) (*tsdb.Response, error) {
  14. jsonData, _ := ds.JsonData.MarshalJSON()
  15. now, _ := ptypes.TimestampProto(query.TimeRange.Now)
  16. pbQuery := &proto.TsdbQuery{
  17. Datasource: &proto.DatasourceInfo{
  18. Access: string(ds.Access),
  19. BasicAuth: ds.BasicAuth,
  20. BasicAuthUser: ds.BasicAuthUser,
  21. BasicAuthPassword: ds.BasicAuthPassword,
  22. JsonData: string(jsonData),
  23. Name: ds.Name,
  24. Type: ds.Type,
  25. Url: ds.Url,
  26. },
  27. Timerange: &proto.Timerange{
  28. From: query.TimeRange.From,
  29. To: query.TimeRange.To,
  30. Now: now,
  31. },
  32. Queries: []*proto.Query{},
  33. }
  34. for _, q := range query.Queries {
  35. modelJson, _ := q.Model.MarshalJSON()
  36. pbQuery.Queries = append(pbQuery.Queries, &proto.Query{
  37. ModelJson: string(modelJson),
  38. IntervalMs: q.IntervalMs,
  39. RefId: q.RefId,
  40. MaxDataPoints: q.MaxDataPoints,
  41. })
  42. }
  43. pbres, err := tw.TsdbPlugin.Query(ctx, pbQuery)
  44. if err != nil {
  45. return nil, err
  46. }
  47. res := &tsdb.Response{
  48. Message: pbres.Message,
  49. }
  50. for _, r := range pbres.Results {
  51. res.Results[r.RefId] = &tsdb.QueryResult{
  52. RefId: r.RefId,
  53. Series: []*tsdb.TimeSeries{},
  54. }
  55. for _, s := range r.Series {
  56. points := tsdb.TimeSeriesPoints{}
  57. for _, p := range s.Points {
  58. po := tsdb.NewTimePoint(null.FloatFrom(float64(p.Timestamp.Nanos)), p.Value)
  59. points = append(points, po)
  60. }
  61. res.Results[r.RefId].Series = append(res.Results[r.RefId].Series, &tsdb.TimeSeries{
  62. Name: s.Name,
  63. Tags: s.Tags,
  64. Points: points,
  65. })
  66. }
  67. }
  68. return res, nil
  69. }