Skip to content

Commit 7a7e3b0

Browse files
Taj010Ambient Code Botclaude
authored
fix(metrics): round percentage tooltips to 2 decimal places (#6853)
* fix(metrics): round percentage tooltips to 2 decimal places Fixes excessive decimal precision in KServe metric tooltips by applying .toFixed(2) rounding to the toPercentage transformation function. Before: Memory utilization tooltips displayed raw floating-point values like "9.896161479334678%" After: Values are rounded to 2 decimal places: "9.90%" This fix applies to all percentage-based metrics: - KserveMemoryUsageGraph - KserveCpuUsageGraph - NIMKVCacheUsageGraph Implementation follows the existing per100 function pattern and makes a single-line change that fixes all three affected components centrally. Added comprehensive regression tests (12 test cases) that verify: - Original bug scenario (0.09896... → 9.90) - Rounding edge cases (0%, 100%, near boundaries) - Floating-point precision artifacts - Property preservation Fixes RHOAIENG-17618 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * lint test fix --------- Co-authored-by: Ambient Code Bot <bot@ambient-code.local> Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent ae12aa3 commit 7a7e3b0

2 files changed

Lines changed: 85 additions & 2 deletions

File tree

frontend/src/pages/modelServing/screens/metrics/__tests__/utils.spec.ts

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { convertInputType } from '#~/pages/modelServing/screens/metrics/utils';
1+
import {
2+
convertInputType,
3+
toPercentage,
4+
per100,
5+
} from '#~/pages/modelServing/screens/metrics/utils';
26

37
describe('convertInputType', () => {
48
describe('string inputs', () => {
@@ -40,3 +44,82 @@ describe('convertInputType', () => {
4044
});
4145
});
4246
});
47+
48+
describe('toPercentage', () => {
49+
it('should round percentage values to 2 decimal places', () => {
50+
const input = { x: 1000, y: 0.09896161479334678, name: 'test' };
51+
const result = toPercentage(input);
52+
expect(result.y).toBe(9.9);
53+
});
54+
55+
it('should handle values that round up', () => {
56+
const input = { x: 1000, y: Number('0.15228456789012345'), name: 'test' };
57+
const result = toPercentage(input);
58+
expect(result.y).toBe(15.23);
59+
});
60+
61+
it('should handle values that round down', () => {
62+
const input = { x: 1000, y: 0.15224, name: 'test' };
63+
const result = toPercentage(input);
64+
expect(result.y).toBe(15.22);
65+
});
66+
67+
it('should handle edge case near 100%', () => {
68+
const input = { x: 1000, y: 0.999999, name: 'test' };
69+
const result = toPercentage(input);
70+
expect(result.y).toBe(100);
71+
});
72+
73+
it('should handle edge case near 0%', () => {
74+
const input = { x: 1000, y: 0.005, name: 'test' };
75+
const result = toPercentage(input);
76+
expect(result.y).toBe(0.5);
77+
});
78+
79+
it('should handle exact zero', () => {
80+
const input = { x: 1000, y: 0, name: 'test' };
81+
const result = toPercentage(input);
82+
expect(result.y).toBe(0);
83+
});
84+
85+
it('should preserve x and name properties', () => {
86+
const input = { x: 12345, y: 0.5, name: 'Memory Usage' };
87+
const result = toPercentage(input);
88+
expect(result.x).toBe(12345);
89+
expect(result.name).toBe('Memory Usage');
90+
});
91+
92+
it('should handle floating-point precision artifacts', () => {
93+
// Test that floating-point errors like 99.99999999999999 get properly rounded
94+
const input = { x: 1000, y: 0.9999999999999999, name: 'test' };
95+
const result = toPercentage(input);
96+
expect(result.y).toBe(100);
97+
});
98+
});
99+
100+
describe('per100', () => {
101+
it('should divide by 100 and round to 2 decimal places', () => {
102+
const input = { x: 1000, y: 989.6161479334678, name: 'test' };
103+
const result = per100(input);
104+
expect(result.y).toBe(9.9);
105+
});
106+
107+
it('should handle values that round up', () => {
108+
const input = { x: 1000, y: Number('1522.8456789012345'), name: 'test' };
109+
const result = per100(input);
110+
expect(result.y).toBe(15.23);
111+
});
112+
113+
it('should handle exact zero', () => {
114+
const input = { x: 1000, y: 0, name: 'test' };
115+
const result = per100(input);
116+
expect(result.y).toBe(0);
117+
});
118+
119+
it('should preserve x and name properties', () => {
120+
const input = { x: 12345, y: 5000, name: 'CPU Usage' };
121+
const result = per100(input);
122+
expect(result.x).toBe(12345);
123+
expect(result.name).toBe('CPU Usage');
124+
});
125+
});

frontend/src/pages/modelServing/screens/metrics/utils.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export const per100: TranslatePoint = (point) => ({
107107

108108
export const toPercentage: TranslatePoint = (point) => ({
109109
...point,
110-
y: point.y * 100,
110+
y: Number((point.y * 100).toFixed(2)),
111111
});
112112

113113
export const createGraphMetricLine = ({

0 commit comments

Comments
 (0)