Files
doktainer-upstream/src/app/servers/components/server-utils.ts
T
2026-06-02 00:17:50 +07:00

44 lines
1.4 KiB
TypeScript

export function formatUptime(sec: string | null | undefined): string {
if (!sec) return "—";
const seconds = Number(sec);
const days = Math.floor(seconds / 86400);
const hours = Math.floor((seconds % 86400) / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
if (days > 0) return `${days}d ${hours}h`;
if (hours > 0) return `${hours}h ${minutes}m`;
return `${minutes}m`;
}
export function formatBytes(bytes: string | null | undefined): string {
if (!bytes) return "—";
const value = Number(bytes);
if (value >= 1e9) return `${(value / 1e9).toFixed(1)} GB`;
if (value >= 1e6) return `${(value / 1e6).toFixed(0)} MB`;
return `${(value / 1e3).toFixed(0)} KB`;
}
export function formatLastUpdated(value: string | null | undefined): string {
if (!value) return "—";
const date = new Date(value);
if (Number.isNaN(date.getTime())) return "—";
const diffSeconds = Math.max(
0,
Math.floor((Date.now() - date.getTime()) / 1000),
);
if (diffSeconds < 60) return `${diffSeconds}s ago`;
if (diffSeconds < 3600) return `${Math.floor(diffSeconds / 60)}m ago`;
if (diffSeconds < 86400) return `${Math.floor(diffSeconds / 3600)}h ago`;
return date.toLocaleString();
}
export function formatDateTime(value: string | null | undefined): string {
if (!value) return "—";
const date = new Date(value);
if (Number.isNaN(date.getTime())) return value;
return date.toLocaleString();
}