feat(sites): one install, many networks (v0.24.0-beta)

Closes #130. Multi-site, not multi-tenancy: a site is a named collection of
devices whose clients, events, topology and dashboards are viewed separately.
Sites separate what you see, not who you are — the admin/manage/read-only
roles are unchanged and every user sees every site. Per-site permissions
would have multiplied the work for a need nobody stated.

Two measurements decided the design.

Every device-derived table hangs off devices(id) — 25 of 50 tables, 35
foreign keys — so a single site_id column on devices scopes clients, events,
topology, backups and the rest transitively. No other table needed one.

The frontend has 162 useQuery call sites across 57 files. Threading a site
through each would be a large change that leaves a permanent trap: every
future query another chance to forget. The site is injected in the existing
axios interceptor instead, beside the auth token, so a new query is scoped by
default. Only 23 collection routes needed filtering; the 131 per-device
routes are already scoped by their device.

Existing installs are untouched. Migration creates one "Default Site", adopts
every device into it, and a single-site install never meets the concept. An
absent X-Site-Id header means unscoped, so existing API tokens and scripts
behave exactly as before.

Polling stays fleet-wide. Sites are a view over data already gathered, not a
change to how it is gathered — a device does not stop being monitored because
you are looking elsewhere. Rogue-AP detection scopes the scans but keeps the
"these are ours" BSSID set fleet-wide, or your own AP in another site reads
as an evil twin. Bulk actions do obey the selector: an operation that
silently spanned two customers is the blast radius the wave guards exist to
contain.

Four rounds of defects came out of testing the UI rather than the API, and
the first is the one worth recording. Switching sites called
queryClient.clear(), which empties the cache and reviews as correct. It
isn't: clear() destroys each query but a *mounted* observer keeps its own
reference and last result, so every rendered page went on showing the
previous site's data. The QueryClient is now keyed on the site, which
remounts the tree against an empty cache. Three tests pin the old behaviour
down so the shortcut cannot return.

The others: an unscoped fallback path behind the Connected Clients chart;
Config Health, TX retries and the activity feed reading fleet-wide; and
site_id added to the device list query but none of the other four that build
a device payload, so the Site field could not read its own value and appeared
to reject every change while the writes were landing. That last one is now
structural — DEVICE_BASE_COLUMNS is shared by all five, with a test that
fails when a device payload is hand-rolled, and which found two further
instances while being written.

Frontend tests were never run by CI despite vitest being configured and six
suites existing; they now run in both the workflow and ci-preflight.

Verified against a live four-device fleet: the migration left every figure
identical, an empty site returns zero from every read endpoint, moving a
device carries its history with it, and deleting a site with devices is
refused.

