2024-01-05 02:42:36 +00:00
|
|
|
import { Annotation } from './annotations/types';
|
|
|
|
|
2023-10-16 20:19:08 +00:00
|
|
|
export function parseCpu(cpu: string) {
|
|
|
|
let res = parseInt(cpu, 10);
|
|
|
|
if (cpu.endsWith('m')) {
|
|
|
|
res /= 1000;
|
|
|
|
} else if (cpu.endsWith('n')) {
|
|
|
|
res /= 1000000000;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2024-01-05 02:42:36 +00:00
|
|
|
|
2024-01-15 00:30:45 +00:00
|
|
|
export function prepareAnnotations(annotations?: Annotation[]) {
|
|
|
|
const result = annotations?.reduce(
|
2024-01-05 02:42:36 +00:00
|
|
|
(acc, a) => {
|
2024-12-10 21:15:46 +00:00
|
|
|
acc[a.key] = a.value;
|
2024-01-05 02:42:36 +00:00
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
{} as Record<string, string>
|
|
|
|
);
|
|
|
|
return result;
|
|
|
|
}
|
2025-02-26 01:13:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the safe value of the given number or string.
|
|
|
|
* @param value - The value to get the safe value for.
|
|
|
|
* @returns The safe value of the given number or string.
|
|
|
|
*/
|
|
|
|
export function getSafeValue(value: number | string) {
|
|
|
|
const valueNumber = Number(value);
|
|
|
|
if (Number.isNaN(valueNumber)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return valueNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the percentage of the value over the total.
|
|
|
|
* @param value - The value to calculate the percentage for.
|
|
|
|
* @param total - The total value to compare the percentage to.
|
|
|
|
* @returns The percentage of the value over the total, with the '- ' string prefixed, for example '- 50%'.
|
|
|
|
*/
|
|
|
|
export function getPercentageString(value: number, total?: number | string) {
|
|
|
|
const totalNumber = Number(total);
|
|
|
|
if (
|
|
|
|
totalNumber === 0 ||
|
|
|
|
total === undefined ||
|
|
|
|
total === '' ||
|
|
|
|
Number.isNaN(totalNumber)
|
|
|
|
) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
if (value > totalNumber) {
|
|
|
|
return '- Exceeded';
|
|
|
|
}
|
|
|
|
return `- ${Math.round((value / totalNumber) * 100)}%`;
|
|
|
|
}
|