import { record, type RecordValue } from './api'; export type GroupKind = 'hypervisor' | 'hetzner' | 'physical' | 'unassigned'; export type RowRole = 'host' | 'guest' | 'plain'; export type FleetGroup = { key: string; kind: GroupKind; /** Hostname of the hypervisor; empty for the fixed blocks. */ title: string; customer: string; /** Detail route of the hypervisor, also when its row is not part of this page. */ hostPath: string | null; hostListed: boolean; rows: { device: RecordValue; role: RowRole }[]; }; const text = (value: unknown) => (typeof value === 'string' ? value : ''); const route = (tenant: string, asset: string, agent: string) => tenant && asset && agent ? ['geraete', tenant, asset, agent].map(encodeURIComponent).join('/') : null; /** The registered hypervisor a virtual device is assigned to, if any. */ function assignedHost(device: RecordValue) { if (device.device_type !== 'virtual' || !device.hypervisor || typeof device.hypervisor !== 'object') return null; const info = record(device.hypervisor); const asset = text(info.asset_id); return info.kind === 'device' && asset ? { asset, agent: text(info.agent_id), hostname: text(info.hostname) } : null; } function isHetzner(device: RecordValue) { if (device.device_type !== 'virtual' || !device.hypervisor || typeof device.hypervisor !== 'object') return false; const info = record(device.hypervisor); return info.kind === 'placeholder' && info.provider === 'hetzner'; } /** * Sorts one page of the device list into blocks: one per hypervisor with its virtual * machines, then Hetzner guests, physical devices and everything without a known place. * A hypervisor is a block of its own within its customer; guests whose host is not on * this page still get that host's block, without the host row. */ export function groupDevices(items: RecordValue[]): FleetGroup[] { const hosts = new Map(); const fixed = new Map(); const hostGroup = (tenant: string, asset: string) => { const key = tenant + '/' + asset; let group = hosts.get(key); if (!group) { group = { key, kind: 'hypervisor', title: '', customer: '', hostPath: null, hostListed: false, rows: [] }; hosts.set(key, group); } return group; }; const fixedGroup = (kind: GroupKind) => { let group = fixed.get(kind); if (!group) { group = { key: kind, kind, title: '', customer: '', hostPath: null, hostListed: false, rows: [] }; fixed.set(kind, group); } return group; }; const referenced = new Set(); for (const device of items) { const host = assignedHost(device); if (host) referenced.add(text(device.tenant_id) + '/' + host.asset); } for (const device of items) { const tenant = text(device.tenant_id), asset = text(device.asset_id); const host = assignedHost(device); if (device.device_type === 'hypervisor' || (!host && referenced.has(tenant + '/' + asset))) { const group = hostGroup(tenant, asset); group.title = text(device.hostname); group.customer = text(device.customer_name); group.hostPath = route(tenant, asset, text(device.agent_id)); group.hostListed = true; group.rows.unshift({ device, role: 'host' }); } else if (host) { const group = hostGroup(tenant, host.asset); if (!group.hostListed) { group.title = host.hostname; group.customer = text(device.customer_name); group.hostPath = route(tenant, host.asset, host.agent); } group.rows.push({ device, role: 'guest' }); } else { const kind = isHetzner(device) ? 'hetzner' : device.device_type === 'physical' ? 'physical' : 'unassigned'; fixedGroup(kind).rows.push({ device, role: kind === 'hetzner' ? 'guest' : 'plain' }); } } return [...hosts.values(), ...(['hetzner', 'physical', 'unassigned'] as const).flatMap(kind => fixed.get(kind) ?? [])]; }