ToQueryResult builds a QueryResult proto.
(ss storage.SeriesSet, sampleLimit int)
| 130 | |
| 131 | // ToQueryResult builds a QueryResult proto. |
| 132 | func ToQueryResult(ss storage.SeriesSet, sampleLimit int) (*prompb.QueryResult, annotations.Annotations, error) { |
| 133 | numSamples := 0 |
| 134 | resp := &prompb.QueryResult{} |
| 135 | var iter chunkenc.Iterator |
| 136 | for ss.Next() { |
| 137 | series := ss.At() |
| 138 | iter = series.Iterator(iter) |
| 139 | |
| 140 | var ( |
| 141 | samples []prompb.Sample |
| 142 | histograms []prompb.Histogram |
| 143 | ) |
| 144 | |
| 145 | for valType := iter.Next(); valType != chunkenc.ValNone; valType = iter.Next() { |
| 146 | numSamples++ |
| 147 | if sampleLimit > 0 && numSamples > sampleLimit { |
| 148 | return nil, ss.Warnings(), HTTPError{ |
| 149 | msg: fmt.Sprintf("exceeded sample limit (%d)", sampleLimit), |
| 150 | status: http.StatusBadRequest, |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | switch valType { |
| 155 | case chunkenc.ValFloat: |
| 156 | ts, val := iter.At() |
| 157 | samples = append(samples, prompb.Sample{ |
| 158 | Timestamp: ts, |
| 159 | Value: val, |
| 160 | }) |
| 161 | case chunkenc.ValHistogram: |
| 162 | ts, h := iter.AtHistogram(nil) |
| 163 | histograms = append(histograms, prompb.FromIntHistogram(ts, h)) |
| 164 | case chunkenc.ValFloatHistogram: |
| 165 | ts, fh := iter.AtFloatHistogram(nil) |
| 166 | histograms = append(histograms, prompb.FromFloatHistogram(ts, fh)) |
| 167 | default: |
| 168 | return nil, ss.Warnings(), fmt.Errorf("unrecognized value type: %s", valType) |
| 169 | } |
| 170 | } |
| 171 | if err := iter.Err(); err != nil { |
| 172 | return nil, ss.Warnings(), err |
| 173 | } |
| 174 | |
| 175 | resp.Timeseries = append(resp.Timeseries, &prompb.TimeSeries{ |
| 176 | Labels: prompb.FromLabels(series.Labels(), nil), |
| 177 | Samples: samples, |
| 178 | Histograms: histograms, |
| 179 | }) |
| 180 | } |
| 181 | return resp, ss.Warnings(), ss.Err() |
| 182 | } |
| 183 | |
| 184 | // FromQueryResult unpacks and sorts a QueryResult proto. |
| 185 | func FromQueryResult(sortSeries bool, res *prompb.QueryResult) storage.SeriesSet { |
searching dependent graphs…