2019-10-24 10:15:27 -05:00
|
|
|
package wrapper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-04-07 09:13:14 -05:00
|
|
|
"fmt"
|
2020-02-19 12:17:05 -06:00
|
|
|
"time"
|
2019-10-24 10:15:27 -05:00
|
|
|
|
2020-04-07 09:13:14 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2020-01-31 04:15:50 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
|
|
|
|
2019-10-29 11:22:31 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/genproto/pluginv2"
|
2019-10-24 10:15:27 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/tsdb"
|
|
|
|
)
|
|
|
|
|
2020-03-10 06:59:03 -05:00
|
|
|
func NewDatasourcePluginWrapperV2(log log.Logger, pluginId, pluginType string, plugin backendplugin.DataPlugin) *DatasourcePluginWrapperV2 {
|
|
|
|
return &DatasourcePluginWrapperV2{DataPlugin: plugin, logger: log, pluginId: pluginId, pluginType: pluginType}
|
2019-10-24 10:15:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type DatasourcePluginWrapperV2 struct {
|
2020-03-10 06:59:03 -05:00
|
|
|
backendplugin.DataPlugin
|
2020-02-19 12:17:05 -06:00
|
|
|
logger log.Logger
|
|
|
|
pluginId string
|
|
|
|
pluginType string
|
2019-10-24 10:15:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tw *DatasourcePluginWrapperV2) Query(ctx context.Context, ds *models.DataSource, query *tsdb.TsdbQuery) (*tsdb.Response, error) {
|
2020-02-19 12:17:05 -06:00
|
|
|
jsonDataBytes, err := ds.JsonData.MarshalJSON()
|
2019-10-24 10:15:27 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-10 06:59:03 -05:00
|
|
|
pbQuery := &pluginv2.QueryDataRequest{
|
2020-01-08 10:43:28 -06:00
|
|
|
Config: &pluginv2.PluginConfig{
|
2020-03-10 09:18:27 -05:00
|
|
|
OrgId: ds.OrgId,
|
|
|
|
PluginId: tw.pluginId,
|
|
|
|
LastUpdatedMS: ds.Updated.UnixNano() / int64(time.Millisecond),
|
2020-02-19 12:17:05 -06:00
|
|
|
DatasourceConfig: &pluginv2.DataSourceConfig{
|
2020-03-10 09:18:27 -05:00
|
|
|
Id: ds.Id,
|
|
|
|
Name: ds.Name,
|
|
|
|
Url: ds.Url,
|
|
|
|
Database: ds.Database,
|
|
|
|
User: ds.User,
|
|
|
|
BasicAuthEnabled: ds.BasicAuth,
|
|
|
|
BasicAuthUser: ds.BasicAuthUser,
|
|
|
|
JsonData: jsonDataBytes,
|
|
|
|
DecryptedSecureJsonData: ds.DecryptedValues(),
|
2020-02-19 12:17:05 -06:00
|
|
|
},
|
2019-10-24 10:15:27 -05:00
|
|
|
},
|
2020-01-08 10:43:28 -06:00
|
|
|
Queries: []*pluginv2.DataQuery{},
|
2019-10-24 10:15:27 -05:00
|
|
|
}
|
|
|
|
|
2020-03-06 07:37:36 -06:00
|
|
|
if query.User != nil {
|
|
|
|
pbQuery.User = &pluginv2.User{
|
|
|
|
Name: query.User.Name,
|
|
|
|
Login: query.User.Login,
|
|
|
|
Email: query.User.Email,
|
|
|
|
Role: string(query.User.OrgRole),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 10:15:27 -05:00
|
|
|
for _, q := range query.Queries {
|
|
|
|
modelJSON, err := q.Model.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-08 10:43:28 -06:00
|
|
|
pbQuery.Queries = append(pbQuery.Queries, &pluginv2.DataQuery{
|
|
|
|
Json: modelJSON,
|
|
|
|
IntervalMS: q.IntervalMs,
|
2019-10-24 10:15:27 -05:00
|
|
|
RefId: q.RefId,
|
|
|
|
MaxDataPoints: q.MaxDataPoints,
|
2020-01-08 10:43:28 -06:00
|
|
|
TimeRange: &pluginv2.TimeRange{
|
|
|
|
ToEpochMS: query.TimeRange.GetToAsMsEpoch(),
|
|
|
|
FromEpochMS: query.TimeRange.GetFromAsMsEpoch(),
|
|
|
|
},
|
2019-10-24 10:15:27 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-09 07:47:46 -05:00
|
|
|
var pbRes *pluginv2.QueryDataResponse
|
|
|
|
err = backendplugin.InstrumentPluginRequest(ds.Type, "dataquery", func() error {
|
|
|
|
var err error
|
|
|
|
pbRes, err = tw.DataPlugin.QueryData(ctx, pbQuery)
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
2019-10-24 10:15:27 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-07 09:13:14 -05:00
|
|
|
tR := &tsdb.Response{
|
|
|
|
Results: make(map[string]*tsdb.QueryResult, len(pbRes.Responses)),
|
|
|
|
}
|
|
|
|
|
|
|
|
for refID, pRes := range pbRes.Responses {
|
|
|
|
qr := &tsdb.QueryResult{
|
|
|
|
RefId: refID,
|
|
|
|
Dataframes: pRes.Frames,
|
|
|
|
}
|
|
|
|
if len(pRes.JsonMeta) != 0 {
|
|
|
|
qr.Meta = simplejson.NewFromAny(pRes.JsonMeta)
|
|
|
|
}
|
|
|
|
if pRes.Error != "" {
|
|
|
|
qr.Error = fmt.Errorf(pRes.Error)
|
|
|
|
qr.ErrorString = pRes.Error
|
|
|
|
}
|
|
|
|
tR.Results[refID] = qr
|
|
|
|
}
|
|
|
|
|
|
|
|
return tR, nil
|
2019-10-24 10:15:27 -05:00
|
|
|
}
|