History: server load, memory and disks are also kept as hourly (four months) and daily (over two years) averages with lowest and highest; endpoint answers as hourly (four months) beside the daily figures. Charts ask for 24 hours to a year and are drawn from whichever is fine enough. A disk that two weeks call flat is looked at again over ninety days for the forecast. Built once from what is kept. One chart: TimeChart replaces the sparkline and the multi-line chart: hover and arrow-key readout, a lowest/highest band, gaps shown as gaps, the same ranges everywhere and the choice remembered. Used for server trends, comparing servers, a check's availability and answer times (Endpoints > History) and a server's availability. Read API: /api/v1 (servers, metrics, alerts, incidents, inventory, network, endpoints and their history, maintenance) with personal read-only tokens made under the user menu: shown once, hashed, 30/90/365 days or none, ten at most, removed with their owner. GET only. Documented in API.md. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
73 lines
4.1 KiB
TypeScript
73 lines
4.1 KiB
TypeScript
/**
|
|
* 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<number, { serverId: number; serverName: string; port: number | null; wired: boolean }[]>();
|
|
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)),
|
|
};
|
|
}
|