Minor bump rather than patch: sites change the shape of the product rather
than adding to it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2GT-Rich
2026-09-07 12:02:00 -07:00
co-authored by Claude Opus 5
parent e47819e232
commit 7b994ad935
50 changed files with 2074 additions and 151 deletions
+77 -28
View File
@@ -2,6 +2,8 @@ import { Router, Request, Response } from 'express';
import { getQueryApi, bucket } from '../config/influxdb';
import { query } from '../config/database';
import { requireAuth } from '../middleware/auth';
import { siteScopeDevices, siteScopeByDevice, siteScopeByNullableDevice, clientSeriesTag } from '../utils/siteScope';
import { activeSite } from '../middleware/site';
const router = Router();
router.use(requireAuth);
@@ -15,6 +17,26 @@ function rangeToFlux(range: string): string {
router.get('/clients-over-time', async (req: Request, res: Response) => {
const range = rangeToFlux(String(req.query.range || '24h'));
const queryApi = getQueryApi();
// Per-site deduplicated series when a site is selected, fleet-wide otherwise.
// Written by DeviceCollector alongside the global series (issue #130).
//
// A site holding the entire fleet is a special case worth taking: its total
// *is* the global total, by definition rather than by approximation. Using
// the global series there is not a shortcut -- it keeps the chart's full
// history, which the per-site series only accumulates from the upgrade
// onward. That covers every single-site install.
const siteId = activeSite(req);
let seriesTag = '_global';
if (siteId !== null) {
const counts = await query<{ in_site: number; total: number }>(
`SELECT COUNT(*) FILTER (WHERE site_id = $1)::int AS in_site,
COUNT(*)::int AS total
FROM devices`,
[siteId]
);
const { in_site = 0, total = 0 } = counts[0] ?? {};
seriesTag = clientSeriesTag(siteId, in_site, total);
}
// Use the global deduplicated metric (_global tag) written by DeviceCollector.
// This avoids double-counting clients seen by multiple devices simultaneously.
@@ -23,7 +45,7 @@ router.get('/clients-over-time', async (req: Request, res: Response) => {
|> range(start: -${range})
|> filter(fn: (r) => r._measurement == "client_counts")
|> filter(fn: (r) => r._field == "total_clients")
|> filter(fn: (r) => r.device_id == "_global")
|> filter(fn: (r) => r.device_id == "${seriesTag}")
|> aggregateWindow(every: 5m, fn: last, createEmpty: false)
|> yield(name: "clients_over_time")
`;
@@ -39,29 +61,48 @@ router.get('/clients-over-time', async (req: Request, res: Response) => {
// InfluxDB might not have data yet
}
// If no global metric exists yet (first run or old data), fall back to max across devices.
// max() is better than sum() since the device with the most clients is the gateway that
// sees everyone — summing would double-count clients visible from multiple devices.
// If no deduplicated series exists yet (first run, or a site whose history
// predates the per-site series), fall back to max across real devices. max()
// is better than sum() since the device with the most clients is the gateway
// that sees everyone — summing would double-count clients visible from
// multiple devices.
if (rawPoints.length === 0) {
const fallbackQuery = `
from(bucket: "${bucket}")
|> range(start: -${range})
|> filter(fn: (r) => r._measurement == "client_counts")
|> filter(fn: (r) => r._field == "total_clients")
|> filter(fn: (r) => r.device_id != "_global")
|> aggregateWindow(every: 5m, fn: max, createEmpty: false)
|> group()
|> aggregateWindow(every: 5m, fn: max, createEmpty: false)
|> yield(name: "clients_over_time_fallback")
`;
try {
await queryApi.collectRows(fallbackQuery, (row, tableMeta) => {
const time = tableMeta.get(row, '_time') as string;
const value = tableMeta.get(row, '_value') as number;
rawPoints.push({ time, value: Math.round(value) });
});
} catch {
// No data yet
// The synthetic series are tagged _global and _site_<id>; the leading
// underscore is what distinguishes them from real device ids. Matching on
// it matters: without it the fleet-wide fallback would fold the per-site
// totals back in and count everyone twice.
let deviceFilter: string | null = `|> filter(fn: (r) => not (r.device_id =~ /^_/))`;
if (siteId !== null) {
const ids = await query<{ id: number }>(
`SELECT id FROM devices WHERE site_id = $1`, [siteId]
);
// An empty site has no series to fall back to, and must show an empty
// chart rather than the fleet's (issue #130).
deviceFilter = ids.length
? `|> filter(fn: (r) => ${ids.map((d) => `r.device_id == "${d.id}"`).join(' or ')})`
: null;
}
if (deviceFilter) {
const fallbackQuery = `
from(bucket: "${bucket}")
|> range(start: -${range})
|> filter(fn: (r) => r._measurement == "client_counts")
|> filter(fn: (r) => r._field == "total_clients")
${deviceFilter}
|> aggregateWindow(every: 5m, fn: max, createEmpty: false)
|> group()
|> aggregateWindow(every: 5m, fn: max, createEmpty: false)
|> yield(name: "clients_over_time_fallback")
`;
try {
await queryApi.collectRows(fallbackQuery, (row, tableMeta) => {
const time = tableMeta.get(row, '_time') as string;
const value = tableMeta.get(row, '_value') as number;
rawPoints.push({ time, value: Math.round(value) });
});
} catch {
// No data yet
}
}
}
@@ -72,6 +113,7 @@ router.get('/clients-over-time', async (req: Request, res: Response) => {
// GET /api/metrics/top-clients?limit=10&range=24h
router.get('/top-clients', async (req: Request, res: Response) => {
const limit = Math.min(parseInt(String(req.query.limit || '10'), 10), 50);
const topFilter = siteScopeByDevice(activeSite(req), 'c.device_id');
// Use postgres data (most recent client data)
const clients = await query(
@@ -80,6 +122,7 @@ router.get('/top-clients', async (req: Request, res: Response) => {
c.tx_bytes, c.rx_bytes, c.client_type, d.name as device_name
FROM clients c JOIN devices d ON d.id = c.device_id
WHERE c.active = TRUE AND (c.tx_bytes + c.rx_bytes) > 0
${topFilter ? `AND ${topFilter}` : ''}
ORDER BY total_bytes DESC LIMIT $1`,
[limit]
);
@@ -308,30 +351,36 @@ router.get('/device/:deviceId/availability', async (req: Request, res: Response)
});
// GET /api/metrics/summary - dashboard summary stats
router.get('/summary', async (_req: Request, res: Response) => {
router.get('/summary', async (req: Request, res: Response) => {
const siteId = activeSite(req);
const devFilter = siteScopeDevices(siteId);
const cliFilter = siteScopeByDevice(siteId, 'device_id');
const evtFilter = siteScopeByNullableDevice(siteId, 'device_id');
const [deviceStats, clientStats, alertStats, availStats] = await Promise.all([
query<{ total: string; online: string; offline: string }>(
`SELECT
COUNT(*) as total,
COUNT(*) FILTER (WHERE status='online') as online,
COUNT(*) FILTER (WHERE status='offline') as offline
FROM devices`
FROM devices ${devFilter ? `WHERE ${devFilter}` : ''}`
),
query<{ total: string; active: string }>(
`SELECT COUNT(DISTINCT mac_address) as total,
COUNT(DISTINCT mac_address) FILTER (WHERE active=TRUE) as active
FROM clients`
FROM clients ${cliFilter ? `WHERE ${cliFilter}` : ''}`
),
query<{ critical: string; warning: string }>(
`SELECT
COUNT(*) FILTER (WHERE severity='error') as critical,
COUNT(*) FILTER (WHERE severity='warning') as warning
FROM events WHERE event_time > NOW() - INTERVAL '24 hours'`
FROM events WHERE event_time > NOW() - INTERVAL '24 hours'
${evtFilter ? `AND ${evtFilter}` : ''}`
),
query<{ total_outage_sec: string }>(
`SELECT COALESCE(SUM(COALESCE(duration_seconds, 0)), 0)::text AS total_outage_sec
FROM device_availability
WHERE went_offline_at > NOW() - INTERVAL '30 days'`
WHERE went_offline_at > NOW() - INTERVAL '30 days'
${cliFilter ? `AND ${cliFilter}` : ''}`
),
]);