[DASHBOARDS] Move all the graph computing logic to the backend (#17189)
- Create resolvers for each type of charts which needs data transformation after the group by operation: Bar Chart, Line Chart and Pie Chart - Move all the utils to the backend and refactored some into services This allows all the computation to be done in the backend, improving performances in the frontend.
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import { formatToShortNumber } from '../format/formatToShortNumber';
|
||||
|
||||
describe('formatToShortNumber', () => {
|
||||
it('formats numbers less than 1000 correctly', () => {
|
||||
expect(formatToShortNumber(500)).toBe('500');
|
||||
expect(formatToShortNumber(123.456)).toBe('123.5');
|
||||
});
|
||||
|
||||
it('formats numbers between 1000 and 999999 correctly', () => {
|
||||
expect(formatToShortNumber(1500)).toBe('1.5k');
|
||||
expect(formatToShortNumber(789456)).toBe('789.5k');
|
||||
});
|
||||
|
||||
it('formats numbers between 1000000 and 999999999 correctly', () => {
|
||||
expect(formatToShortNumber(2000000)).toBe('2m');
|
||||
expect(formatToShortNumber(654987654)).toBe('655m');
|
||||
});
|
||||
|
||||
it('formats numbers greater than or equal to 1000000000 correctly', () => {
|
||||
expect(formatToShortNumber(1200000000)).toBe('1.2b');
|
||||
expect(formatToShortNumber(987654321987)).toBe('987.7b');
|
||||
});
|
||||
|
||||
it('handles numbers with decimal places correctly', () => {
|
||||
expect(formatToShortNumber(123.456)).toBe('123.5');
|
||||
expect(formatToShortNumber(789.0123)).toBe('789');
|
||||
});
|
||||
|
||||
it('handles zero correctly', () => {
|
||||
expect(formatToShortNumber(0)).toBe('0');
|
||||
});
|
||||
|
||||
describe('negative numbers', () => {
|
||||
it('formats negative numbers less than 1000 correctly', () => {
|
||||
expect(formatToShortNumber(-500)).toBe('-500');
|
||||
expect(formatToShortNumber(-123.456)).toBe('-123.5');
|
||||
});
|
||||
|
||||
it('formats negative thousands correctly', () => {
|
||||
expect(formatToShortNumber(-1500)).toBe('-1.5k');
|
||||
expect(formatToShortNumber(-789456)).toBe('-789.5k');
|
||||
});
|
||||
|
||||
it('formats negative millions correctly', () => {
|
||||
expect(formatToShortNumber(-2000000)).toBe('-2m');
|
||||
expect(formatToShortNumber(-654987654)).toBe('-655m');
|
||||
});
|
||||
|
||||
it('formats negative billions correctly', () => {
|
||||
expect(formatToShortNumber(-1200000000)).toBe('-1.2b');
|
||||
expect(formatToShortNumber(-987654321987)).toBe('-987.7b');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
export const formatToShortNumber = (amount: number) => {
|
||||
const sign = amount < 0 ? '-' : '';
|
||||
const absoluteAmount = Math.abs(amount);
|
||||
|
||||
if (absoluteAmount < 1000) {
|
||||
return sign + absoluteAmount.toFixed(1).replace(/\.?0+$/, '');
|
||||
}
|
||||
|
||||
if (absoluteAmount < 1_000_000) {
|
||||
return (
|
||||
sign + (absoluteAmount / 1000).toFixed(1).replace(/\.?0+$/, '') + 'k'
|
||||
);
|
||||
}
|
||||
|
||||
if (absoluteAmount < 1_000_000_000) {
|
||||
return (
|
||||
sign + (absoluteAmount / 1_000_000).toFixed(1).replace(/\.?0+$/, '') + 'm'
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
sign +
|
||||
(absoluteAmount / 1_000_000_000).toFixed(1).replace(/\.?0+$/, '') +
|
||||
'b'
|
||||
);
|
||||
};
|
||||
@@ -123,6 +123,7 @@ export {
|
||||
relationFilterValueSchemaObject,
|
||||
jsonRelationFilterValueSchema,
|
||||
} from './filter/utils/validation-schemas/jsonRelationFilterValueSchema';
|
||||
export { formatToShortNumber } from './format/formatToShortNumber';
|
||||
export { fromArrayToUniqueKeyRecord } from './from-array-to-unique-key-record.util';
|
||||
export { fromArrayToValuesByKeyRecord } from './fromArrayToValuesByKeyRecord.util';
|
||||
export { getURLSafely } from './getURLSafely';
|
||||
|
||||
Reference in New Issue
Block a user