wrapper.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. Id: ds.Id,
  27. OrgId: ds.OrgId,
  28. },
  29. Timerange: &proto.Timerange{
  30. From: query.TimeRange.From,
  31. To: query.TimeRange.To,
  32. Now: now,
  33. },
  34. Queries: []*proto.Query{},
  35. }
  36. for _, q := range query.Queries {
  37. modelJson, _ := q.Model.MarshalJSON()
  38. pbQuery.Queries = append(pbQuery.Queries, &proto.Query{
  39. ModelJson: string(modelJson),
  40. IntervalMs: q.IntervalMs,
  41. RefId: q.RefId,
  42. MaxDataPoints: q.MaxDataPoints,
  43. })
  44. }
  45. pbres, err := tw.TsdbPlugin.Query(ctx, pbQuery)
  46. if err != nil {
  47. return nil, err
  48. }
  49. res := &tsdb.Response{
  50. Message: pbres.Message,
  51. }
  52. for _, r := range pbres.Results {
  53. res.Results[r.RefId] = &tsdb.QueryResult{
  54. RefId: r.RefId,
  55. Series: []*tsdb.TimeSeries{},
  56. }
  57. for _, s := range r.Series {
  58. points := tsdb.TimeSeriesPoints{}
  59. for _, p := range s.Points {
  60. po := tsdb.NewTimePoint(null.FloatFrom(float64(p.Timestamp.Nanos)), p.Value)
  61. points = append(points, po)
  62. }
  63. res.Results[r.RefId].Series = append(res.Results[r.RefId].Series, &tsdb.TimeSeries{
  64. Name: s.Name,
  65. Tags: s.Tags,
  66. Points: points,
  67. })
  68. }
  69. }
  70. return res, nil
  71. }