Commit 12888600 by Erik Sundell Committed by Daniel Lee

Stackdriver: Break out parse response to its own func

parent 56e8e504
......@@ -108,31 +108,19 @@ func (e *StackdriverExecutor) Query(ctx context.Context, dsInfo *models.DataSour
return nil, err
}
data, err := e.parseResponse(res)
data, err := e.unmarshalResponse(res)
if err != nil {
return nil, err
}
queryRes := tsdb.NewQueryResult()
queryRes.RefId = query.RefId
for _, series := range data.TimeSeries {
points := make([]tsdb.TimePoint, 0)
for _, point := range series.Points {
points = append(points, tsdb.NewTimePoint(null.FloatFrom(point.Value.DoubleValue), float64((point.Interval.EndTime).Unix())*1000))
}
queryRes.Series = append(queryRes.Series, &tsdb.TimeSeries{
Name: series.Metric.Type,
Points: points,
})
}
queryRes, err := e.parseResponse(data, query.RefId)
result.Results[query.RefId] = queryRes
}
return result, nil
}
func (e *StackdriverExecutor) parseResponse(res *http.Response) (StackDriverResponse, error) {
func (e *StackdriverExecutor) unmarshalResponse(res *http.Response) (StackDriverResponse, error) {
body, err := ioutil.ReadAll(res.Body)
defer res.Body.Close()
if err != nil {
......@@ -147,13 +135,31 @@ func (e *StackdriverExecutor) parseResponse(res *http.Response) (StackDriverResp
var data StackDriverResponse
err = json.Unmarshal(body, &data)
if err != nil {
glog.Info("Failed to unmarshal graphite response", "error", err, "status", res.Status, "body", string(body))
glog.Info("Failed to unmarshal Stackdriver response", "error", err, "status", res.Status, "body", string(body))
return StackDriverResponse{}, err
}
return data, nil
}
func (e *StackdriverExecutor) parseResponse(data StackDriverResponse, queryRefId string) (*tsdb.QueryResult, error) {
queryRes := tsdb.NewQueryResult()
queryRes.RefId = queryRefId
for _, series := range data.TimeSeries {
points := make([]tsdb.TimePoint, 0)
for _, point := range series.Points {
points = append(points, tsdb.NewTimePoint(null.FloatFrom(point.Value.DoubleValue), float64((point.Interval.EndTime).Unix())*1000))
}
queryRes.Series = append(queryRes.Series, &tsdb.TimeSeries{
Name: series.Metric.Type,
Points: points,
})
}
return queryRes, nil
}
func (e *StackdriverExecutor) createRequest(ctx context.Context, dsInfo *models.DataSource) (*http.Request, error) {
u, _ := url.Parse(dsInfo.Url)
u.Path = path.Join(u.Path, "render")
......
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