feat(mqe): send http requests

This commit is contained in:
bergquist
2016-11-16 15:28:31 +01:00
parent f1897b7e96
commit fa8213d8d5
+54 -4
View File
@@ -3,20 +3,28 @@ package mqe
import (
"context"
"net/http"
"net/url"
"path"
"strings"
"golang.org/x/net/context/ctxhttp"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/tsdb"
)
type MQEExecutor struct {
*tsdb.DataSourceInfo
QueryParser *MQEQueryParser
QueryParser *MQEQueryParser
ResponseParser *MQEResponseParser
}
func NewMQEExecutor(dsInfo *tsdb.DataSourceInfo) tsdb.Executor {
return &MQEExecutor{
DataSourceInfo: dsInfo,
QueryParser: &MQEQueryParser{},
ResponseParser: &MQEResponseParser{},
}
}
@@ -61,12 +69,54 @@ func (e *MQEExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, cont
rawQueries = append(rawQueries, queries...)
}
asdf := &tsdb.QueryResult{}
for _, v := range rawQueries {
glog.Info("Mqe executor", "query", v)
//create request from v
//send request
//parse request
req, err := e.createRequest(v)
resp, err := ctxhttp.Do(ctx, HttpClient, req)
if err != nil {
return result.WithError(err)
}
series, err := e.ResponseParser.Parse(resp)
if err != nil {
return result.WithError(err)
}
asdf.Series = append(asdf.Series, series.Series...)
}
result.QueryResults["A"] = asdf
return result
}
func (e *MQEExecutor) createRequest(query string) (*http.Request, error) {
u, _ := url.Parse(e.Url)
u.Path = path.Join(u.Path, "query")
payload := simplejson.New()
payload.Set("query", query)
jsonPayload, err := payload.MarshalJSON()
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(string(jsonPayload)))
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "Grafana")
req.Header.Set("Content-Type", "application/json")
if e.BasicAuth {
req.SetBasicAuth(e.BasicAuthUser, e.BasicAuthPassword)
}
glog.Debug("Mqe request", "url", req.URL.String())
return req, nil
}