111 lines
2.1 KiB
TypeScript
111 lines
2.1 KiB
TypeScript
import dayjs from "dayjs";
|
|
import advancedFormat from "dayjs/plugin/advancedFormat";
|
|
import type { Metadata } from "./Metadata";
|
|
|
|
export class Pod
|
|
{
|
|
metadata?: Metadata
|
|
status?: Status
|
|
spec?: Spec
|
|
}
|
|
|
|
class Spec {
|
|
nodeName?: string;
|
|
}
|
|
|
|
class Status
|
|
{
|
|
phase?: PodStatus
|
|
}
|
|
|
|
enum PodStatus
|
|
{
|
|
RUNNING = "Running",
|
|
SUCCEEDED = "Succeeded",
|
|
FAILED = "Failed"
|
|
}
|
|
|
|
enum ChronoUnit
|
|
{
|
|
SECOND, MINUTE, HOUR, DAY
|
|
}
|
|
|
|
export function calcAge(datetime: string | undefined)
|
|
{
|
|
dayjs.extend(advancedFormat);
|
|
if(datetime != null)
|
|
{
|
|
const today = Number(dayjs().format('X'));
|
|
const createdAt = Number(dayjs(datetime).format('X'));
|
|
const dif = today - createdAt;
|
|
if(dif < 60)
|
|
{
|
|
return format(dif, ChronoUnit.SECOND);
|
|
}
|
|
if(dif < 60 * 60)
|
|
{
|
|
const minutes = Math.floor((dif / 60));
|
|
return format(minutes, ChronoUnit.MINUTE);
|
|
}
|
|
if(dif < 60 * 60 * 24)
|
|
{
|
|
const hours = Math.floor(dif / (60 * 60));
|
|
return format(hours, ChronoUnit.HOUR);
|
|
}
|
|
else
|
|
{
|
|
const days = Math.floor(dif / (60 * 60 * 24));
|
|
return format(days, ChronoUnit.DAY);
|
|
}
|
|
}
|
|
return "-";
|
|
}
|
|
|
|
function format(count: number, unit: ChronoUnit): string
|
|
{
|
|
if(unit === ChronoUnit.SECOND)
|
|
{
|
|
if(count != 1)
|
|
{
|
|
return count + " Sekunden";
|
|
}
|
|
else
|
|
{
|
|
return count + " Sekunde";
|
|
}
|
|
}
|
|
if(unit === ChronoUnit.MINUTE)
|
|
{
|
|
if(count != 1)
|
|
{
|
|
return count + " Minuten";
|
|
}
|
|
else
|
|
{
|
|
return count + " Minute";
|
|
}
|
|
}
|
|
if(unit === ChronoUnit.HOUR)
|
|
{
|
|
if(count != 1)
|
|
{
|
|
return count + " Stunden";
|
|
}
|
|
else
|
|
{
|
|
return count + " Stunde";
|
|
}
|
|
}
|
|
if(unit === ChronoUnit.DAY)
|
|
{
|
|
if(count != 1)
|
|
{
|
|
return count + " Tage";
|
|
}
|
|
else
|
|
{
|
|
return count + " Tag";
|
|
}
|
|
}
|
|
return "-";
|
|
} |