Fix handling of scalar and string in isHeatmapData

Signed-off-by: Kevin Mingtarja <kevin.mingtarja@gmail.com>
This commit is contained in:
Kevin Mingtarja 2024-01-04 02:48:54 +07:00
parent c126c21baa
commit 40941d450b
2 changed files with 52 additions and 11 deletions

View File

@ -1,8 +1,23 @@
import { DataTableProps } from './DataTable';
import { isHeatmapData } from './GraphHeatmapHelpers'; import { isHeatmapData } from './GraphHeatmapHelpers';
describe('GraphHeatmapHelpers', () => { describe('GraphHeatmapHelpers', () => {
it('isHeatmapData should return false for a vector', () => { it('isHeatmapData should return false for scalar and string resultType', () => {
const data = { let data = {
resultType: 'scalar',
result: [1703091180.125, '1703091180.125'],
} as DataTableProps['data'];
expect(isHeatmapData(data)).toBe(false);
data = {
resultType: 'string',
result: [1704305680.332, '2504'],
} as DataTableProps['data'];
expect(isHeatmapData(data)).toBe(false);
});
it('isHeatmapData should return false for a vector and matrix if length < 2', () => {
let data = {
resultType: 'vector', resultType: 'vector',
result: [ result: [
{ {
@ -13,15 +28,39 @@ describe('GraphHeatmapHelpers', () => {
value: [1703091180.683, '6'], value: [1703091180.683, '6'],
}, },
], ],
}; } as DataTableProps['data'];
expect(isHeatmapData(data)).toBe(false);
data = {
resultType: 'matrix',
result: [
{
metric: {},
values: [[1703091180.683, '6']],
},
],
} as DataTableProps['data'];
expect(isHeatmapData(data)).toBe(false); expect(isHeatmapData(data)).toBe(false);
}); });
it('isHeatmapData should return false for scalar resultType', () => { it('isHeatmapData should return true for valid heatmap data', () => {
const data = { const data = {
resultType: 'scalar', resultType: 'matrix',
result: [1703091180.125, '1703091180.125'], result: [
}; {
expect(isHeatmapData(data)).toBe(false); metric: {
le: '100',
},
values: [[1703091180.683, '6']],
},
{
metric: {
le: '1000',
},
values: [[1703091190.683, '6.1']],
},
],
} as DataTableProps['data'];
expect(isHeatmapData(data)).toBe(true);
}); });
}); });

View File

@ -1,10 +1,12 @@
import { DataTableProps } from './DataTable';
import { GraphProps, GraphSeries } from './Graph'; import { GraphProps, GraphSeries } from './Graph';
export function isHeatmapData(data: GraphProps['data']) { export function isHeatmapData(data: DataTableProps['data']) {
if (!data?.result?.length || data?.result?.length < 2) { if (data?.resultType === 'scalar' || data?.resultType === 'string' || !data?.result?.length || data?.result?.length < 2) {
return false; return false;
} }
const result = data.result; // Type assertion to prevent TS2349 error.
const result = data.result as GraphProps['data']['result'];
const firstLabels = Object.keys(result[0].metric).filter((label) => label !== 'le'); const firstLabels = Object.keys(result[0].metric).filter((label) => label !== 'le');
return result.every(({ metric }) => { return result.every(({ metric }) => {
const labels = Object.keys(metric).filter((label) => label !== 'le'); const labels = Object.keys(metric).filter((label) => label !== 'le');