Prometheus: make parsing of Infinity variants case-insensitive (#48660)

This commit is contained in:
Leon Sorokin
2022-05-05 12:38:26 -05:00
committed by GitHub
parent 1a633812a7
commit 6de77283c6
2 changed files with 45 additions and 11 deletions

View File

@@ -1,6 +1,6 @@
import { DataFrame, FieldType, DataQueryRequest, DataQueryResponse, MutableDataFrame } from '@grafana/data';
import { transform, transformV2, transformDFToTable } from './result_transformer';
import { transform, transformV2, transformDFToTable, parseSampleValue } from './result_transformer';
import { PromQuery } from './types';
jest.mock('@grafana/runtime', () => ({
@@ -33,6 +33,43 @@ const matrixResponse = {
};
describe('Prometheus Result Transformer', () => {
describe('parse variants of "+Inf" and "-Inf" strings', () => {
it('+Inf', () => {
expect(parseSampleValue('+Inf')).toEqual(Number.POSITIVE_INFINITY);
});
it('Inf', () => {
expect(parseSampleValue('Inf')).toEqual(Number.POSITIVE_INFINITY);
});
it('inf', () => {
expect(parseSampleValue('inf')).toEqual(Number.POSITIVE_INFINITY);
});
it('+Infinity', () => {
expect(parseSampleValue('+Infinity')).toEqual(Number.POSITIVE_INFINITY);
});
it('+infinity', () => {
expect(parseSampleValue('+infinity')).toEqual(Number.POSITIVE_INFINITY);
});
it('infinity', () => {
expect(parseSampleValue('infinity')).toEqual(Number.POSITIVE_INFINITY);
});
it('-Inf', () => {
expect(parseSampleValue('-Inf')).toEqual(Number.NEGATIVE_INFINITY);
});
it('-inf', () => {
expect(parseSampleValue('-inf')).toEqual(Number.NEGATIVE_INFINITY);
});
it('-Infinity', () => {
expect(parseSampleValue('-Infinity')).toEqual(Number.NEGATIVE_INFINITY);
});
it('-infinity', () => {
expect(parseSampleValue('-infinity')).toEqual(Number.NEGATIVE_INFINITY);
});
});
describe('transformV2', () => {
it('results with time_series format should be enriched with preferredVisualisationType', () => {
const request = {

View File

@@ -38,8 +38,8 @@ import {
TransformOptions,
} from './types';
const POSITIVE_INFINITY_SAMPLE_VALUE = '+Inf';
const NEGATIVE_INFINITY_SAMPLE_VALUE = '-Inf';
// handles case-insensitive Inf, +Inf, -Inf (with optional "inity" suffix)
const INFINITY_SAMPLE_REGEX = /^[+-]?inf(?:inity)?$/i;
interface TimeAndValue {
[TIME_SERIES_TIME_FIELD_NAME]: number;
@@ -611,13 +611,10 @@ function sortSeriesByLabel(s1: DataFrame, s2: DataFrame): number {
return 0;
}
function parseSampleValue(value: string): number {
switch (value) {
case POSITIVE_INFINITY_SAMPLE_VALUE:
return Number.POSITIVE_INFINITY;
case NEGATIVE_INFINITY_SAMPLE_VALUE:
return Number.NEGATIVE_INFINITY;
default:
return parseFloat(value);
/** @internal */
export function parseSampleValue(value: string): number {
if (INFINITY_SAMPLE_REGEX.test(value)) {
return value[0] === '-' ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY;
}
return parseFloat(value);
}