DisplayProcessor: Handle reverse-ordered data when auto-showing millis (#54923)

pull/54935/head
Leon Sorokin 3 years ago committed by GitHub
parent eb90d0c4b5
commit 085db83cd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 83
      packages/grafana-data/src/field/displayProcessor.test.ts
  2. 2
      packages/grafana-data/src/field/displayProcessor.ts

@ -3,6 +3,7 @@ import { createTheme } from '../themes';
import { FieldConfig, FieldType, ThresholdsMode } from '../types'; import { FieldConfig, FieldType, ThresholdsMode } from '../types';
import { DisplayProcessor, DisplayValue } from '../types/displayValue'; import { DisplayProcessor, DisplayValue } from '../types/displayValue';
import { MappingType, ValueMapping } from '../types/valueMapping'; import { MappingType, ValueMapping } from '../types/valueMapping';
import { ArrayVector } from '../vector';
import { getDisplayProcessor, getRawDisplayProcessor } from './displayProcessor'; import { getDisplayProcessor, getRawDisplayProcessor } from './displayProcessor';
@ -339,6 +340,35 @@ describe('Format value', () => {
expect(disp.text).toEqual('1.19'); expect(disp.text).toEqual('1.19');
expect(disp.suffix).toEqual(' GiB'); expect(disp.suffix).toEqual(' GiB');
}); });
describe('number formatting for string values', () => {
it('should preserve string unchanged if unit is string', () => {
const processor = getDisplayProcessorFromConfig({ unit: 'string' }, FieldType.string);
expect(processor('22.1122334455').text).toEqual('22.1122334455');
});
it('should preserve string unchanged if no unit is specified', () => {
const processor = getDisplayProcessorFromConfig({}, FieldType.string);
expect(processor('22.1122334455').text).toEqual('22.1122334455');
// Support empty/missing strings
expect(processor(undefined).text).toEqual('');
expect(processor(null).text).toEqual('');
expect(processor('').text).toEqual('');
});
it('should format string as number if unit is `none`', () => {
const processor = getDisplayProcessorFromConfig({ unit: 'none' }, FieldType.string);
expect(processor('0x10').text).toEqual('16');
});
it('should not parse a 64 bit number when the data type is string', () => {
const value = '2882377905688543293';
const instance = getDisplayProcessorFromConfig({}, FieldType.string);
const disp = instance(value);
expect(disp.text).toEqual(value);
});
});
}); });
describe('Date display options', () => { describe('Date display options', () => {
@ -455,33 +485,48 @@ describe('Date display options', () => {
expect(processor('2020-12-01T08:48:43.783337').text).toEqual('2020-12-01 09:48:43'); expect(processor('2020-12-01T08:48:43.783337').text).toEqual('2020-12-01 09:48:43');
}); });
describe('number formatting for string values', () => { it('should include milliseconds when value range is < 60s', () => {
it('should preserve string unchanged if unit is string', () => { const processor = getDisplayProcessor({
const processor = getDisplayProcessorFromConfig({ unit: 'string' }, FieldType.string); timeZone: 'utc',
expect(processor('22.1122334455').text).toEqual('22.1122334455'); field: {
type: FieldType.time,
config: {},
values: new ArrayVector([Date.parse('2020-08-01T08:48:43.783337Z'), Date.parse('2020-08-01T08:49:15.123456Z')]),
},
theme: createTheme(),
}); });
it('should preserve string unchanged if no unit is specified', () => { expect(processor('2020-08-01T08:48:43.783337Z').text).toEqual('2020-08-01 08:48:43.783');
const processor = getDisplayProcessorFromConfig({}, FieldType.string); });
expect(processor('22.1122334455').text).toEqual('22.1122334455');
// Support empty/missing strings it('should not include milliseconds when value range is >= 60s (reversed)', () => {
expect(processor(undefined).text).toEqual(''); const processor = getDisplayProcessor({
expect(processor(null).text).toEqual(''); timeZone: 'utc',
expect(processor('').text).toEqual(''); field: {
type: FieldType.time,
config: {},
values: new ArrayVector([Date.parse('2020-08-01T08:49:15.123456Z'), Date.parse('2020-08-01T08:43:43.783337Z')]),
},
theme: createTheme(),
}); });
it('should format string as number if unit is `none`', () => { expect(processor('2020-08-01T08:48:43Z').text).toEqual('2020-08-01 08:48:43');
const processor = getDisplayProcessorFromConfig({ unit: 'none' }, FieldType.string);
expect(processor('0x10').text).toEqual('16');
}); });
it('should not parse a 64 bit number when the data type is string', () => { it('should not include milliseconds when value range is < 60s with explicit unit time:', () => {
const value = '2882377905688543293'; const processor = getDisplayProcessor({
const instance = getDisplayProcessorFromConfig({}, FieldType.string); timeZone: 'utc',
const disp = instance(value); field: {
expect(disp.text).toEqual(value); type: FieldType.time,
config: {
unit: 'time:YYYY-MM-DD HH:mm',
},
values: new ArrayVector([Date.parse('2020-08-01T08:48:43.783337Z'), Date.parse('2020-08-01T08:49:15.123456Z')]),
},
theme: createTheme(),
}); });
expect(processor('2020-08-01T08:48:43.783337Z').text).toEqual('2020-08-01 08:48');
}); });
}); });

@ -62,7 +62,7 @@ export function getDisplayProcessor(options?: DisplayProcessorOptions): DisplayP
start /= 1e3; start /= 1e3;
end /= 1e3; end /= 1e3;
} }
showMs = end - start < 60; //show ms when minute or less showMs = Math.abs(end - start) < 60; //show ms when minute or less
} }
} else if (field.type === FieldType.boolean) { } else if (field.type === FieldType.boolean) {
if (!isBooleanUnit(unit)) { if (!isBooleanUnit(unit)) {

Loading…
Cancel
Save