func (rp *ResponseParser) Parse(response *Response, query *Query) *tsdb.QueryResult { queryRes := tsdb.NewQueryResult() for _, result := range response.Results { queryRes.Series = append(queryRes.Series, rp.transformRows(result.Series, queryRes, query)...) } return queryRes }
func (e *GraphiteExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, context *tsdb.QueryContext) *tsdb.BatchResult { result := &tsdb.BatchResult{} formData := url.Values{ "from": []string{"-" + formatTimeRange(context.TimeRange.From)}, "until": []string{formatTimeRange(context.TimeRange.To)}, "format": []string{"json"}, "maxDataPoints": []string{"500"}, } for _, query := range queries { if fullTarget, err := query.Model.Get("targetFull").String(); err == nil { formData["target"] = []string{fullTarget} } else { formData["target"] = []string{query.Model.Get("target").MustString()} } } if setting.Env == setting.DEV { glog.Debug("Graphite request", "params", formData) } req, err := e.createRequest(formData) if err != nil { result.Error = err return result } res, err := ctxhttp.Do(ctx, HttpClient, req) if err != nil { result.Error = err return result } data, err := e.parseResponse(res) if err != nil { result.Error = err return result } result.QueryResults = make(map[string]*tsdb.QueryResult) queryRes := tsdb.NewQueryResult() for _, series := range data { queryRes.Series = append(queryRes.Series, &tsdb.TimeSeries{ Name: series.Target, Points: series.DataPoints, }) if setting.Env == setting.DEV { glog.Debug("Graphite response", "target", series.Target, "datapoints", len(series.DataPoints)) } } result.QueryResults["A"] = queryRes return result }
func (e *OpenTsdbExecutor) parseResponse(query OpenTsdbQuery, res *http.Response) (map[string]*tsdb.QueryResult, error) { queryResults := make(map[string]*tsdb.QueryResult) queryRes := tsdb.NewQueryResult() body, err := ioutil.ReadAll(res.Body) defer res.Body.Close() if err != nil { return nil, err } if res.StatusCode/100 != 2 { plog.Info("Request failed", "status", res.Status, "body", string(body)) return nil, fmt.Errorf("Request failed status: %v", res.Status) } var data []OpenTsdbResponse err = json.Unmarshal(body, &data) if err != nil { plog.Info("Failed to unmarshal opentsdb response", "error", err, "status", res.Status, "body", string(body)) return nil, err } for _, val := range data { series := tsdb.TimeSeries{ Name: val.Metric, } for timeString, value := range val.DataPoints { timestamp, err := strconv.ParseFloat(timeString, 64) if err != nil { plog.Info("Failed to unmarshal opentsdb timestamp", "timestamp", timeString) return nil, err } series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFrom(value), timestamp)) } queryRes.Series = append(queryRes.Series, &series) } queryResults["A"] = queryRes return queryResults, nil }
func parseResponse(value pmodel.Value, query *PrometheusQuery) (map[string]*tsdb.QueryResult, error) { queryResults := make(map[string]*tsdb.QueryResult) queryRes := tsdb.NewQueryResult() data, ok := value.(pmodel.Matrix) if !ok { return queryResults, fmt.Errorf("Unsupported result format: %s", value.Type().String()) } for _, v := range data { series := tsdb.TimeSeries{ Name: formatLegend(v.Metric, query), } for _, k := range v.Values { series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFrom(float64(k.Value)), float64(k.Timestamp.Unix()*1000))) } queryRes.Series = append(queryRes.Series, &series) } queryResults["A"] = queryRes return queryResults, nil }
func init() { ScenarioRegistry = make(map[string]*Scenario) logger := log.New("tsdb.testdata") logger.Debug("Initializing TestData Scenario") registerScenario(&Scenario{ Id: "random_walk", Name: "Random Walk", Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult { timeWalkerMs := context.TimeRange.GetFromAsMsEpoch() to := context.TimeRange.GetToAsMsEpoch() series := newSeriesForQuery(query) points := make(tsdb.TimeSeriesPoints, 0) walker := rand.Float64() * 100 for i := int64(0); i < 10000 && timeWalkerMs < to; i++ { points = append(points, tsdb.NewTimePoint(walker, float64(timeWalkerMs))) walker += rand.Float64() - 0.5 timeWalkerMs += query.IntervalMs } series.Points = points queryRes := tsdb.NewQueryResult() queryRes.Series = append(queryRes.Series, series) return queryRes }, }) registerScenario(&Scenario{ Id: "no_data_points", Name: "No Data Points", Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult { return tsdb.NewQueryResult() }, }) registerScenario(&Scenario{ Id: "datapoints_outside_range", Name: "Datapoints Outside Range", Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult { queryRes := tsdb.NewQueryResult() series := newSeriesForQuery(query) outsideTime := context.TimeRange.MustGetFrom().Add(-1*time.Hour).Unix() * 1000 series.Points = append(series.Points, tsdb.NewTimePoint(10, float64(outsideTime))) queryRes.Series = append(queryRes.Series, series) return queryRes }, }) registerScenario(&Scenario{ Id: "csv_metric_values", Name: "CSV Metric Values", StringInput: "1,20,90,30,5,0", Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult { queryRes := tsdb.NewQueryResult() stringInput := query.Model.Get("stringInput").MustString() values := []float64{} for _, strVal := range strings.Split(stringInput, ",") { if val, err := strconv.ParseFloat(strVal, 64); err == nil { values = append(values, val) } } if len(values) == 0 { return queryRes } series := newSeriesForQuery(query) startTime := context.TimeRange.GetFromAsMsEpoch() endTime := context.TimeRange.GetToAsMsEpoch() step := (endTime - startTime) / int64(len(values)-1) for _, val := range values { series.Points = append(series.Points, tsdb.NewTimePoint(val, float64(startTime))) startTime += step } queryRes.Series = append(queryRes.Series, series) return queryRes }, }) }