Skip to content

Commit

Permalink
Merge pull request #720 from nklincoln/add-prometheus-sum
Browse files Browse the repository at this point in the history
enable statistic summation on prometheus queries
  • Loading branch information
nklincoln authored Feb 3, 2020
2 parents 89375bc + 240739b commit aa99bed
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ class PrometheusQueryHelper {
return values[0];
}
}
case 'sum':
return values.reduce((x, y) => x + y);
default:
Logger.error(`Unknown stat type passed: ${statType}`);
throw new Error(`Unknown stat type passed: ${statType}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,19 @@ describe('PrometheusQueryHelper implementation', () => {
output.get('unknown').should.equal(2.8);
});

it('should retrieve the sum value from a matrix response', () => {
const response = {
data: {
resultType: 'matrix',
result: [{ values: [[111, 1], [111, 1], [111, 2], [111, 2], [111, 8]] }]
}
};
const output = PrometheusQueryHelper.extractStatisticFromRange(response, 'sum');
output.should.be.an('map');
output.size.should.equal(1);
output.get('unknown').should.equal(14);
});

it('should return `-` if passed too few results', () => {
const response = {
data: {
Expand Down

0 comments on commit aa99bed

Please sign in to comment.