Commit 69d63165 by bergquist

style(mqe): instance dependencies in ctor func

parent 549c3ab7
......@@ -6,6 +6,10 @@ import (
"github.com/grafana/grafana/pkg/tsdb"
)
func NewQueryParser() *MQEQueryParser {
return &MQEQueryParser{}
}
type MQEQueryParser struct{}
func (qp *MQEQueryParser) Parse(model *simplejson.Json, dsInfo *models.DataSource, queryContext *tsdb.QueryContext) (*MQEQuery, error) {
......
......@@ -23,9 +23,11 @@ import (
type MQEExecutor struct {
*models.DataSource
QueryParser *MQEQueryParser
ResponseParser *MQEResponseParser
HttpClient *http.Client
queryParser *MQEQueryParser
responseParser *MQEResponseParser
httpClient *http.Client
log log.Logger
tokenClient *TokenClient
}
func NewMQEExecutor(dsInfo *models.DataSource) (tsdb.Executor, error) {
......@@ -36,18 +38,15 @@ func NewMQEExecutor(dsInfo *models.DataSource) (tsdb.Executor, error) {
return &MQEExecutor{
DataSource: dsInfo,
QueryParser: &MQEQueryParser{},
ResponseParser: &MQEResponseParser{},
HttpClient: httpclient,
httpClient: httpclient,
log: log.New("tsdb.mqe"),
queryParser: NewQueryParser(),
responseParser: NewResponseParser(),
tokenClient: NewTokenClient(dsInfo),
}, nil
}
var (
glog log.Logger
)
func init() {
glog = log.New("tsdb.mqe")
tsdb.RegisterExecutor("mqe-datasource", NewMQEExecutor)
}
......@@ -59,14 +58,14 @@ type QueryToSend struct {
func (e *MQEExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, queryContext *tsdb.QueryContext) *tsdb.BatchResult {
result := &tsdb.BatchResult{}
availableSeries, err := NewTokenClient(e.HttpClient).GetTokenData(ctx, e.DataSource)
availableSeries, err := e.tokenClient.GetTokenData(ctx)
if err != nil {
return result.WithError(err)
}
var mqeQueries []*MQEQuery
for _, v := range queries {
q, err := e.QueryParser.Parse(v.Model, e.DataSource, queryContext)
q, err := e.queryParser.Parse(v.Model, e.DataSource, queryContext)
if err != nil {
return result.WithError(err)
}
......@@ -85,16 +84,16 @@ func (e *MQEExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, quer
queryResult := &tsdb.QueryResult{}
for _, v := range rawQueries {
glog.Info("Mqe executor", "query", v)
e.log.Info("Mqe executor", "query", v)
req, err := e.createRequest(v.RawQuery)
resp, err := ctxhttp.Do(ctx, e.HttpClient, req)
resp, err := ctxhttp.Do(ctx, e.httpClient, req)
if err != nil {
return result.WithError(err)
}
series, err := e.ResponseParser.Parse(resp, v.QueryRef)
series, err := e.responseParser.Parse(resp, v.QueryRef)
if err != nil {
return result.WithError(err)
}
......@@ -132,6 +131,6 @@ func (e *MQEExecutor) createRequest(query string) (*http.Request, error) {
req.SetBasicAuth(e.BasicAuthUser, e.BasicAuthPassword)
}
glog.Debug("Mqe request", "url", req.URL.String())
e.log.Debug("Mqe request", "url", req.URL.String())
return req, nil
}
......@@ -17,12 +17,16 @@ import (
type TokenClient struct {
tlog log.Logger
Datasource *models.DataSource
HttpClient *http.Client
}
func NewTokenClient(httpClient *http.Client) *TokenClient {
func NewTokenClient(datasource *models.DataSource) *TokenClient {
httpClient, _ := datasource.GetHttpClient()
return &TokenClient{
tlog: log.New("tsdb.mqe.tokenclient"),
Datasource: datasource,
HttpClient: httpClient,
}
}
......@@ -30,22 +34,22 @@ func NewTokenClient(httpClient *http.Client) *TokenClient {
var cache map[int64]*TokenBody = map[int64]*TokenBody{}
//Replace this stupid cache with internal cache from grafana master before merging
func (client *TokenClient) GetTokenData(ctx context.Context, datasource *models.DataSource) (*TokenBody, error) {
_, excist := cache[datasource.Id]
func (client *TokenClient) GetTokenData(ctx context.Context) (*TokenBody, error) {
_, excist := cache[client.Datasource.Id]
if !excist {
b, err := client.RequestTokenData(ctx, datasource)
b, err := client.RequestTokenData(ctx)
if err != nil {
return nil, err
}
cache[datasource.Id] = b
cache[client.Datasource.Id] = b
}
return cache[datasource.Id], nil
return cache[client.Datasource.Id], nil
}
func (client *TokenClient) RequestTokenData(ctx context.Context, datasource *models.DataSource) (*TokenBody, error) {
u, _ := url.Parse(datasource.Url)
func (client *TokenClient) RequestTokenData(ctx context.Context) (*TokenBody, error) {
u, _ := url.Parse(client.Datasource.Url)
u.Path = path.Join(u.Path, "token")
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
......
......@@ -4,8 +4,6 @@ import (
"context"
"testing"
"net/http"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
......@@ -18,9 +16,9 @@ func TestTokenClient(t *testing.T) {
Url: "",
}
client := NewTokenClient(http.DefaultClient)
client := NewTokenClient(dsInfo)
body, err := client.RequestTokenData(context.TODO(), dsInfo)
body, err := client.RequestTokenData(context.TODO())
So(err, ShouldBeNil)
//So(len(body.Functions), ShouldBeGreaterThan, 1)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment