Group the device overview by hypervisor, Hetzner, physical and unassigned devices
Rust development slice / linux (push) Failing after 7s

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
2026-11-20 22:57:02 +01:00
co-authored by Claude Fable 5.2
parent dbfa76184d
commit e29257b808
8 changed files with 352 additions and 104 deletions
+54
View File
@@ -0,0 +1,54 @@
import { expect, it } from 'vitest';
import type { RecordValue } from './api';
import { groupDevices } from './device-groups';
const tenant = '11111111-1111-4111-8111-111111111111';
const device = (hostname: string, device_type: string | null, extra: RecordValue = {}): RecordValue => ({
tenant_id: tenant, customer_name: 'Testkunde', asset_id: hostname + '-asset', agent_id: hostname + '-agent',
hostname, device_type, ...extra });
const on = (host: string, tenantHost = host) => ({ hypervisor: {
kind: 'device', asset_id: tenantHost + '-asset', agent_id: tenantHost + '-agent', hostname: host } });
const hetzner = { hypervisor: { kind: 'placeholder', provider: 'hetzner', hostname: 'Hetzner Cloud' } };
const shape = (items: RecordValue[]) => groupDevices(items).map(g =>
[g.kind, g.title, g.rows.map(r => r.role + ':' + String(r.device.hostname)).join(',')]);
it('lists each hypervisor with its virtual machines, then Hetzner, physical and unassigned devices', () => {
expect(shape([
device('vm-b', 'virtual', on('pve-01')),
device('laptop', 'physical'),
device('cloud', 'virtual', hetzner),
device('pve-01', 'hypervisor'),
device('stray', 'virtual'),
device('vm-a', 'virtual', on('pve-01')),
device('pve-02', 'hypervisor'),
device('untyped', null),
])).toEqual([
['hypervisor', 'pve-01', 'host:pve-01,guest:vm-b,guest:vm-a'],
['hypervisor', 'pve-02', 'host:pve-02'],
['hetzner', '', 'guest:cloud'],
['physical', '', 'plain:laptop'],
['unassigned', '', 'plain:stray,plain:untyped'],
]);
});
it('keeps the block and the link of a hypervisor that is not on this page', () => {
const [group] = groupDevices([device('vm-a', 'virtual', on('pve-01'))]);
expect(group).toMatchObject({ kind: 'hypervisor', title: 'pve-01', hostListed: false,
hostPath: 'geraete/' + tenant + '/pve-01-asset/pve-01-agent' });
expect(group?.rows.map(r => r.role)).toEqual(['guest']);
});
it('never merges hosts of two customers that share an asset id', () => {
const other = { tenant_id: '22222222-2222-4222-8222-222222222222', customer_name: 'Anderer Kunde' };
const groups = groupDevices([
device('pve-01', 'hypervisor'),
device('vm-a', 'virtual', { ...on('pve-01'), ...other }),
]);
expect(groups.map(g => [g.customer, g.hostListed, g.rows.length])).toEqual([
['Testkunde', true, 1], ['Anderer Kunde', false, 1]]);
});
it('uses a manually assigned host as the head of its block whatever type it reports', () => {
expect(shape([device('vm-a', 'virtual', on('esx')), device('esx', 'physical')])).toEqual([
['hypervisor', 'esx', 'host:esx,guest:vm-a']]);
});
+91
View File
@@ -0,0 +1,91 @@
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<string, FleetGroup>();
const fixed = new Map<GroupKind, FleetGroup>();
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<string>();
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) ?? [])];
}
+158 -93
View File
@@ -21,6 +21,7 @@ import { OPNsenseDetails } from './opnsense';
import { ProxmoxDetails } from './proxmox';
import { DeviceCustomerTransfer } from './device-customer-transfer';
import { HypervisorAssignment, HypervisorLabel } from './device-hypervisor';
import { groupDevices, type FleetGroup, type RowRole } from './device-groups';
import { ErrorBox, errorText, useResource } from './resource';
import { NavIcon, orderDetailRoute } from './navigation';
import { orderTitle, Status } from './order-presentation';
@@ -32,6 +33,49 @@ import { SystemNonstartReview } from './system-nonstart-review';
const unknown = 'Nicht gemeldet';
const noPurpose = 'Nicht hinterlegt';
const plural = (n: number, one: string, many: string) => `${n} ${n === 1 ? one : many}`;
function groupName(group: FleetGroup) {
return group.kind === 'hypervisor' ? 'Hypervisor ' + (group.title || 'unbekannt')
: group.kind === 'hetzner' ? 'Hetzner Cloud'
: group.kind === 'physical' ? 'Physische Geräte' : 'Nicht zugeordnet';
}
/** How a listed virtual machine got its place under a registered hypervisor. */
function linkSource(d: RecordValue) {
if (!d.hypervisor || typeof d.hypervisor !== 'object') return '';
const info = record(d.hypervisor);
return info.kind !== 'device' ? '' : info.source === 'proxmox' ? 'automatisch erkannt' : 'manuell zugeordnet';
}
function GroupHeader({ group, showCustomer }: { group: FleetGroup; showCustomer: boolean }) {
const guests = group.rows.filter((r) => r.role !== 'host').length;
const machines = plural(guests, 'virtuelle Maschine', 'virtuelle Maschinen');
const [icon, eyebrow, title, facts] =
group.kind === 'hypervisor'
? (['server', 'Hypervisor',
group.hostPath ? <a href={'#' + group.hostPath}>{group.title || 'Unbekannter Host'}</a> : group.title || 'Unbekannter Host',
[guests ? machines : 'Keine zugeordneten virtuellen Maschinen',
!group.hostListed && 'Host in dieser Ansicht nicht enthalten',
showCustomer && group.customer]] as const)
: group.kind === 'hetzner'
? (['cloud', 'Externer Anbieter', 'Hetzner Cloud', [machines, 'Platzhalter, kein Gerät im Bestand']] as const)
: group.kind === 'physical'
? (['geraete', 'Eigene Hardware', 'Physische Geräte', [plural(guests, 'Gerät', 'Geräte')]] as const)
: (['offen', 'Ohne Zuordnung', 'Nicht zugeordnet',
[plural(guests, 'Gerät', 'Geräte'), 'kein Hypervisor oder Gerätetyp erkannt']] as const);
return (
<div className="fleet-group-head">
<span className="group-icon"><NavIcon name={icon} /></span>
<div>
<span className="group-eyebrow">{eyebrow}</span>
<strong>{title}</strong>
</div>
<p>{facts.filter(Boolean).map((fact, i) => <span key={i}>{fact}</span>)}</p>
</div>
);
}
const number = new Intl.NumberFormat('de-DE', { maximumFractionDigits: 1 });
const date = new Intl.DateTimeFormat('de-DE', { dateStyle: 'medium', timeStyle: 'short' });
function label(value: unknown) {
@@ -580,6 +624,105 @@ export function Devices({
}}
/>
);
const columns = ['Hostname', 'Type', 'Zweck', 'OS', 'Status', 'Speicher belegt / gesamt', 'RAM belegt / gesamt', 'CPU', 'Kunde'];
const row = (d: RecordValue, role: RowRole | 'flat') => {
const asset = objectId(d, 'asset'),
agent = objectId(d, 'agent'),
customer = objectId(d, 'tenant'),
model = cpu(d),
purpose = typeof d.purpose === 'string' ? d.purpose : '';
const show = () => {
setDeletionNotice('');
window.location.hash = ['geraete', customer, asset, agent]
.map(encodeURIComponent)
.join('/');
};
const another = selection.ids.length > 0 && selection.customer !== customer;
return (
<tr key={agent} onClick={show} className={role === 'flat' ? undefined : role}>
{!history && (
<td className="target-col" onClick={(e) => e.stopPropagation()}>
<input
type="checkbox"
aria-label={'Ziel ' + asset}
title={
another ? 'Aufträge werden je Kunde geplant' : 'Für Auftrag auswählen'
}
disabled={another}
checked={selection.customer === customer && selection.ids.includes(asset)}
onChange={(e) => select(d, e.target.checked)}
/>
</td>
)}
<td className="hostname">
<button
className="host-link"
onClick={(e) => {
e.stopPropagation();
show();
}}
>
{label(d.hostname)}
</button>
{role === 'flat' && <HypervisorLabel device={d} />}
{role === 'guest' && linkSource(d) && <small className="link-source">{linkSource(d)}</small>}
</td>
<td data-label="Type">{label(d.device_type)}</td>
<td data-label="Zweck" className="purpose-col">
<span className={'purpose-text' + (purpose ? '' : ' muted')} title={purpose || undefined}>
{purpose || noPurpose}
</span>
</td>
<td data-label="OS">{label(d.os)}</td>
<td data-label="Status" className="device-presence">
<Presence d={d} />
{d.status === 'offline' && d.lifecycle === 'current' && (
<small>Zuletzt {time(d.last_contact_at)}</small>
)}
</td>
<td data-label="Speicher belegt / gesamt" className="capacity-cell">
<DiskCapacity device={d} />
</td>
<td data-label="RAM belegt / gesamt" className="capacity-cell" title={measuredAt(d.ram_observed_at)}>
<Capacity free={d.ram_available_bytes} total={d.ram_total_bytes} />
</td>
<td data-label="CPU">
{model.primary}
{model.secondary && <small>{model.secondary}</small>}
<small title={measuredAt(d.cpu_usage_observed_at)}>
Auslastung {cpuUsage(d.cpu_usage_percent)}
</small>
</td>
<td data-label="Kunde">{label(d.customer_name)}</td>
</tr>
);
};
/** Every block shares these column widths, so the blocks line up under one heading. */
const table = (rows: { device: RecordValue; role: RowRole | 'flat' }[], heading: 'visible' | 'hidden' | 'only') => (
<div className={'table fleet-table' + (heading === 'only' ? ' fleet-head' : '')} aria-hidden={heading === 'only' || undefined}>
<table>
<colgroup>
{!history && <col className="target-col" />}
{columns.map((h) => <col key={h} className={h === 'Zweck' ? 'purpose-col' : h === 'Hostname' ? 'host-col' : undefined} />)}
</colgroup>
<thead className={heading === 'hidden' ? 'sr-only' : undefined}>
<tr>
{!history && (
<th className="target-col">
<span className="sr-only">Ziel auswählen</span>
</th>
)}
{columns.map((h) => (
<th scope="col" key={h} className={h === 'Zweck' ? 'purpose-col' : undefined}>
{h}
</th>
))}
</tr>
</thead>
{heading !== 'only' && <tbody>{rows.map((r) => row(r.device, r.role))}</tbody>}
</table>
</div>
);
return (
<>
<div className="page-title">
@@ -669,99 +812,21 @@ export function Devices({
{!list.data && !list.error && <p className="list-message">Geräte werden geladen …</p>}
{list.data && (
<>
<div className="table fleet-table">
<table>
<thead>
<tr>
{!history && (
<th className="target-col">
<span className="sr-only">Ziel auswählen</span>
</th>
)}
{['Hostname', 'Type', 'Zweck', 'OS', 'Status', 'Speicher belegt / gesamt', 'RAM belegt / gesamt', 'CPU', 'Kunde'].map(
(h) => (
<th scope="col" key={h} className={h === 'Zweck' ? 'purpose-col' : undefined}>
{h}
</th>
)
)}
</tr>
</thead>
<tbody>
{list.data.items.map((d) => {
const asset = objectId(d, 'asset'),
agent = objectId(d, 'agent'),
customer = objectId(d, 'tenant'),
model = cpu(d),
purpose = typeof d.purpose === 'string' ? d.purpose : '';
const show = () => {
setDeletionNotice('');
window.location.hash = ['geraete', customer, asset, agent]
.map(encodeURIComponent)
.join('/');
};
const another = selection.ids.length > 0 && selection.customer !== customer;
return (
<tr key={agent} onClick={show}>
{!history && (
<td className="target-col" onClick={(e) => e.stopPropagation()}>
<input
type="checkbox"
aria-label={'Ziel ' + asset}
title={
another ? 'Aufträge werden je Kunde geplant' : 'Für Auftrag auswählen'
}
disabled={another}
checked={selection.customer === customer && selection.ids.includes(asset)}
onChange={(e) => select(d, e.target.checked)}
/>
</td>
)}
<td className="hostname">
<button
className="host-link"
onClick={(e) => {
e.stopPropagation();
show();
}}
>
{label(d.hostname)}
</button>
<HypervisorLabel device={d} />
</td>
<td data-label="Type">{label(d.device_type)}</td>
<td data-label="Zweck" className="purpose-col">
<span className={'purpose-text' + (purpose ? '' : ' muted')} title={purpose || undefined}>
{purpose || noPurpose}
</span>
</td>
<td data-label="OS">{label(d.os)}</td>
<td data-label="Status" className="device-presence">
<Presence d={d} />
{d.status === 'offline' && d.lifecycle === 'current' && (
<small>Zuletzt {time(d.last_contact_at)}</small>
)}
</td>
<td data-label="Speicher belegt / gesamt" className="capacity-cell">
<DiskCapacity device={d} />
</td>
<td data-label="RAM belegt / gesamt" className="capacity-cell" title={measuredAt(d.ram_observed_at)}>
<Capacity free={d.ram_available_bytes} total={d.ram_total_bytes} />
</td>
<td data-label="CPU">
{model.primary}
{model.secondary && <small>{model.secondary}</small>}
<small title={measuredAt(d.cpu_usage_observed_at)}>
Auslastung {cpuUsage(d.cpu_usage_percent)}
</small>
</td>
<td data-label="Kunde">{label(d.customer_name)}</td>
</tr>
);
})}
</tbody>
</table>
</div>
{history ? (
table(list.data.items.map((device) => ({ device, role: 'flat' as const })), 'visible')
) : (
!!list.data.items.length && (
<div className="fleet-groups">
{table([], 'only')}
{groupDevices(list.data.items).map((group) => (
<div className={'fleet-group ' + group.kind} role="group" aria-label={groupName(group)} key={group.key}>
<GroupHeader group={group} showCustomer={!filter} />
{table(group.rows, 'hidden')}
</div>
))}
</div>
)
)}
{!list.data.items.length && (
<div className="empty-list">
<NavIcon name="geraete" />
+4 -1
View File
@@ -12,7 +12,10 @@ const glyphs = {
suchen: 'M16 16l5 5 M18 10a8 8 0 1 1-16 0 8 8 0 0 1 16 0',
plus: 'M12 5v14 M5 12h14',
abmelden: 'M10 4H4v16h6 M8 12h13 M16 7l5 5-5 5',
zurueck: 'M20 12H4 M10 6l-6 6 6 6'
zurueck: 'M20 12H4 M10 6l-6 6 6 6',
server: 'M3 4h18v7H3z M3 13h18v7H3z M7 7.5h.01 M7 16.5h.01 M11 7.5h6 M11 16.5h6',
cloud: 'M7 19a4.5 4.5 0 0 1-.6-9A6 6 0 0 1 18 11.2a3.9 3.9 0 0 1 0 7.8z',
offen: 'M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0z M9.6 9.4a2.5 2.5 0 1 1 3.6 2.3c-.8.5-1.2 1-1.2 1.9 M12 17h.01'
};
export function NavIcon({ name }: { name: keyof typeof glyphs }) {
return (
+30 -1
View File
@@ -97,7 +97,32 @@ section.fleet { padding:0; overflow:hidden; }
.vm-hypervisor { display:flex; align-items:baseline; gap:6px; margin-top:5px; color:var(--subtle); font-size:.78rem; overflow-wrap:anywhere; }
.vm-hypervisor .nav-icon { width:13px; height:13px; flex:none; align-self:flex-start; margin-top:2px; }
.fleet-table .target-col { width:35px; }
.fleet-table th.purpose-col { width:17%; }
.fleet-table th.purpose-col,.fleet-table col.purpose-col { width:17%; }
.fleet-groups { --group:var(--accent); --group-soft:#eaf3ef; padding:0 14px 16px; background:#f7f9f8; border-top:1px solid var(--line); }
.fleet-head thead th { background:none; border-bottom:0; padding-block:11px 9px; }
.fleet-group { background:var(--surface); border:1px solid var(--line); border-left:4px solid var(--group); border-radius:10px; overflow:hidden; box-shadow:0 1px 2px rgba(21,45,42,.05); }
.fleet-group + .fleet-group { margin-top:14px; }
.fleet-group.hetzner { --group:#b4432f; --group-soft:#fbeeea; }
.fleet-group.physical { --group:var(--blue); --group-soft:#e8f1f4; }
.fleet-group.unassigned { --group:#a8791f; --group-soft:#faf1df; border-style:dashed; border-left-style:solid; }
.fleet-group-head { display:flex; align-items:center; gap:12px; padding:11px 16px 11px 14px; background:linear-gradient(90deg,var(--group-soft),transparent 75%); border-bottom:1px solid var(--line); }
.group-icon { display:grid; place-items:center; flex:none; width:34px; height:34px; border-radius:9px; background:var(--group); color:#fff; }
.group-icon .nav-icon { width:18px; height:18px; }
.fleet-group-head > div { display:flex; flex-direction:column; min-width:0; }
.group-eyebrow { color:var(--group); font-size:.68rem; font-weight:700; letter-spacing:.08em; text-transform:uppercase; }
.fleet-group-head strong { font-size:1.04rem; letter-spacing:-.015em; overflow-wrap:anywhere; }
.fleet-group-head strong a { color:inherit; text-decoration:none; } .fleet-group-head strong a:hover { text-decoration:underline; }
.fleet-group-head p { display:flex; flex-wrap:wrap; justify-content:flex-end; gap:6px; margin:0 0 0 auto; }
.fleet-group-head p span { border-radius:20px; padding:2px 10px; background:var(--surface); border:1px solid var(--line); color:var(--subtle); font-size:.78rem; white-space:nowrap; }
.fleet-group-head p span:first-child { border-color:transparent; background:var(--group-soft); color:var(--group); font-weight:650; }
.fleet-group .fleet-table tbody tr:last-child td { border-bottom:0; }
.fleet-group tr.host { background:#fbfdfc; } .fleet-group tr.host .host-link { color:var(--ink); }
.link-source { display:block; color:var(--subtle); overflow-wrap:normal; }
.fleet-table col.host-col { width:15%; }
/* Guests hang on a branch below their host. */
.fleet-group tr.guest td.hostname { position:relative; padding-left:38px; }
.fleet-group tr.guest td.hostname::before { content:''; position:absolute; left:12px; top:0; bottom:50%; width:16px; border-left:2px solid var(--group); border-bottom:2px solid var(--group); border-bottom-left-radius:8px; opacity:.45; }
.fleet-group tr.guest:not(:last-child) td.hostname::after { content:''; position:absolute; left:12px; top:50%; bottom:0; border-left:2px solid var(--group); opacity:.45; }
.purpose-text { white-space:pre-wrap; overflow-wrap:anywhere; }
.fleet-table .purpose-text { display:-webkit-box; -webkit-box-orient:vertical; -webkit-line-clamp:3; overflow:hidden; }
.device-purpose textarea { font-family:inherit; }
@@ -167,6 +192,10 @@ ul.compact { margin:4px 0 0; padding-left:18px; font-size:.85rem; }
.fleet-table td,.fleet-table td:first-child,.fleet-table td:last-child {display:block;padding:0;border:0;font-size:.9rem;}
.fleet-table td[data-label]::before {content:attr(data-label);display:block;color:var(--subtle);font-size:.73rem;}
.fleet-table .hostname {grid-column:1/-1;padding-right:28px;} .host-link {font-size:1rem;}
.fleet-head {display:none;} .fleet-groups {padding:12px 10px;border-top:0;} .fleet-group .fleet-table tr:first-child {border-top:0;}
.fleet-group-head {flex-wrap:wrap;} .fleet-group-head p span {white-space:normal;} .fleet-group-head p {margin-left:46px;justify-content:flex-start;flex-basis:100%;}
.fleet-group tr.guest {margin-left:16px;border-left:2px solid color-mix(in srgb,var(--group) 45%,transparent);}
.fleet-group tr.guest td.hostname {padding-left:0;} .fleet-group tr.guest td.hostname::before,.fleet-group tr.guest td.hostname::after {display:none;}
.fleet-table .purpose-col {grid-column:1/-1;}
.fleet-table .target-col {position:absolute;right:14px;top:14px;width:auto;} .capacity-track {width:min(76px,100%);}
.list-footer {border-top:1px solid var(--line);flex-wrap:wrap;} .facts {grid-template-columns:1fr 1fr;gap:12px;}