import type { ContainerHealth, ContainerStat, ContainerState } from "@/types/Container"; import type { UseThrottledRefHistoryReturn } from "@vueuse/core"; import { Ref } from "vue"; type Stat = Omit; const SWARM_ID_REGEX = /(\.[a-z0-9]{25})+$/i; export class Container { public stat: Ref; private readonly throttledStatHistory: UseThrottledRefHistoryReturn; public readonly swarmId: string | null = null; public readonly isSwarm: boolean = false; constructor( public readonly id: string, public readonly created: Date, public readonly image: string, public readonly name: string, public readonly command: string, public status: string, public state: ContainerState, public health?: ContainerHealth ) { this.stat = ref({ cpu: 0, memory: 0, memoryUsage: 0 }); this.throttledStatHistory = useThrottledRefHistory(this.stat, { capacity: 300, deep: true, throttle: 1000 }); const match = name.match(SWARM_ID_REGEX); if (match) { this.swarmId = match[0]; this.name = name.replace(`${this.swarmId}`, ""); this.isSwarm = true; } } public getStatHistory() { return unref(this.throttledStatHistory.history); } public getLastStat() { return unref(this.throttledStatHistory.last); } }