mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
style(tsdb): extract some methods
This commit is contained in:
@@ -36,7 +36,7 @@ func init() {
|
|||||||
func (e *GraphiteExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryContext) *tsdb.BatchResult {
|
func (e *GraphiteExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryContext) *tsdb.BatchResult {
|
||||||
result := &tsdb.BatchResult{}
|
result := &tsdb.BatchResult{}
|
||||||
|
|
||||||
params := url.Values{
|
formData := url.Values{
|
||||||
"from": []string{"-" + formatTimeRange(context.TimeRange.From)},
|
"from": []string{"-" + formatTimeRange(context.TimeRange.From)},
|
||||||
"until": []string{formatTimeRange(context.TimeRange.To)},
|
"until": []string{formatTimeRange(context.TimeRange.To)},
|
||||||
"format": []string{"json"},
|
"format": []string{"json"},
|
||||||
@@ -44,52 +44,28 @@ func (e *GraphiteExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryC
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, query := range queries {
|
for _, query := range queries {
|
||||||
params["target"] = []string{query.Query}
|
formData["target"] = []string{query.Query}
|
||||||
}
|
}
|
||||||
|
|
||||||
u, _ := url.Parse(e.Url)
|
glog.Info("Graphite request body", "formdata", formData.Encode())
|
||||||
u.Path = path.Join(u.Path, "render")
|
|
||||||
glog.Info("Graphite request body", "formdata", params.Encode())
|
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(params.Encode()))
|
req, err := e.createRequest(formData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Info("Failed to create request", "error", err)
|
result.Error = err
|
||||||
result.Error = fmt.Errorf("Failed to create request. error: %v", err)
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
||||||
if e.BasicAuth {
|
|
||||||
req.SetBasicAuth(e.BasicAuthUser, e.BasicAuthPassword)
|
|
||||||
}
|
|
||||||
|
|
||||||
res, err := HttpClient.Do(req)
|
res, err := HttpClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.Error = err
|
result.Error = err
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(res.Body)
|
data, err := e.parseResponse(res)
|
||||||
defer res.Body.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.Error = err
|
result.Error = err
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
if res.StatusCode == http.StatusUnauthorized {
|
|
||||||
glog.Info("Request is Unauthorized", "status", res.Status, "body", string(body))
|
|
||||||
result.Error = fmt.Errorf("Request is Unauthorized status: %v body: %s", res.Status, string(body))
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
var data []TargetResponseDTO
|
|
||||||
err = json.Unmarshal(body, &data)
|
|
||||||
if err != nil {
|
|
||||||
glog.Info("Failed to unmarshal graphite response", "error", err, "status", res.Status, "body", string(body))
|
|
||||||
result.Error = err
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
result.QueryResults = make(map[string]*tsdb.QueryResult)
|
result.QueryResults = make(map[string]*tsdb.QueryResult)
|
||||||
queryRes := &tsdb.QueryResult{}
|
queryRes := &tsdb.QueryResult{}
|
||||||
for _, series := range data {
|
for _, series := range data {
|
||||||
@@ -103,6 +79,46 @@ func (e *GraphiteExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryC
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *GraphiteExecutor) parseResponse(res *http.Response) ([]TargetResponseDTO, error) {
|
||||||
|
body, err := ioutil.ReadAll(res.Body)
|
||||||
|
defer res.Body.Close()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if res.StatusCode == http.StatusUnauthorized {
|
||||||
|
glog.Info("Request is Unauthorized", "status", res.Status, "body", string(body))
|
||||||
|
return nil, fmt.Errorf("Request is Unauthorized status: %v body: %s", res.Status, string(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
var data []TargetResponseDTO
|
||||||
|
err = json.Unmarshal(body, &data)
|
||||||
|
if err != nil {
|
||||||
|
glog.Info("Failed to unmarshal graphite response", "error", err, "status", res.Status, "body", string(body))
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *GraphiteExecutor) createRequest(data url.Values) (*http.Request, error) {
|
||||||
|
u, _ := url.Parse(e.Url)
|
||||||
|
u.Path = path.Join(u.Path, "render")
|
||||||
|
|
||||||
|
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(data.Encode()))
|
||||||
|
if err != nil {
|
||||||
|
glog.Info("Failed to create request", "error", err)
|
||||||
|
return nil, fmt.Errorf("Failed to create request. error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
if e.BasicAuth {
|
||||||
|
req.SetBasicAuth(e.BasicAuthUser, e.BasicAuthPassword)
|
||||||
|
}
|
||||||
|
|
||||||
|
return req, err
|
||||||
|
}
|
||||||
|
|
||||||
func formatTimeRange(input string) string {
|
func formatTimeRange(input string) string {
|
||||||
if input == "now" {
|
if input == "now" {
|
||||||
return input
|
return input
|
||||||
|
|||||||
Reference in New Issue
Block a user