- Weekly digest through the notification channels: alerts now, a week of uptime, what happened, disks heading for full, expiring secrets and domains, updates waiting, planned maintenance. Off until switched on in Settings -> Notifications; Preview and Send it now there. Setting key 'digest'. - Agents say their version in every report (Linux and Windows scripts are version 2); the server compares it with the scripts it serves and marks agents that are behind on the Servers, server and Updates pages. servers.agent_version, migration 0026. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
542 lines
26 KiB
TypeScript
542 lines
26 KiB
TypeScript
import { sql } from "drizzle-orm";
|
||
import { sqliteTable, text, integer, real, uniqueIndex, index } from "drizzle-orm/sqlite-core";
|
||
|
||
// ─── Users & roles ──────────────────────────────────────────────────────────
|
||
|
||
export const userRoles = ["admin", "operator", "viewer"] as const;
|
||
export type UserRole = (typeof userRoles)[number];
|
||
|
||
export const users = sqliteTable("users", {
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
oidcSub: text("oidc_sub").notNull().unique(),
|
||
email: text("email"),
|
||
name: text("name"),
|
||
role: text("role").$type<UserRole>().notNull().default("viewer"),
|
||
createdAt: text("created_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
lastLoginAt: text("last_login_at"),
|
||
// The pages this person pinned to the top of their menu, as a JSON array of paths ("/servers", "/alerts"). It follows them to any browser.
|
||
favorites: text("favorites"),
|
||
// When this person last pressed "Got it" on the dashboard summary of what changed; the next summary starts from here (ISO).
|
||
lastLookedAt: text("last_looked_at"),
|
||
// How this person arranged the dashboard: a JSON object with the cards they hid ("hidden": ["Gitea"]) and the order of the others
|
||
// ("order": {"DNS": 0, "Secrets": 1}). Follows them to any browser; null is the default layout.
|
||
dashboardLayout: text("dashboard_layout"),
|
||
});
|
||
|
||
// ─── Audit log ──────────────────────────────────────────────────────────────
|
||
|
||
export const auditLog = sqliteTable("audit_log", {
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
actorUserId: integer("actor_user_id").references(() => users.id, { onDelete: "set null" }),
|
||
actorLabel: text("actor_label"), // denormalized name/email snapshot, survives user deletion
|
||
category: text("category").notNull(), // 'secret' | 'ipam' | 'dns' | 'user' | 'integration' | 'task' | ...
|
||
action: text("action").notNull(), // 'create' | 'update' | 'delete' | 'start' | 'stop' | ...
|
||
targetType: text("target_type"),
|
||
targetId: text("target_id"),
|
||
detail: text("detail"), // JSON-encoded free-form context
|
||
createdAt: text("created_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
});
|
||
|
||
// ─── Diagnostic log — every outbound call to a DNS provider or integration ──
|
||
|
||
export const diagLog = sqliteTable("diag_log", {
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
source: text("source").notNull(), // e.g. 'cloudflare' | 'tailscale' | 'proxmox' | ...
|
||
operation: text("operation").notNull(), // adapter method name, e.g. 'listZones' | 'listDevices'
|
||
ok: integer("ok", { mode: "boolean" }).notNull(),
|
||
latencyMs: integer("latency_ms").notNull(),
|
||
error: text("error"),
|
||
createdAt: text("created_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
});
|
||
|
||
// ─── Notification queue — pending notifications held during quiet hours ────
|
||
|
||
export const notificationQueue = sqliteTable("notification_queue", {
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
title: text("title").notNull(),
|
||
message: text("message").notNull(),
|
||
createdAt: text("created_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
});
|
||
|
||
// ─── Maintenance windows — alerts about a target are silenced until endsAt ──
|
||
|
||
export const maintenanceTargetTypes = ["server", "integration", "dns_provider"] as const;
|
||
export type MaintenanceTargetType = (typeof maintenanceTargetTypes)[number];
|
||
|
||
export const maintenanceWindows = sqliteTable("maintenance_windows", {
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
targetType: text("target_type").$type<MaintenanceTargetType>().notNull(),
|
||
targetId: integer("target_id").notNull(), // polymorphic — no FK; a deleted target's window is simply ignored
|
||
reason: text("reason"),
|
||
startedAt: text("started_at").notNull(), // ISO
|
||
endsAt: text("ends_at").notNull(), // ISO — required: a forgotten open-ended window would silence real problems forever
|
||
createdBy: text("created_by"),
|
||
// Set on windows mirrored from another system (`kuma:<integration id>:<maintenance id>`), so a later sync can find and adjust them. NULL = started by hand.
|
||
source: text("source"),
|
||
});
|
||
|
||
// An address that Uptime Kuma's monitors use for a thing this app knows by another address (a Tailscale IP for a host added by its LAN
|
||
// address, say), so a maintenance window in Uptime Kuma lands on the right server, integration or DNS provider. The address is stored as a
|
||
// bare lowercase host.
|
||
export const maintenanceAliases = sqliteTable(
|
||
"maintenance_aliases",
|
||
{
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
address: text("address").notNull(),
|
||
targetType: text("target_type").$type<MaintenanceTargetType>().notNull(),
|
||
targetId: integer("target_id").notNull(), // polymorphic, like maintenance_windows — no FK; an alias whose target was deleted is ignored
|
||
createdAt: text("created_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
},
|
||
(table) => [uniqueIndex("maintenance_aliases_unique_idx").on(table.address, table.targetType, table.targetId)],
|
||
);
|
||
|
||
// A short history of each agent-reported server's load, memory and disk use, in percent — see services/serverMetrics.ts.
|
||
// `metric` is "cpu", "mem" or "disk:<mount point>".
|
||
export const serverMetrics = sqliteTable(
|
||
"server_metrics",
|
||
{
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
serverId: integer("server_id")
|
||
.notNull()
|
||
.references(() => servers.id, { onDelete: "cascade" }),
|
||
metric: text("metric").notNull(),
|
||
value: real("value").notNull(),
|
||
recordedAt: text("recorded_at").notNull(), // ISO
|
||
},
|
||
(table) => [index("server_metrics_lookup_idx").on(table.serverId, table.metric, table.recordedAt)],
|
||
);
|
||
|
||
// ─── Settings (key/value) ───────────────────────────────────────────────────
|
||
|
||
export const settings = sqliteTable("settings", {
|
||
key: text("key").primaryKey(),
|
||
value: text("value").notNull(),
|
||
updatedAt: text("updated_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
});
|
||
|
||
// ─── Secrets expiry tracker (ported from Sloth Manager) ────────────────────
|
||
|
||
export const secretTypes = ["api_token", "ssl_certificate", "password", "generic"] as const;
|
||
export type SecretType = (typeof secretTypes)[number];
|
||
|
||
export const secrets = sqliteTable("secrets", {
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
name: text("name").notNull(),
|
||
type: text("type").$type<SecretType>().notNull().default("generic"),
|
||
description: text("description"),
|
||
expiryDate: text("expiry_date"), // ISO date, e.g. 2026-03-01. NULL = never expires (no status, no reminders).
|
||
warnDays: integer("warn_days").notNull().default(30),
|
||
notes: text("notes"),
|
||
// ssl_certificate secrets only: when set, expiryDate is read from the live certificate on this host:port instead of typed in.
|
||
checkHost: text("check_host"),
|
||
checkPort: integer("check_port"),
|
||
lastCheckedAt: text("last_checked_at"),
|
||
lastCheckError: text("last_check_error"),
|
||
createdAt: text("created_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
updatedAt: text("updated_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
});
|
||
|
||
// ─── IPAM (ported from Sloth Manager) ───────────────────────────────────────
|
||
|
||
export const ipamEntries = sqliteTable("ipam_entries", {
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
ipAddress: text("ip_address").notNull().unique(),
|
||
label: text("label"),
|
||
vendor: text("vendor"),
|
||
location: text("location"),
|
||
notes: text("notes"),
|
||
source: text("source"), // null = entered manually; "tailscale" = auto-synced from a Tailscale integration
|
||
createdAt: text("created_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
updatedAt: text("updated_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
});
|
||
|
||
// ─── Integration credentials (encrypted API tokens) ─────────────────────────
|
||
|
||
export const integrationCredentials = sqliteTable("integration_credentials", {
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
name: text("name").notNull(),
|
||
encryptedSecret: text("encrypted_secret").notNull(), // AES-256-GCM, see src/crypto.ts
|
||
createdAt: text("created_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
});
|
||
|
||
// ─── DNS providers + record cache (ported from Sloth Manager) ──────────────
|
||
|
||
export const dnsProviderTypes = [
|
||
"cloudflare",
|
||
"loopia",
|
||
"pihole",
|
||
"azure",
|
||
"cpanel",
|
||
"technitium",
|
||
] as const;
|
||
export type DnsProviderType = (typeof dnsProviderTypes)[number];
|
||
|
||
export const dnsProviders = sqliteTable("dns_providers", {
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
providerType: text("provider_type").$type<DnsProviderType>().notNull(),
|
||
name: text("name").notNull(),
|
||
credentialId: integer("credential_id").references(() => integrationCredentials.id, {
|
||
onDelete: "set null",
|
||
}),
|
||
config: text("config"), // JSON: non-secret provider config (base URL, zone list, etc.)
|
||
enabled: integer("enabled", { mode: "boolean" }).notNull().default(true),
|
||
createdAt: text("created_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
});
|
||
|
||
export const dnsZonesCache = sqliteTable(
|
||
"dns_zones_cache",
|
||
{
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
providerId: integer("provider_id")
|
||
.notNull()
|
||
.references(() => dnsProviders.id, { onDelete: "cascade" }),
|
||
zoneId: text("zone_id").notNull(), // provider-specific zone identifier
|
||
zoneName: text("zone_name").notNull(),
|
||
syncedAt: text("synced_at"),
|
||
},
|
||
(table) => [uniqueIndex("dns_zones_cache_provider_zone_idx").on(table.providerId, table.zoneId)],
|
||
);
|
||
|
||
export const dnsRecordsCache = sqliteTable("dns_records_cache", {
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
providerId: integer("provider_id")
|
||
.notNull()
|
||
.references(() => dnsProviders.id, { onDelete: "cascade" }),
|
||
zoneId: text("zone_id").notNull(),
|
||
recordId: text("record_id").notNull(), // provider-specific record identifier
|
||
type: text("type").notNull(), // A, AAAA, CNAME, TXT, MX, ...
|
||
name: text("name").notNull(),
|
||
content: text("content").notNull(),
|
||
ttl: integer("ttl"),
|
||
priority: integer("priority"),
|
||
proxied: integer("proxied", { mode: "boolean" }),
|
||
});
|
||
|
||
// ─── Servers & scheduled tasks (ported from Schedule Task Manager) ─────────
|
||
|
||
export const proxmoxGuestTypes = ["qemu", "lxc"] as const;
|
||
export type ProxmoxGuestTypeCol = (typeof proxmoxGuestTypes)[number];
|
||
|
||
export const servers = sqliteTable("servers", {
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
name: text("name").notNull(),
|
||
hostname: text("hostname"),
|
||
osType: text("os_type").notNull().default("linux"),
|
||
description: text("description"),
|
||
apiTokenHash: text("api_token_hash").notNull(),
|
||
apiTokenPrefix: text("api_token_prefix").notNull(),
|
||
createdAt: text("created_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
lastSeenAt: text("last_seen_at"),
|
||
|
||
// Agent-reported hardware/network snapshot — updated on every agent report.
|
||
ipAddresses: text("ip_addresses"), // JSON string[]
|
||
cpuModel: text("cpu_model"),
|
||
cpuCores: integer("cpu_cores"),
|
||
cpuLoadPercent: real("cpu_load_percent"),
|
||
memTotalBytes: integer("mem_total_bytes"),
|
||
memUsedBytes: integer("mem_used_bytes"),
|
||
disks: text("disks"), // JSON string: {mount, sizeBytes, usedBytes}[]
|
||
listeningPorts: text("listening_ports"), // JSON string: {protocol, port, address, process}[] — what the agent sees bound on the host
|
||
lastPortScan: text("last_port_scan"), // JSON string: summary of the most recent network scan from this app
|
||
tags: text("tags"), // JSON string: string[] — free-form labels for grouping and filtering, normalized by services/serverTags
|
||
|
||
// Optional link to a Proxmox VM/LXC — set by an admin, not the agent. The "set null" below is what this file asks for,
|
||
// but migration 0001 created the column without it, so the database itself has no ON DELETE rule here: deleting an
|
||
// integration clears these columns in routes/integrations.ts instead. (See DATABASE.md.)
|
||
proxmoxIntegrationId: integer("proxmox_integration_id").references(() => integrations.id, {
|
||
onDelete: "set null",
|
||
}),
|
||
proxmoxNode: text("proxmox_node"),
|
||
proxmoxGuestType: text("proxmox_guest_type").$type<ProxmoxGuestTypeCol>(),
|
||
proxmoxVmid: integer("proxmox_vmid"),
|
||
|
||
// Not every server is a Proxmox guest (bare-metal boxes, other hosts) — an
|
||
// admin can hide the "Proxmox link" card on this server's detail page
|
||
// rather than seeing an irrelevant option on every server. Ignored (the
|
||
// card always shows) once a server IS actually linked, so unlinking stays
|
||
// reachable.
|
||
hideProxmoxLink: integer("hide_proxmox_link", { mode: "boolean" }).notNull().default(false),
|
||
|
||
// The server this one runs on (a VM on its host), set by an admin. Null means "work it out": a guest linked to a Proxmox node runs on
|
||
// the server named after that node. When the parent stops reporting, this server's own offline alert is folded under the parent's.
|
||
parentServerId: integer("parent_server_id"),
|
||
|
||
// What the agent says is waiting to be installed, as JSON: {manager, count, security, rebootRequired, reportedAt}. Null until it reports.
|
||
pendingUpdates: text("pending_updates"),
|
||
// When updates were first seen waiting (and none were before) — how long the oldest has been waiting. Null when none are.
|
||
updatesPendingSince: text("updates_pending_since"),
|
||
|
||
// Which version of the agent script is running on it, as the agent says in each report (see agent/*/report-tasks.*). Null until it says:
|
||
// an agent that predates the number being reported is version 1.
|
||
agentVersion: integer("agent_version"),
|
||
});
|
||
|
||
export const scheduledTasks = sqliteTable("scheduled_tasks", {
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
serverId: integer("server_id")
|
||
.notNull()
|
||
.references(() => servers.id, { onDelete: "cascade" }),
|
||
scheduleType: text("schedule_type").notNull(), // 'cron' | 'systemd_timer' | 'windows_task' | 'docker' | 'backup' | 'update' | 'n8n_workflow' | 'manual'
|
||
origin: text("origin").notNull().default("agent"), // 'agent' | 'manual' — manual rows are never touched by agent sync
|
||
name: text("name").notNull(),
|
||
command: text("command"),
|
||
scheduleExpression: text("schedule_expression"),
|
||
source: text("source"),
|
||
enabled: integer("enabled", { mode: "boolean" }).notNull().default(true),
|
||
nextRunAt: text("next_run_at"),
|
||
rawMetadata: text("raw_metadata"),
|
||
isStale: integer("is_stale", { mode: "boolean" }).notNull().default(false),
|
||
firstSeenAt: text("first_seen_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
lastSeenAt: text("last_seen_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
});
|
||
|
||
// ─── Server links — admin-page bookmarks per server (Dockge, Webmin, Cockpit, etc.) ─
|
||
|
||
export const serverLinks = sqliteTable("server_links", {
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
serverId: integer("server_id")
|
||
.notNull()
|
||
.references(() => servers.id, { onDelete: "cascade" }),
|
||
label: text("label").notNull(),
|
||
url: text("url").notNull(),
|
||
createdAt: text("created_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
});
|
||
|
||
// ─── Live integrations (Proxmox, Synology, Semaphore, Tailscale, Gitea, Dockhand) ─
|
||
|
||
export const integrationTypes = [
|
||
"proxmox",
|
||
"synology",
|
||
"semaphore",
|
||
"tailscale",
|
||
"gitea",
|
||
"dockhand",
|
||
"uptimekuma",
|
||
"phpipam",
|
||
"pbs",
|
||
"osticket",
|
||
] as const;
|
||
export type IntegrationType = (typeof integrationTypes)[number];
|
||
|
||
export const integrations = sqliteTable("integrations", {
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
type: text("type").$type<IntegrationType>().notNull(),
|
||
name: text("name").notNull(),
|
||
baseUrl: text("base_url").notNull(),
|
||
credentialId: integer("credential_id").references(() => integrationCredentials.id, {
|
||
onDelete: "set null",
|
||
}),
|
||
config: text("config"), // JSON: per-type non-secret config (tailnet name, node name, etc.)
|
||
enabled: integer("enabled", { mode: "boolean" }).notNull().default(true),
|
||
createdAt: text("created_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
});
|
||
|
||
// A port on a server that's either been seen open (by a scan or the agent) or that someone wrote a note about.
|
||
// Rows exist only while they carry information: an open port, or one with a label/comment ("reserved").
|
||
export const serverPorts = sqliteTable(
|
||
"server_ports",
|
||
{
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
serverId: integer("server_id")
|
||
.notNull()
|
||
.references(() => servers.id, { onDelete: "cascade" }),
|
||
port: integer("port").notNull(),
|
||
protocol: text("protocol").$type<"tcp" | "udp">().notNull().default("tcp"),
|
||
label: text("label"),
|
||
comment: text("comment"),
|
||
// True when the last network scan connected to it. The agent's view is stored on the server row instead.
|
||
open: integer("open", { mode: "boolean" }).notNull().default(false),
|
||
lastSeenOpenAt: text("last_seen_open_at"),
|
||
updatedAt: text("updated_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
},
|
||
(t) => [uniqueIndex("server_ports_unique").on(t.serverId, t.port, t.protocol)],
|
||
);
|
||
|
||
// A manually-recorded port opening on something this app doesn't monitor directly — a router's port forward, an
|
||
// edge firewall rule, a cloud provider's security group, etc. Distinct from serverPorts (which is what a server
|
||
// itself, or a scan of it, reports): this is what someone tells the app is open further out on the network path,
|
||
// for the same reason people keep a spreadsheet of "what did I open on the router and why."
|
||
export const portForwards = sqliteTable("port_forwards", {
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
label: text("label").notNull(),
|
||
externalPort: integer("external_port").notNull(),
|
||
protocol: text("protocol").$type<"tcp" | "udp">().notNull().default("tcp"),
|
||
// Optional link to a tracked server this forward points at; "destination" covers anything else (a bare IP,
|
||
// an untracked device) or extra detail alongside a linked server.
|
||
serverId: integer("server_id").references(() => servers.id, { onDelete: "set null" }),
|
||
destination: text("destination"),
|
||
// The port it's actually forwarded to, when NAT changes it (a router forwarding external 8443 to internal 443).
|
||
internalPort: integer("internal_port"),
|
||
// Free text: where this rule actually lives ("Home router", "OPNsense WAN rule", "Cloudflare Tunnel") — this
|
||
// app has no integration with any firewall/router, so it can't verify or manage the rule, only record it.
|
||
source: text("source"),
|
||
comment: text("comment"),
|
||
createdAt: text("created_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
updatedAt: text("updated_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
});
|
||
|
||
// ─── Domain registrations ───────────────────────────────────────────────────
|
||
|
||
export const domainOrigins = ["manual", "zone"] as const;
|
||
export type DomainOrigin = (typeof domainOrigins)[number];
|
||
|
||
// A registered domain whose expiry we track. "zone" rows are created from the DNS zones already synced from the
|
||
// providers (and removed again when the zone goes away); "manual" rows were typed in — for domains whose DNS lives elsewhere.
|
||
export const domains = sqliteTable("domains", {
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
name: text("name").notNull().unique(), // the registrable domain, lowercase ASCII
|
||
origin: text("origin").$type<DomainOrigin>().notNull().default("manual"),
|
||
expiresAt: text("expires_at"), // YYYY-MM-DD (UTC), as reported by the registry
|
||
registrar: text("registrar"),
|
||
lookupSource: text("lookup_source"), // "rdap" | "whois"
|
||
lastCheckedAt: text("last_checked_at"),
|
||
lastCheckedOkAt: text("last_checked_ok_at"),
|
||
lastCheckError: text("last_check_error"),
|
||
createdAt: text("created_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
});
|
||
|
||
// ─── Consistency report ─────────────────────────────────────────────────────
|
||
|
||
// Findings someone has looked at and decided are fine (a DNS record that intentionally points elsewhere, a Docker bridge
|
||
// address, ...). Keyed by the finding's stable key so it stays ignored across runs.
|
||
export const consistencyIgnores = sqliteTable("consistency_ignores", {
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
key: text("key").notNull().unique(),
|
||
title: text("title").notNull(), // what the finding said when it was ignored, so the list still makes sense once it's gone
|
||
reason: text("reason"),
|
||
createdBy: text("created_by"),
|
||
createdAt: text("created_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
});
|
||
|
||
// ─── Tag catalogue ──────────────────────────────────────────────────────────
|
||
|
||
// Tags themselves live on the servers that carry them (servers.tags). A row here adds two things a server can't: a tag
|
||
// that exists before anything uses it (so it's offered when tagging), and a chosen colour. A defined tag is kept until an
|
||
// admin deletes it, even with no server using it; a tag with no row is simply one that's in use and has an automatic colour.
|
||
export const tagDefinitions = sqliteTable("tag_definitions", {
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
name: text("name").notNull().unique(), // normalised, as in services/serverTags
|
||
color: text("color"), // "#rrggbb"; null = automatic
|
||
createdAt: text("created_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
});
|
||
|
||
// ─── Tickets opened for alerts ──────────────────────────────────────────────
|
||
|
||
export const alertTicketStatuses = ["pending", "open", "baseline"] as const;
|
||
export type AlertTicketStatus = (typeof alertTicketStatuses)[number];
|
||
|
||
// What the ticket feature remembers about each problem it has seen, so one problem opens one ticket however many checks see it.
|
||
// A row is one *occurrence*: when the alert clears and later returns, that is a new row. Alert ids (e.g. "disk:server:3:/") are the
|
||
// stable keys the Alerts page already uses, so they aren't a foreign key to anything.
|
||
export const alertTickets = sqliteTable(
|
||
"alert_tickets",
|
||
{
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
alertKey: text("alert_key").notNull(),
|
||
// pending = seen, but not yet in enough checks in a row to open a ticket (so a blip that fixes itself never does)
|
||
// open = a ticket was opened for it (ticket_number says which)
|
||
// baseline = it was already there when the feature was switched on and "existing problems" is set to be left alone
|
||
status: text("status").$type<AlertTicketStatus>().notNull(),
|
||
seenCount: integer("seen_count").notNull().default(1),
|
||
integrationId: integer("integration_id").references(() => integrations.id, { onDelete: "set null" }),
|
||
ticketNumber: text("ticket_number"), // osTicket's own ticket number; null unless status is "open"
|
||
grouped: integer("grouped", { mode: "boolean" }).notNull().default(false), // part of a summary ticket covering several alerts
|
||
severity: text("severity").notNull(),
|
||
message: text("message").notNull(), // what the alert said when it was first recorded
|
||
firstSeenAt: text("first_seen_at").notNull(), // ISO
|
||
lastSeenAt: text("last_seen_at").notNull(), // ISO
|
||
ticketedAt: text("ticketed_at"), // ISO; when the ticket was opened
|
||
clearedAt: text("cleared_at"), // ISO; null while the alert is still active
|
||
},
|
||
(t) => [index("alert_tickets_key_idx").on(t.alertKey)],
|
||
);
|
||
|
||
// ─── Share links: a read-only status board anyone with the address can open ─
|
||
|
||
export const shareLinkDetails = ["summary", "names"] as const;
|
||
export type ShareLinkDetail = (typeof shareLinkDetails)[number];
|
||
|
||
// A link to the wall display that needs no sign-in, for a TV in the hall or someone who only wants to know if things are up. Only a hash
|
||
// of the secret part of the address is kept (like an agent token), so it can't be read back out of the database or a backup.
|
||
export const shareLinks = sqliteTable("share_links", {
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
label: text("label").notNull(), // what it is for, e.g. "Hall TV"
|
||
tokenHash: text("token_hash").notNull().unique(),
|
||
tokenPrefix: text("token_prefix").notNull(), // the first characters, to tell links apart in the list
|
||
// summary = how things are, in numbers; names = also the names of what is wrong, in maintenance, and the integrations
|
||
detail: text("detail").$type<ShareLinkDetail>().notNull().default("summary"),
|
||
createdBy: text("created_by"),
|
||
createdAt: text("created_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
expiresAt: text("expires_at"), // ISO; null = never
|
||
lastUsedAt: text("last_used_at"), // ISO
|
||
});
|
||
|
||
// ─── Recurring maintenance ──────────────────────────────────────────────────
|
||
|
||
// "Every Sunday 02:00–04:00 on these servers": a schedule kept here, turned into ordinary maintenance windows a week ahead (each marked
|
||
// `schedule:<id>:<date>` in maintenance_windows.source). The windows are what silence alerts; the schedule only says when they come.
|
||
export const maintenanceSchedules = sqliteTable("maintenance_schedules", {
|
||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||
name: text("name").notNull(), // e.g. "Patch night"
|
||
reason: text("reason"),
|
||
// JSON: {type: "server" | "integration" | "dns_provider", id: number}[] — polymorphic, so no foreign key; a deleted target is skipped.
|
||
targets: text("targets").notNull(),
|
||
days: text("days").notNull(), // JSON: weekdays it happens on, 0 = Sunday … 6 = Saturday
|
||
startTime: text("start_time").notNull(), // "HH:MM" on the clock of `timezone`
|
||
durationMinutes: integer("duration_minutes").notNull(),
|
||
timezone: text("timezone").notNull(), // IANA name, so "02:00" stays 02:00 across summer and winter time
|
||
enabled: integer("enabled", { mode: "boolean" }).notNull().default(true),
|
||
createdBy: text("created_by"),
|
||
createdAt: text("created_at")
|
||
.notNull()
|
||
.default(sql`(current_timestamp)`),
|
||
});
|