From 04eff67e568e3c9add355a6b828315749ecf7297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Untersch=C3=BCtz?= Date: Sun, 22 Nov 2026 11:36:14 +0100 Subject: [PATCH] feat(store): allow heartbeat monitors and find overdue ones Refs #27 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3 --- internal/migrations/sql/00028_heartbeats.sql | 13 +++ internal/store/heartbeats.sql.go | 89 ++++++++++++++++++++ internal/store/querier.go | 7 ++ internal/store/queries/heartbeats.sql | 16 ++++ 4 files changed, 125 insertions(+) create mode 100644 internal/migrations/sql/00028_heartbeats.sql create mode 100644 internal/store/heartbeats.sql.go create mode 100644 internal/store/queries/heartbeats.sql diff --git a/internal/migrations/sql/00028_heartbeats.sql b/internal/migrations/sql/00028_heartbeats.sql new file mode 100644 index 0000000..e69d0f0 --- /dev/null +++ b/internal/migrations/sql/00028_heartbeats.sql @@ -0,0 +1,13 @@ +-- +goose Up +-- Heartbeat monitors are pinged by the monitored job instead of being +-- checked. Their target is the secret token of the ping URL. +ALTER TABLE monitors DROP CONSTRAINT monitors_type_check; +ALTER TABLE monitors ADD CONSTRAINT monitors_type_check + CHECK (type IN ('http', 'tcp', 'icmp', 'dns', 'tls', 'heartbeat')); +CREATE UNIQUE INDEX monitors_heartbeat_token_idx ON monitors (target) WHERE type = 'heartbeat'; + +-- +goose Down +DROP INDEX monitors_heartbeat_token_idx; +DELETE FROM monitors WHERE type = 'heartbeat'; +ALTER TABLE monitors DROP CONSTRAINT monitors_type_check; +ALTER TABLE monitors ADD CONSTRAINT monitors_type_check CHECK (type IN ('http', 'tcp', 'icmp', 'dns', 'tls')); diff --git a/internal/store/heartbeats.sql.go b/internal/store/heartbeats.sql.go new file mode 100644 index 0000000..898b38a --- /dev/null +++ b/internal/store/heartbeats.sql.go @@ -0,0 +1,89 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.31.1 +// source: heartbeats.sql + +package store + +import ( + "context" + "time" + + "github.com/google/uuid" +) + +const heartbeatMonitor = `-- name: HeartbeatMonitor :one +SELECT id, org_id, interval_seconds, confirmations, enabled +FROM monitors WHERE type = 'heartbeat' AND target = $1 +` + +type HeartbeatMonitorRow struct { + ID uuid.UUID + OrgID uuid.UUID + IntervalSeconds int32 + Confirmations int32 + Enabled bool +} + +// System scope: the monitor a ping token belongs to. +func (q *Queries) HeartbeatMonitor(ctx context.Context, target string) (HeartbeatMonitorRow, error) { + row := q.db.QueryRow(ctx, heartbeatMonitor, target) + var i HeartbeatMonitorRow + err := row.Scan( + &i.ID, + &i.OrgID, + &i.IntervalSeconds, + &i.Confirmations, + &i.Enabled, + ) + return i, err +} + +const heartbeatsDue = `-- name: HeartbeatsDue :many +SELECT m.id, m.org_id, m.interval_seconds, m.confirmations, + coalesce((m.config->>'grace_seconds')::integer, 0)::integer AS grace_seconds, + greatest(m.updated_at, coalesce((SELECT max(r.time) FROM check_results r + WHERE r.org_id = m.org_id AND r.monitor_id = m.id AND r.time > now() - interval '3 days'), m.updated_at))::timestamptz AS waiting_since +FROM monitors m +WHERE m.type = 'heartbeat' AND m.enabled +` + +type HeartbeatsDueRow struct { + ID uuid.UUID + OrgID uuid.UUID + IntervalSeconds int32 + Confirmations int32 + GraceSeconds int32 + WaitingSince time.Time +} + +// System scope: enabled heartbeat monitors with their grace period and +// since when they are being waited for: the latest result, or the last +// change of the monitor, which covers new and re-enabled ones. The result +// lookup is bounded because the longest interval plus grace is two days. +func (q *Queries) HeartbeatsDue(ctx context.Context) ([]HeartbeatsDueRow, error) { + rows, err := q.db.Query(ctx, heartbeatsDue) + if err != nil { + return nil, err + } + defer rows.Close() + items := []HeartbeatsDueRow{} + for rows.Next() { + var i HeartbeatsDueRow + if err := rows.Scan( + &i.ID, + &i.OrgID, + &i.IntervalSeconds, + &i.Confirmations, + &i.GraceSeconds, + &i.WaitingSince, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/internal/store/querier.go b/internal/store/querier.go index 9861681..e447c32 100644 --- a/internal/store/querier.go +++ b/internal/store/querier.go @@ -103,6 +103,13 @@ type Querier interface { GetStatusPage(ctx context.Context, id uuid.UUID) (StatusPage, error) GetUser(ctx context.Context, id uuid.UUID) (User, error) GetUserByEmail(ctx context.Context, email string) (User, error) + // System scope: the monitor a ping token belongs to. + HeartbeatMonitor(ctx context.Context, target string) (HeartbeatMonitorRow, error) + // System scope: enabled heartbeat monitors with their grace period and + // since when they are being waited for: the latest result, or the last + // change of the monitor, which covers new and re-enabled ones. The result + // lookup is bounded because the longest interval plus grace is two days. + HeartbeatsDue(ctx context.Context) ([]HeartbeatsDueRow, error) // Per monitor and hour since $2, across all regions. HourlyBuckets(ctx context.Context, arg HourlyBucketsParams) ([]HourlyBucketsRow, error) IncidentsForMonitors(ctx context.Context, arg IncidentsForMonitorsParams) ([]Incident, error) diff --git a/internal/store/queries/heartbeats.sql b/internal/store/queries/heartbeats.sql new file mode 100644 index 0000000..fd27453 --- /dev/null +++ b/internal/store/queries/heartbeats.sql @@ -0,0 +1,16 @@ +-- name: HeartbeatMonitor :one +-- System scope: the monitor a ping token belongs to. +SELECT id, org_id, interval_seconds, confirmations, enabled +FROM monitors WHERE type = 'heartbeat' AND target = $1; + +-- name: HeartbeatsDue :many +-- System scope: enabled heartbeat monitors with their grace period and +-- since when they are being waited for: the latest result, or the last +-- change of the monitor, which covers new and re-enabled ones. The result +-- lookup is bounded because the longest interval plus grace is two days. +SELECT m.id, m.org_id, m.interval_seconds, m.confirmations, + coalesce((m.config->>'grace_seconds')::integer, 0)::integer AS grace_seconds, + greatest(m.updated_at, coalesce((SELECT max(r.time) FROM check_results r + WHERE r.org_id = m.org_id AND r.monitor_id = m.id AND r.time > now() - interval '3 days'), m.updated_at))::timestamptz AS waiting_since +FROM monitors m +WHERE m.type = 'heartbeat' AND m.enabled;