datasource_plugin_wrapper.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package wrapper
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/grafana/grafana/pkg/components/null"
  6. "github.com/grafana/grafana/pkg/log"
  7. "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/tsdb"
  9. "github.com/grafana/grafana_plugin_model/go/datasource"
  10. )
  11. func NewDatasourcePluginWrapper(log log.Logger, plugin datasource.DatasourcePlugin) *DatasourcePluginWrapper {
  12. return &DatasourcePluginWrapper{DatasourcePlugin: plugin, logger: log}
  13. }
  14. type DatasourcePluginWrapper struct {
  15. datasource.DatasourcePlugin
  16. logger log.Logger
  17. }
  18. func (tw *DatasourcePluginWrapper) Query(ctx context.Context, ds *models.DataSource, query *tsdb.TsdbQuery) (*tsdb.Response, error) {
  19. jsonData, err := ds.JsonData.MarshalJSON()
  20. if err != nil {
  21. return nil, err
  22. }
  23. pbQuery := &datasource.DatasourceRequest{
  24. Datasource: &datasource.DatasourceInfo{
  25. Name: ds.Name,
  26. Type: ds.Type,
  27. Url: ds.Url,
  28. Id: ds.Id,
  29. OrgId: ds.OrgId,
  30. JsonData: string(jsonData),
  31. DecryptedSecureJsonData: ds.SecureJsonData.Decrypt(),
  32. },
  33. TimeRange: &datasource.TimeRange{
  34. FromRaw: query.TimeRange.From,
  35. ToRaw: query.TimeRange.To,
  36. ToEpochMs: query.TimeRange.GetToAsMsEpoch(),
  37. FromEpochMs: query.TimeRange.GetFromAsMsEpoch(),
  38. },
  39. Queries: []*datasource.Query{},
  40. }
  41. for _, q := range query.Queries {
  42. modelJson, _ := q.Model.MarshalJSON()
  43. pbQuery.Queries = append(pbQuery.Queries, &datasource.Query{
  44. ModelJson: string(modelJson),
  45. IntervalMs: q.IntervalMs,
  46. RefId: q.RefId,
  47. MaxDataPoints: q.MaxDataPoints,
  48. })
  49. }
  50. pbres, err := tw.DatasourcePlugin.Query(ctx, pbQuery)
  51. if err != nil {
  52. return nil, err
  53. }
  54. res := &tsdb.Response{
  55. Results: map[string]*tsdb.QueryResult{},
  56. }
  57. for _, r := range pbres.Results {
  58. res.Results[r.RefId] = &tsdb.QueryResult{
  59. RefId: r.RefId,
  60. Series: []*tsdb.TimeSeries{},
  61. }
  62. for _, s := range r.GetSeries() {
  63. points := tsdb.TimeSeriesPoints{}
  64. for _, p := range s.Points {
  65. po := tsdb.NewTimePoint(null.FloatFrom(p.Value), float64(p.Timestamp))
  66. points = append(points, po)
  67. }
  68. res.Results[r.RefId].Series = append(res.Results[r.RefId].Series, &tsdb.TimeSeries{
  69. Name: s.Name,
  70. Tags: s.Tags,
  71. Points: points,
  72. })
  73. }
  74. mappedTables, err := tw.mapTables(r)
  75. if err != nil {
  76. return nil, err
  77. }
  78. res.Results[r.RefId].Tables = mappedTables
  79. }
  80. return res, nil
  81. }
  82. func (tw *DatasourcePluginWrapper) mapTables(r *datasource.QueryResult) ([]*tsdb.Table, error) {
  83. var tables []*tsdb.Table
  84. for _, t := range r.GetTables() {
  85. mappedTable, err := tw.mapTable(t)
  86. if err != nil {
  87. return nil, err
  88. }
  89. tables = append(tables, mappedTable)
  90. }
  91. return tables, nil
  92. }
  93. func (tw *DatasourcePluginWrapper) mapTable(t *datasource.Table) (*tsdb.Table, error) {
  94. table := &tsdb.Table{}
  95. for _, c := range t.GetColumns() {
  96. table.Columns = append(table.Columns, tsdb.TableColumn{
  97. Text: c.Name,
  98. })
  99. }
  100. for _, r := range t.GetRows() {
  101. row := tsdb.RowValues{}
  102. for _, rv := range r.Values {
  103. mappedRw, err := tw.mapRowValue(rv)
  104. if err != nil {
  105. return nil, err
  106. }
  107. row = append(row, mappedRw)
  108. }
  109. table.Rows = append(table.Rows, row)
  110. }
  111. return table, nil
  112. }
  113. func (tw *DatasourcePluginWrapper) mapRowValue(rv *datasource.RowValue) (interface{}, error) {
  114. switch rv.Kind {
  115. case datasource.RowValue_TYPE_NULL:
  116. return nil, nil
  117. case datasource.RowValue_TYPE_INT64:
  118. return rv.Int64Value, nil
  119. case datasource.RowValue_TYPE_BOOL:
  120. return rv.BoolValue, nil
  121. case datasource.RowValue_TYPE_STRING:
  122. return rv.StringValue, nil
  123. case datasource.RowValue_TYPE_DOUBLE:
  124. return rv.DoubleValue, nil
  125. case datasource.RowValue_TYPE_BYTES:
  126. return rv.BytesValue, nil
  127. default:
  128. return nil, fmt.Errorf("Unsupported row value %v from plugin", rv.Kind)
  129. }
  130. }