feat(store): keep check evidence apart from results
Refs #79 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
78cd911008
commit
8f5eb5d075
5 files changed
+238
No files matched your search
@@ -0,0 +1,29 @@
|
|||||||
|
-- +goose Up
|
||||||
|
-- What checkers saw of HTTP exchanges (see check.Evidence). Kept apart
|
||||||
|
-- from check_results, which is compressed and holds one small row per
|
||||||
|
-- check; evidence is sampled and large. Per monitor and region the API
|
||||||
|
-- keeps the latest success and failures at most every few minutes.
|
||||||
|
CREATE TABLE check_evidence (
|
||||||
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
org_id uuid NOT NULL,
|
||||||
|
monitor_id uuid NOT NULL,
|
||||||
|
region text NOT NULL,
|
||||||
|
checker_id uuid NOT NULL,
|
||||||
|
time timestamptz NOT NULL,
|
||||||
|
ok boolean NOT NULL,
|
||||||
|
status_code integer,
|
||||||
|
error text,
|
||||||
|
evidence jsonb NOT NULL,
|
||||||
|
FOREIGN KEY (monitor_id, org_id) REFERENCES monitors (id, org_id) ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
CREATE INDEX check_evidence_monitor_idx ON check_evidence (monitor_id, region, time DESC);
|
||||||
|
CREATE INDEX check_evidence_time_idx ON check_evidence (time);
|
||||||
|
|
||||||
|
ALTER TABLE check_evidence ENABLE ROW LEVEL SECURITY;
|
||||||
|
ALTER TABLE check_evidence FORCE ROW LEVEL SECURITY;
|
||||||
|
CREATE POLICY check_evidence_tenant ON check_evidence FOR ALL
|
||||||
|
USING (app_scope_system() OR org_id = app_org_id())
|
||||||
|
WITH CHECK (app_scope_system() OR org_id = app_org_id());
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
DROP TABLE check_evidence;
|
||||||
@@ -0,0 +1,166 @@
|
|||||||
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// sqlc v1.31.1
|
||||||
|
// source: evidence.sql
|
||||||
|
|
||||||
|
package store
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
const deleteSuccessEvidence = `-- name: DeleteSuccessEvidence :exec
|
||||||
|
DELETE FROM check_evidence WHERE monitor_id = $1 AND region = $2 AND ok
|
||||||
|
`
|
||||||
|
|
||||||
|
type DeleteSuccessEvidenceParams struct {
|
||||||
|
MonitorID uuid.UUID
|
||||||
|
Region string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only the latest success per monitor and region is kept.
|
||||||
|
func (q *Queries) DeleteSuccessEvidence(ctx context.Context, arg DeleteSuccessEvidenceParams) error {
|
||||||
|
_, err := q.db.Exec(ctx, deleteSuccessEvidence, arg.MonitorID, arg.Region)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const getEvidence = `-- name: GetEvidence :one
|
||||||
|
SELECT id, org_id, monitor_id, region, checker_id, time, ok, status_code, error, evidence FROM check_evidence WHERE id = $1 AND monitor_id = $2
|
||||||
|
`
|
||||||
|
|
||||||
|
type GetEvidenceParams struct {
|
||||||
|
ID uuid.UUID
|
||||||
|
MonitorID uuid.UUID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) GetEvidence(ctx context.Context, arg GetEvidenceParams) (CheckEvidence, error) {
|
||||||
|
row := q.db.QueryRow(ctx, getEvidence, arg.ID, arg.MonitorID)
|
||||||
|
var i CheckEvidence
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.OrgID,
|
||||||
|
&i.MonitorID,
|
||||||
|
&i.Region,
|
||||||
|
&i.CheckerID,
|
||||||
|
&i.Time,
|
||||||
|
&i.Ok,
|
||||||
|
&i.StatusCode,
|
||||||
|
&i.Error,
|
||||||
|
&i.Evidence,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const insertEvidence = `-- name: InsertEvidence :exec
|
||||||
|
INSERT INTO check_evidence (org_id, monitor_id, region, checker_id, time, ok, status_code, error, evidence)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||||
|
`
|
||||||
|
|
||||||
|
type InsertEvidenceParams struct {
|
||||||
|
OrgID uuid.UUID
|
||||||
|
MonitorID uuid.UUID
|
||||||
|
Region string
|
||||||
|
CheckerID uuid.UUID
|
||||||
|
Time time.Time
|
||||||
|
Ok bool
|
||||||
|
StatusCode *int32
|
||||||
|
Error *string
|
||||||
|
Evidence []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) InsertEvidence(ctx context.Context, arg InsertEvidenceParams) error {
|
||||||
|
_, err := q.db.Exec(ctx, insertEvidence,
|
||||||
|
arg.OrgID,
|
||||||
|
arg.MonitorID,
|
||||||
|
arg.Region,
|
||||||
|
arg.CheckerID,
|
||||||
|
arg.Time,
|
||||||
|
arg.Ok,
|
||||||
|
arg.StatusCode,
|
||||||
|
arg.Error,
|
||||||
|
arg.Evidence,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const lastFailureEvidence = `-- name: LastFailureEvidence :one
|
||||||
|
SELECT time, coalesce(error, '')::text AS error FROM check_evidence
|
||||||
|
WHERE monitor_id = $1 AND region = $2 AND NOT ok
|
||||||
|
ORDER BY time DESC LIMIT 1
|
||||||
|
`
|
||||||
|
|
||||||
|
type LastFailureEvidenceParams struct {
|
||||||
|
MonitorID uuid.UUID
|
||||||
|
Region string
|
||||||
|
}
|
||||||
|
|
||||||
|
type LastFailureEvidenceRow struct {
|
||||||
|
Time time.Time
|
||||||
|
Error string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) LastFailureEvidence(ctx context.Context, arg LastFailureEvidenceParams) (LastFailureEvidenceRow, error) {
|
||||||
|
row := q.db.QueryRow(ctx, lastFailureEvidence, arg.MonitorID, arg.Region)
|
||||||
|
var i LastFailureEvidenceRow
|
||||||
|
err := row.Scan(&i.Time, &i.Error)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const listEvidence = `-- name: ListEvidence :many
|
||||||
|
SELECT id, region, checker_id, time, ok, status_code, error FROM check_evidence
|
||||||
|
WHERE monitor_id = $1
|
||||||
|
ORDER BY time DESC LIMIT 100
|
||||||
|
`
|
||||||
|
|
||||||
|
type ListEvidenceRow struct {
|
||||||
|
ID uuid.UUID
|
||||||
|
Region string
|
||||||
|
CheckerID uuid.UUID
|
||||||
|
Time time.Time
|
||||||
|
Ok bool
|
||||||
|
StatusCode *int32
|
||||||
|
Error *string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) ListEvidence(ctx context.Context, monitorID uuid.UUID) ([]ListEvidenceRow, error) {
|
||||||
|
rows, err := q.db.Query(ctx, listEvidence, monitorID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []ListEvidenceRow{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i ListEvidenceRow
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.Region,
|
||||||
|
&i.CheckerID,
|
||||||
|
&i.Time,
|
||||||
|
&i.Ok,
|
||||||
|
&i.StatusCode,
|
||||||
|
&i.Error,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const purgeEvidence = `-- name: PurgeEvidence :execrows
|
||||||
|
DELETE FROM check_evidence WHERE time < $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) PurgeEvidence(ctx context.Context, argTime time.Time) (int64, error) {
|
||||||
|
result, err := q.db.Exec(ctx, purgeEvidence, argTime)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return result.RowsAffected(), nil
|
||||||
|
}
|
||||||
@@ -60,6 +60,19 @@ type AuditEvent struct {
|
|||||||
RequestID *string
|
RequestID *string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CheckEvidence struct {
|
||||||
|
ID uuid.UUID
|
||||||
|
OrgID uuid.UUID
|
||||||
|
MonitorID uuid.UUID
|
||||||
|
Region string
|
||||||
|
CheckerID uuid.UUID
|
||||||
|
Time time.Time
|
||||||
|
Ok bool
|
||||||
|
StatusCode *int32
|
||||||
|
Error *string
|
||||||
|
Evidence []byte
|
||||||
|
}
|
||||||
|
|
||||||
type CheckResult struct {
|
type CheckResult struct {
|
||||||
Time time.Time
|
Time time.Time
|
||||||
OrgID uuid.UUID
|
OrgID uuid.UUID
|
||||||
|
|||||||
@@ -65,6 +65,8 @@ type Querier interface {
|
|||||||
DeleteRole(ctx context.Context, id uuid.UUID) (int64, error)
|
DeleteRole(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
DeleteSSOProvider(ctx context.Context, id uuid.UUID) (int64, error)
|
DeleteSSOProvider(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
DeleteStatusPage(ctx context.Context, id uuid.UUID) (int64, error)
|
DeleteStatusPage(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
|
// Only the latest success per monitor and region is kept.
|
||||||
|
DeleteSuccessEvidence(ctx context.Context, arg DeleteSuccessEvidenceParams) error
|
||||||
DeleteUser(ctx context.Context, id uuid.UUID) (int64, error)
|
DeleteUser(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
DisableTOTP(ctx context.Context, id uuid.UUID) error
|
DisableTOTP(ctx context.Context, id uuid.UUID) error
|
||||||
EnableTOTP(ctx context.Context, arg EnableTOTPParams) error
|
EnableTOTP(ctx context.Context, arg EnableTOTPParams) error
|
||||||
@@ -88,6 +90,7 @@ type Querier interface {
|
|||||||
GetAgentByHash(ctx context.Context, tokenHash []byte) (GetAgentByHashRow, error)
|
GetAgentByHash(ctx context.Context, tokenHash []byte) (GetAgentByHashRow, error)
|
||||||
GetAssignment(ctx context.Context, id uuid.UUID) (RoleAssignment, error)
|
GetAssignment(ctx context.Context, id uuid.UUID) (RoleAssignment, error)
|
||||||
GetChannel(ctx context.Context, id uuid.UUID) (NotificationChannel, error)
|
GetChannel(ctx context.Context, id uuid.UUID) (NotificationChannel, error)
|
||||||
|
GetEvidence(ctx context.Context, arg GetEvidenceParams) (CheckEvidence, error)
|
||||||
GetGroup(ctx context.Context, id uuid.UUID) (MonitorGroup, error)
|
GetGroup(ctx context.Context, id uuid.UUID) (MonitorGroup, error)
|
||||||
GetIdentity(ctx context.Context, arg GetIdentityParams) (UserIdentity, error)
|
GetIdentity(ctx context.Context, arg GetIdentityParams) (UserIdentity, error)
|
||||||
GetInvitation(ctx context.Context, id uuid.UUID) (Invitation, error)
|
GetInvitation(ctx context.Context, id uuid.UUID) (Invitation, error)
|
||||||
@@ -119,9 +122,11 @@ type Querier interface {
|
|||||||
// check_results and its aggregates have no row-level security (see
|
// check_results and its aggregates have no row-level security (see
|
||||||
// migration 00013). Every query here must filter by org_id.
|
// migration 00013). Every query here must filter by org_id.
|
||||||
InsertCheckResults(ctx context.Context, arg []InsertCheckResultsParams) (int64, error)
|
InsertCheckResults(ctx context.Context, arg []InsertCheckResultsParams) (int64, error)
|
||||||
|
InsertEvidence(ctx context.Context, arg InsertEvidenceParams) error
|
||||||
InsertRecoveryCodes(ctx context.Context, arg []InsertRecoveryCodesParams) (int64, error)
|
InsertRecoveryCodes(ctx context.Context, arg []InsertRecoveryCodesParams) (int64, error)
|
||||||
InvitationOrg(ctx context.Context, hash []byte) (uuid.UUID, error)
|
InvitationOrg(ctx context.Context, hash []byte) (uuid.UUID, error)
|
||||||
IsMember(ctx context.Context, arg IsMemberParams) (bool, error)
|
IsMember(ctx context.Context, arg IsMemberParams) (bool, error)
|
||||||
|
LastFailureEvidence(ctx context.Context, arg LastFailureEvidenceParams) (LastFailureEvidenceRow, error)
|
||||||
// Newest result per monitor and region in the org.
|
// Newest result per monitor and region in the org.
|
||||||
LatestResults(ctx context.Context, orgID uuid.UUID) ([]LatestResultsRow, error)
|
LatestResults(ctx context.Context, orgID uuid.UUID) ([]LatestResultsRow, error)
|
||||||
LatestResultsForMonitor(ctx context.Context, arg LatestResultsForMonitorParams) ([]LatestResultsForMonitorRow, error)
|
LatestResultsForMonitor(ctx context.Context, arg LatestResultsForMonitorParams) ([]LatestResultsForMonitorRow, error)
|
||||||
@@ -140,6 +145,7 @@ type Querier interface {
|
|||||||
// schedule.
|
// schedule.
|
||||||
ListEnabledMonitorsForSchedule(ctx context.Context) ([]ListEnabledMonitorsForScheduleRow, error)
|
ListEnabledMonitorsForSchedule(ctx context.Context) ([]ListEnabledMonitorsForScheduleRow, error)
|
||||||
ListEnabledSSOProviders(ctx context.Context) ([]ListEnabledSSOProvidersRow, error)
|
ListEnabledSSOProviders(ctx context.Context) ([]ListEnabledSSOProvidersRow, error)
|
||||||
|
ListEvidence(ctx context.Context, monitorID uuid.UUID) ([]ListEvidenceRow, error)
|
||||||
ListGroups(ctx context.Context) ([]MonitorGroup, error)
|
ListGroups(ctx context.Context) ([]MonitorGroup, error)
|
||||||
ListIncidents(ctx context.Context) ([]ListIncidentsRow, error)
|
ListIncidents(ctx context.Context) ([]ListIncidentsRow, error)
|
||||||
ListMaintenance(ctx context.Context) ([]MaintenanceWindow, error)
|
ListMaintenance(ctx context.Context) ([]MaintenanceWindow, error)
|
||||||
@@ -189,6 +195,7 @@ type Querier interface {
|
|||||||
// Organizations user owns, with their member count.
|
// Organizations user owns, with their member count.
|
||||||
OwnedOrgs(ctx context.Context, userID uuid.UUID) ([]OwnedOrgsRow, error)
|
OwnedOrgs(ctx context.Context, userID uuid.UUID) ([]OwnedOrgsRow, error)
|
||||||
PurgeAuditEvents(ctx context.Context, argTime time.Time) (int64, error)
|
PurgeAuditEvents(ctx context.Context, argTime time.Time) (int64, error)
|
||||||
|
PurgeEvidence(ctx context.Context, argTime time.Time) (int64, error)
|
||||||
PurgeExpiredTokens(ctx context.Context, expiresAt *time.Time) (int64, error)
|
PurgeExpiredTokens(ctx context.Context, expiresAt *time.Time) (int64, error)
|
||||||
// Incidents resolved before the oldest data still charted.
|
// Incidents resolved before the oldest data still charted.
|
||||||
PurgeIncidents(ctx context.Context, resolvedAt *time.Time) (int64, error)
|
PurgeIncidents(ctx context.Context, resolvedAt *time.Time) (int64, error)
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
-- name: InsertEvidence :exec
|
||||||
|
INSERT INTO check_evidence (org_id, monitor_id, region, checker_id, time, ok, status_code, error, evidence)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9);
|
||||||
|
|
||||||
|
-- name: DeleteSuccessEvidence :exec
|
||||||
|
-- Only the latest success per monitor and region is kept.
|
||||||
|
DELETE FROM check_evidence WHERE monitor_id = $1 AND region = $2 AND ok;
|
||||||
|
|
||||||
|
-- name: LastFailureEvidence :one
|
||||||
|
SELECT time, coalesce(error, '')::text AS error FROM check_evidence
|
||||||
|
WHERE monitor_id = $1 AND region = $2 AND NOT ok
|
||||||
|
ORDER BY time DESC LIMIT 1;
|
||||||
|
|
||||||
|
-- name: ListEvidence :many
|
||||||
|
SELECT id, region, checker_id, time, ok, status_code, error FROM check_evidence
|
||||||
|
WHERE monitor_id = $1
|
||||||
|
ORDER BY time DESC LIMIT 100;
|
||||||
|
|
||||||
|
-- name: GetEvidence :one
|
||||||
|
SELECT * FROM check_evidence WHERE id = $1 AND monitor_id = $2;
|
||||||
|
|
||||||
|
-- name: PurgeEvidence :execrows
|
||||||
|
DELETE FROM check_evidence WHERE time < $1;
|
||||||
Reference in new issue
Block a user