style(tsdb): extract some methods

This commit is contained in:
bergquist 2016-09-01 13:03:15 +02:00
parent d73547c0dc
commit 86aea89214

View File

@ -36,7 +36,7 @@ func init() {
func (e *GraphiteExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryContext) *tsdb.BatchResult {
result := &tsdb.BatchResult{}
params := url.Values{
formData := url.Values{
"from": []string{"-" + formatTimeRange(context.TimeRange.From)},
"until": []string{formatTimeRange(context.TimeRange.To)},
"format": []string{"json"},
@ -44,52 +44,28 @@ func (e *GraphiteExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryC
}
for _, query := range queries {
params["target"] = []string{query.Query}
formData["target"] = []string{query.Query}
}
u, _ := url.Parse(e.Url)
u.Path = path.Join(u.Path, "render")
glog.Info("Graphite request body", "formdata", params.Encode())
glog.Info("Graphite request body", "formdata", formData.Encode())
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(params.Encode()))
req, err := e.createRequest(formData)
if err != nil {
glog.Info("Failed to create request", "error", err)
result.Error = fmt.Errorf("Failed to create request. error: %v", err)
result.Error = err
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)
if err != nil {
result.Error = err
return result
}
body, err := ioutil.ReadAll(res.Body)
defer res.Body.Close()
data, err := e.parseResponse(res)
if err != nil {
result.Error = err
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)
queryRes := &tsdb.QueryResult{}
for _, series := range data {
@ -103,6 +79,46 @@ func (e *GraphiteExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryC
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 {
if input == "now" {
return input