(cherry picked from commit 6de77283c6)
Co-authored-by: Leon Sorokin <leeoniya@gmail.com>
This commit is contained in:
co-authored by
Leon Sorokin
parent
59359e512f
commit
6ec7434fde
@@ -1,6 +1,6 @@
|
|||||||
import { DataFrame, FieldType, DataQueryRequest, DataQueryResponse, MutableDataFrame } from '@grafana/data';
|
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';
|
import { PromQuery } from './types';
|
||||||
|
|
||||||
jest.mock('@grafana/runtime', () => ({
|
jest.mock('@grafana/runtime', () => ({
|
||||||
@@ -33,6 +33,43 @@ const matrixResponse = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
describe('Prometheus Result Transformer', () => {
|
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', () => {
|
describe('transformV2', () => {
|
||||||
it('results with time_series format should be enriched with preferredVisualisationType', () => {
|
it('results with time_series format should be enriched with preferredVisualisationType', () => {
|
||||||
const request = {
|
const request = {
|
||||||
|
|||||||
@@ -38,8 +38,8 @@ import {
|
|||||||
TransformOptions,
|
TransformOptions,
|
||||||
} from './types';
|
} from './types';
|
||||||
|
|
||||||
const POSITIVE_INFINITY_SAMPLE_VALUE = '+Inf';
|
// handles case-insensitive Inf, +Inf, -Inf (with optional "inity" suffix)
|
||||||
const NEGATIVE_INFINITY_SAMPLE_VALUE = '-Inf';
|
const INFINITY_SAMPLE_REGEX = /^[+-]?inf(?:inity)?$/i;
|
||||||
|
|
||||||
interface TimeAndValue {
|
interface TimeAndValue {
|
||||||
[TIME_SERIES_TIME_FIELD_NAME]: number;
|
[TIME_SERIES_TIME_FIELD_NAME]: number;
|
||||||
@@ -611,13 +611,10 @@ function sortSeriesByLabel(s1: DataFrame, s2: DataFrame): number {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseSampleValue(value: string): number {
|
/** @internal */
|
||||||
switch (value) {
|
export function parseSampleValue(value: string): number {
|
||||||
case POSITIVE_INFINITY_SAMPLE_VALUE:
|
if (INFINITY_SAMPLE_REGEX.test(value)) {
|
||||||
return Number.POSITIVE_INFINITY;
|
return value[0] === '-' ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY;
|
||||||
case NEGATIVE_INFINITY_SAMPLE_VALUE:
|
|
||||||
return Number.NEGATIVE_INFINITY;
|
|
||||||
default:
|
|
||||||
return parseFloat(value);
|
|
||||||
}
|
}
|
||||||
|
return parseFloat(value);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user