/** * The network equipment read from UniFi controllers: devices with their state, address, uptime and what they are connected through, the networks and the Wi-Fi * names, and which servers are plugged into which device. Shared by the Network devices page and the read API. */ import { eq } from "drizzle-orm"; import { db } from "../db/client.js"; import { integrations, networkDevices, networkSegments, networkServerLinks, servers } from "../db/schema.js"; import { dependentsOf, loadGraph, needsOfNode, nodeKey } from "./dependencies.js"; import { activeSubjects } from "./maintenance.js"; import { syncStatuses } from "./unifiSync.js"; export async function buildNetworkOverview() { const [intRows, devices, segments, statuses, graph, silenced, linkRows, serverRows] = await Promise.all([ db.select({ id: integrations.id, name: integrations.name, enabled: integrations.enabled }).from(integrations).where(eq(integrations.type, "unifi")), db.select().from(networkDevices), db.select().from(networkSegments), syncStatuses(), loadGraph(), activeSubjects(), db.select().from(networkServerLinks), db.select({ id: servers.id, name: servers.name }).from(servers), ]); const serverName = new Map(serverRows.map((s) => [s.id, s.name])); const intName = new Map(intRows.map((i) => [i.id, i.name])); const byExternal = new Map(devices.map((d) => [`${d.integrationId}:${d.externalId}`, d])); // The servers plugged into each device, by what the controller says is connected (see unifiSync.matchClientsToServers). const plugged = new Map(); const serverLinks: { serverId: number; serverName: string; deviceId: number; deviceName: string; port: number | null; wired: boolean }[] = []; for (const l of linkRows) { const device = byExternal.get(`${l.integrationId}:${l.deviceExternalId}`); const name = serverName.get(l.serverId); if (!device || !name) continue; plugged.set(device.id, [...(plugged.get(device.id) ?? []), { serverId: l.serverId, serverName: name, port: l.port, wired: l.wired }]); serverLinks.push({ serverId: l.serverId, serverName: name, deviceId: device.id, deviceName: device.name, port: l.port, wired: l.wired }); } return { serverLinks, integrations: intRows.map((i) => ({ id: i.id, name: i.name, enabled: i.enabled, status: statuses[i.id] ?? null })), devices: devices .map((d) => { const key = nodeKey("device", d.id); const uplink = d.uplinkExternalId ? byExternal.get(`${d.integrationId}:${d.uplinkExternalId}`) : undefined; return { id: d.id, integrationId: d.integrationId, integrationName: intName.get(d.integrationId) ?? "UniFi", siteName: d.siteName, name: d.name, model: d.model, kind: d.kind, ip: d.ip, mac: d.mac, state: d.state, uptimeSeconds: d.uptimeSeconds, firmware: d.firmware, firmwareUpdatable: d.firmwareUpdatable, uplink: uplink ? { id: uplink.id, name: uplink.name } : null, offlineSince: d.offlineSince, lastOnlineAt: d.lastOnlineAt, updatedAt: d.updatedAt, // How many things depend on it (directly or through others), and how many it needs: what the dependency map says about it. dependents: dependentsOf(graph.edges, key).length, needs: needsOfNode(graph.edges, key).length, inMaintenance: silenced.has(key), servers: (plugged.get(d.id) ?? []).sort((a, b) => a.serverName.localeCompare(b.serverName)), }; }) .sort((a, b) => a.name.localeCompare(b.name)), networks: segments.filter((s) => s.kind === "network").map(({ kind: _k, ...s }) => ({ ...s, integrationName: intName.get(s.integrationId) ?? "UniFi" })).sort((a, b) => (a.vlanId ?? 0) - (b.vlanId ?? 0) || a.name.localeCompare(b.name)), ssids: segments.filter((s) => s.kind === "ssid").map(({ kind: _k, ...s }) => ({ ...s, integrationName: intName.get(s.integrationId) ?? "UniFi" })).sort((a, b) => a.name.localeCompare(b.name)), }; }