2019-10-24 10:15:27 -05:00
|
|
|
package wrapper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-02-19 12:17:05 -06:00
|
|
|
"time"
|
2019-10-24 10:15:27 -05:00
|
|
|
|
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/components/simplejson"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/tsdb"
|
|
|
|
)
|
|
|
|
|
2020-03-05 12:44:07 -06:00
|
|
|
func NewDatasourcePluginWrapperV2(log log.Logger, pluginId, pluginType string, plugin backendplugin.CorePlugin) *DatasourcePluginWrapperV2 {
|
|
|
|
return &DatasourcePluginWrapperV2{CorePlugin: plugin, logger: log, pluginId: pluginId, pluginType: pluginType}
|
2019-10-24 10:15:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type DatasourcePluginWrapperV2 struct {
|
2020-03-05 12:44:07 -06:00
|
|
|
backendplugin.CorePlugin
|
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-01-08 10:43:28 -06:00
|
|
|
pbQuery := &pluginv2.DataQueryRequest{
|
|
|
|
Config: &pluginv2.PluginConfig{
|
2019-10-24 10:15:27 -05:00
|
|
|
OrgId: ds.OrgId,
|
2020-02-19 12:17:05 -06:00
|
|
|
PluginId: tw.pluginId,
|
|
|
|
PluginType: tw.pluginType,
|
|
|
|
UpdatedMS: ds.Updated.UnixNano() / int64(time.Millisecond),
|
|
|
|
JsonData: jsonDataBytes,
|
|
|
|
DecryptedSecureJsonData: ds.DecryptedValues(),
|
|
|
|
DatasourceConfig: &pluginv2.DataSourceConfig{
|
|
|
|
Id: ds.Id,
|
|
|
|
Name: ds.Name,
|
|
|
|
Url: ds.Url,
|
|
|
|
Database: ds.Database,
|
|
|
|
User: ds.User,
|
|
|
|
BasicAuthEnabled: ds.BasicAuth,
|
|
|
|
BasicAuthUser: ds.BasicAuthUser,
|
|
|
|
},
|
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-03-05 12:44:07 -06:00
|
|
|
pbRes, err := tw.CorePlugin.DataQuery(ctx, pbQuery)
|
2019-10-24 10:15:27 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-08 10:43:28 -06:00
|
|
|
return &tsdb.Response{
|
|
|
|
Results: map[string]*tsdb.QueryResult{
|
|
|
|
"": {
|
|
|
|
Dataframes: pbRes.Frames,
|
|
|
|
Meta: simplejson.NewFromAny(pbRes.Metadata),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
2019-10-24 10:15:27 -05:00
|
|
|
}
|