Merge pull request #13371 from kevinmingtarja/fix-isHeatmapData

ui: fix handling of scalar and string in isHeatmapData
This commit is contained in:
Bartlomiej Plotka 2024-01-07 19:59:20 +01:00 committed by GitHub
commit 775d955919
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 6 deletions

View file

@ -338,7 +338,7 @@ describe('DataTable', () => {
const dataTableProps: DataTableProps = {
data: {
resultType: 'string',
result: 'string',
result: [1572098246.599, 'test'],
},
useLocalTime: false,
};
@ -346,7 +346,7 @@ describe('DataTable', () => {
it('renders a string row', () => {
const table = dataTable.find(Table);
const rows = table.find('tr');
expect(rows.text()).toEqual('stringt');
expect(rows.text()).toEqual('stringtest');
});
});
});

View file

@ -24,7 +24,7 @@ export interface DataTableProps {
}
| {
resultType: 'string';
result: string;
result: SampleValue;
};
useLocalTime: boolean;
}

View file

@ -0,0 +1,66 @@
import { DataTableProps } from './DataTable';
import { isHeatmapData } from './GraphHeatmapHelpers';
describe('GraphHeatmapHelpers', () => {
it('isHeatmapData should return false for scalar and string resultType', () => {
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',
result: [
{
metric: {
__name__: 'my_gauge',
job: 'target',
},
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);
});
it('isHeatmapData should return true for valid heatmap data', () => {
const data = {
resultType: 'matrix',
result: [
{
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';
export function isHeatmapData(data: GraphProps['data']) {
if (!data?.result?.length || data?.result?.length < 2) {
export function isHeatmapData(data: DataTableProps['data']) {
if (data?.resultType === 'scalar' || data?.resultType === 'string' || !data?.result?.length || data?.result?.length < 2) {
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');
return result.every(({ metric }) => {
const labels = Object.keys(metric).filter((label) => label !== 'le');