Files
geo/server/migrations/20260424130000_add_desktop_primary_leases.up.sql
T
root ff86933c24 feat(monitoring): generate daily tasks and optimize heartbeat writes
Adds a MonitoringDailyTaskWorker that materializes and dispatches
daily collect tasks from active subscription/desktop-client state,
with per-run atomic metrics and a Prometheus collector. Dashboard
and question-detail APIs now surface a platform_authorization_status
so the UI can distinguish no-desktop-client, no-authorized-platforms,
and authorized states; quota/access-state lookups resolve by request
workspace instead of tenant.

Hardens heartbeat: a desktop_client_primary_leases table gives
per-(tenant, workspace) sticky primary selection, read-mostly lease
resolution avoids a per-heartbeat transaction, and unchanged platform
access reports skip snapshot upserts outside a 10-minute refresh
window. Adds heartbeat primary/snapshot metrics exposed via
token-protected tenant-api /api/internal/metrics endpoints plus
Prometheus. Monitoring quota seed is removed from dev-seed since
daily plans now derive from desktop bindings, and the projection
uses the canonical six-platform catalog so platforms with zero
samples still render.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-24 22:19:57 +08:00

53 lines
2.0 KiB
SQL

-- Backfilled primary leases use the same 90s TTL as
-- server/internal/tenant/app/desktop_presence.go:desktopClientPresenceTTL.
-- Keep these values aligned; desktop heartbeat interval must be < TTL/2 so
-- a single missed heartbeat does not let another client steal primary.
CREATE TABLE IF NOT EXISTS desktop_client_primary_leases (
tenant_id BIGINT NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
workspace_id BIGINT NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
primary_client_id UUID NOT NULL REFERENCES desktop_clients(id) ON DELETE CASCADE,
lease_expires_at TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
PRIMARY KEY (tenant_id, workspace_id)
);
CREATE INDEX IF NOT EXISTS idx_desktop_client_primary_leases_primary_client
ON desktop_client_primary_leases (primary_client_id);
CREATE INDEX IF NOT EXISTS idx_desktop_clients_tenant_workspace_seen_active
ON desktop_clients (tenant_id, workspace_id, last_seen_at DESC, created_at DESC, id DESC)
WHERE revoked_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_desktop_clients_tenant_workspace_user_seen_active
ON desktop_clients (tenant_id, workspace_id, user_id, last_seen_at DESC, created_at DESC, id DESC)
WHERE revoked_at IS NULL;
INSERT INTO desktop_client_primary_leases (
tenant_id,
workspace_id,
primary_client_id,
lease_expires_at
)
SELECT
tenant_id,
workspace_id,
id,
COALESCE(last_seen_at, created_at) + INTERVAL '90 seconds'
FROM (
SELECT
dc.id,
dc.tenant_id,
dc.workspace_id,
dc.created_at,
dc.last_seen_at,
ROW_NUMBER() OVER (
PARTITION BY dc.tenant_id, dc.workspace_id
ORDER BY dc.last_seen_at DESC NULLS LAST, dc.created_at DESC, dc.id DESC
) AS rn
FROM desktop_clients dc
WHERE dc.revoked_at IS NULL
) ranked_clients
WHERE rn = 1
ON CONFLICT (tenant_id, workspace_id) DO NOTHING;