func parseResponse(value pmodel.Value, query *PrometheusQuery) (map[string]*tsdb.QueryResult, error) { queryResults := make(map[string]*tsdb.QueryResult) queryRes := &tsdb.QueryResult{} data, ok := value.(pmodel.Matrix) if !ok { return queryResults, fmt.Errorf("Unsupported result format: %s", value.Type().String()) } for _, v := range data { var points [][2]*float64 for _, k := range v.Values { timestamp := float64(k.Timestamp) val := float64(k.Value) points = append(points, [2]*float64{&val, ×tamp}) } queryRes.Series = append(queryRes.Series, &tsdb.TimeSeries{ Name: formatLegend(v.Metric, query), Points: points, }) } queryResults["A"] = queryRes return queryResults, nil }
// RespondJSON converts the given data value to JSON and writes it to w. func respondJSON(w io.Writer, val model.Value) error { data := struct { Type string `json:"type"` Value interface{} `json:"value"` Version int `json:"version"` }{ Type: val.Type().String(), Value: val, Version: jsonFormatVersion, } // TODO(fabxc): Adding MarshalJSON to promql.Values might be a good idea. if sc, ok := val.(*model.Scalar); ok { data.Value = sc.Value } enc := json.NewEncoder(w) return enc.Encode(data) }
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 }