From c94020f7dc8d363df43f79f86fea5b2ddecceb67 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 17 Jan 2023 19:50:37 -0800 Subject: [PATCH] app/vmui: do not round the number in formatPrettyNumber() if the range isn't set --- .../packages/vmui/src/pages/TopQueries/index.tsx | 2 +- app/vmui/packages/vmui/src/utils/uplot/helpers.ts | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/vmui/packages/vmui/src/pages/TopQueries/index.tsx b/app/vmui/packages/vmui/src/pages/TopQueries/index.tsx index 752f8c1ee..ef269d830 100644 --- a/app/vmui/packages/vmui/src/pages/TopQueries/index.tsx +++ b/app/vmui/packages/vmui/src/pages/TopQueries/index.tsx @@ -40,7 +40,7 @@ const Index: FC = () => { const getQueryStatsTitle = (key: keyof TopQueryStats) => { if (!data) return key; const value = data[key]; - if (typeof value === "number") return formatPrettyNumber(value, 0, value); + if (typeof value === "number") return formatPrettyNumber(value); return value || key; }; diff --git a/app/vmui/packages/vmui/src/utils/uplot/helpers.ts b/app/vmui/packages/vmui/src/utils/uplot/helpers.ts index d55fa54cb..0c3f6c4b7 100644 --- a/app/vmui/packages/vmui/src/utils/uplot/helpers.ts +++ b/app/vmui/packages/vmui/src/utils/uplot/helpers.ts @@ -37,7 +37,18 @@ export const formatPrettyNumber = (n: number | null | undefined, min = 0, max = if (n === undefined || n === null) { return ""; } - let digits = 3 + Math.floor(1 + Math.log10(Math.max(Math.abs(min), Math.abs(max))) - Math.log10(Math.abs(min - max))); + const range = Math.abs(max - min); + if (isNaN(range) || range == 0) { + // Return the constant number as is if the range isn't set of it is too small. + if (Math.abs(n) >= 1000) { + return n.toLocaleString("en-US"); + } + return n.toString(); + } + // Make sure n has 3 significant digits on the given range. + // This precision should be enough for most UX cases, + // since the remaining digits are usually a white noise. + let digits = 3 + Math.floor(1 + Math.log10(Math.max(Math.abs(min), Math.abs(max))) - Math.log10(range)); if (isNaN(digits) || digits > 20) { digits = 20; }