Skip to content

Commit 28253af

Browse files
committed
feat: Add round function for decimal rounding in resource utilization calculations
1 parent 6252029 commit 28253af

File tree

3 files changed

+19
-41
lines changed

3 files changed

+19
-41
lines changed

src/lib/utils/resources.ts

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import {
77
} from '$houdini';
88
import bytes from 'bytes-iec';
99

10+
export function round(value: number, decimals: number = 0): number {
11+
const factor = Math.pow(10, decimals);
12+
return Math.round(value * factor) / factor;
13+
}
14+
1015
// memory should be in Bytes
1116
export function yearlyOverageCost(
1217
resourceType: UsageResourceType$options,

src/routes/team/[team]/(teamPages)/utilization/+page.svelte

+7-21
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import MemoryIcon from '$lib/icons/MemoryIcon.svelte';
88
import { euroValueFormatter, percentageFormatter } from '$lib/utils/formatters';
99
import {
10-
Accordion,
11-
AccordionItem,
1210
Alert,
1311
HelpText,
1412
Table,
@@ -25,7 +23,7 @@
2523
import { goto } from '$app/navigation';
2624
import { PendingValue, UsageResourceType } from '$houdini';
2725
import { truncateString } from '$lib/chart/util';
28-
import { mergeCalculateAndSortOverageData, yearlyOverageCost } from '$lib/utils/resources';
26+
import { mergeCalculateAndSortOverageData, round, yearlyOverageCost } from '$lib/utils/resources';
2927
import type { EChartsOption } from 'echarts';
3028
import prettyBytes from 'pretty-bytes';
3129
@@ -226,13 +224,7 @@
226224
(acc, { used }) => acc + used,
227225
0
228226
)}
229-
{percentageFormatter((cpuUsage / cpuRequested) * 100)} of {cpuRequested.toLocaleString(
230-
'en-GB',
231-
{
232-
minimumFractionDigits: 2,
233-
maximumFractionDigits: 2
234-
}
235-
)} cores
227+
{percentageFormatter(round((cpuUsage / cpuRequested) * 100,0))} of {round(cpuRequested,0)} cores
236228
{/if}
237229
</p>
238230
</div>
@@ -259,7 +251,7 @@
259251
(acc, { used }) => acc + used,
260252
0
261253
)}
262-
{percentageFormatter((memoryUsage / memoryRequested) * 100)} of {bytes.format(
254+
{percentageFormatter(round((memoryUsage / memoryRequested) * 100,0))} of {bytes.format(
263255
memoryRequested,
264256
{ decimalPlaces: 2 }
265257
)}
@@ -290,14 +282,11 @@
290282
(acc, { used }) => acc + used,
291283
0
292284
)}
293-
€{yearlyOverageCost(
285+
€{round(yearlyOverageCost(
294286
UsageResourceType.CPU,
295287
cpuRequested,
296288
cpuUsage / cpuRequested
297-
).toLocaleString('en-GB', {
298-
minimumFractionDigits: 2,
299-
maximumFractionDigits: 2
300-
})}
289+
),0)}
301290
{/if}
302291
</p>
303292
</div>
@@ -325,14 +314,11 @@
325314
(acc, { used }) => acc + used,
326315
0
327316
)}
328-
€{yearlyOverageCost(
317+
€{round(yearlyOverageCost(
329318
UsageResourceType.MEMORY,
330319
memoryRequested,
331320
memoryUsage / memoryRequested
332-
).toLocaleString('en-GB', {
333-
minimumFractionDigits: 2,
334-
maximumFractionDigits: 2
335-
})}
321+
),0)}
336322
{/if}
337323
</p>
338324
</div>

src/routes/utilization/+page.svelte

+7-20
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
import { euroValueFormatter, percentageFormatter } from '$lib/utils/formatters';
1111
import {
1212
mergeCalculateAndSortOverageDataAllTeams,
13+
round,
1314
yearlyOverageCost
1415
} from '$lib/utils/resources';
1516
import {
16-
Accordion,
17-
AccordionItem,
1817
Alert,
1918
HelpText,
2019
Table,
@@ -243,13 +242,7 @@
243242
(acc, { used }) => acc + used,
244243
0
245244
)}
246-
{percentageFormatter((cpuUsage / cpuRequested) * 100)} of {cpuRequested.toLocaleString(
247-
'en-GB',
248-
{
249-
minimumFractionDigits: 2,
250-
maximumFractionDigits: 2
251-
}
252-
)} cores
245+
{percentageFormatter(round((cpuUsage / cpuRequested) * 100),0)} of {round(cpuRequested,0)} cores
253246
{/if}
254247
</p>
255248
</div>
@@ -276,7 +269,7 @@
276269
(acc, { used }) => acc + used,
277270
0
278271
)}
279-
{percentageFormatter((memoryUsage / memoryRequested) * 100)} of {bytes.format(
272+
{percentageFormatter(round((memoryUsage / memoryRequested) * 100),0)} of {bytes.format(
280273
memoryRequested,
281274
{ decimalPlaces: 2 }
282275
)}
@@ -307,14 +300,11 @@
307300
(acc, { used }) => acc + used,
308301
0
309302
)}
310-
€{yearlyOverageCost(
303+
€{round(yearlyOverageCost(
311304
UsageResourceType.CPU,
312305
cpuRequested,
313306
cpuUsage / cpuRequested
314-
).toLocaleString('en-GB', {
315-
minimumFractionDigits: 2,
316-
maximumFractionDigits: 2
317-
})}
307+
),0)}
318308
{/if}
319309
</p>
320310
</div>
@@ -342,14 +332,11 @@
342332
(acc, { used }) => acc + used,
343333
0
344334
)}
345-
€{yearlyOverageCost(
335+
€{round(yearlyOverageCost(
346336
UsageResourceType.MEMORY,
347337
memoryRequested,
348338
memoryUsage / memoryRequested
349-
).toLocaleString('en-GB', {
350-
minimumFractionDigits: 2,
351-
maximumFractionDigits: 2
352-
})}
339+
),0)}
353340
{/if}
354341
</p>
355342
</div>

0 commit comments

Comments
 (0)