ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 13m0s
ci / test (pull_request) Successful in 13m56s
ci / build (pull_request) Successful in 19m56s
ci / image (pull_request) Successful in 5m50s
Checkers send evidence of healthy checks every 10 minutes, first with the first result after they start. When checkers restart together, as after a self-update, every monitor's evidence came in the same minute, every 10 minutes. The API stored each in a transaction of its own while the checker waited, reports took up to 36 seconds, the checkers' slots filled and runs piled up: the overdue waves seen in prod. Evidence sent for another reason than the period now puts the next periodic one at a random point of the period, and the API writes all of a report's evidence in one transaction, with one query each for the successes to replace and the failures last kept. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
245 lines
5.6 KiB
Go
245 lines
5.6 KiB
Go
// 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 region = $1 AND ok AND monitor_id = ANY($2::uuid[])
|
|
`
|
|
|
|
type DeleteSuccessEvidenceParams struct {
|
|
Region string
|
|
MonitorIds []uuid.UUID
|
|
}
|
|
|
|
// 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.Region, arg.MonitorIds)
|
|
return err
|
|
}
|
|
|
|
const evidenceBetween = `-- name: EvidenceBetween :many
|
|
SELECT id, region, time FROM check_evidence
|
|
WHERE monitor_id = $1 AND time BETWEEN $2 AND $3
|
|
`
|
|
|
|
type EvidenceBetweenParams struct {
|
|
MonitorID uuid.UUID
|
|
Since time.Time
|
|
Until time.Time
|
|
}
|
|
|
|
type EvidenceBetweenRow struct {
|
|
ID uuid.UUID
|
|
Region string
|
|
Time time.Time
|
|
}
|
|
|
|
// Evidence of a monitor in a time span, to link results to it.
|
|
func (q *Queries) EvidenceBetween(ctx context.Context, arg EvidenceBetweenParams) ([]EvidenceBetweenRow, error) {
|
|
rows, err := q.db.Query(ctx, evidenceBetween, arg.MonitorID, arg.Since, arg.Until)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []EvidenceBetweenRow{}
|
|
for rows.Next() {
|
|
var i EvidenceBetweenRow
|
|
if err := rows.Scan(&i.ID, &i.Region, &i.Time); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
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 :many
|
|
SELECT DISTINCT ON (monitor_id) monitor_id, time, coalesce(error, '')::text AS error
|
|
FROM check_evidence
|
|
WHERE region = $1 AND NOT ok AND monitor_id = ANY($2::uuid[])
|
|
ORDER BY monitor_id, time DESC
|
|
`
|
|
|
|
type LastFailureEvidenceParams struct {
|
|
Region string
|
|
MonitorIds []uuid.UUID
|
|
}
|
|
|
|
type LastFailureEvidenceRow struct {
|
|
MonitorID uuid.UUID
|
|
Time time.Time
|
|
Error string
|
|
}
|
|
|
|
// The newest failure per monitor in region.
|
|
func (q *Queries) LastFailureEvidence(ctx context.Context, arg LastFailureEvidenceParams) ([]LastFailureEvidenceRow, error) {
|
|
rows, err := q.db.Query(ctx, lastFailureEvidence, arg.Region, arg.MonitorIds)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []LastFailureEvidenceRow{}
|
|
for rows.Next() {
|
|
var i LastFailureEvidenceRow
|
|
if err := rows.Scan(&i.MonitorID, &i.Time, &i.Error); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listEvidence = `-- name: ListEvidence :many
|
|
SELECT id, region, checker_id, time, ok, status_code, error FROM check_evidence
|
|
WHERE monitor_id = $1
|
|
AND ($2::timestamptz IS NULL OR (time, id) < ($2, $3::uuid))
|
|
AND ($4::timestamptz IS NULL OR (time, id) > ($4, $5::uuid))
|
|
ORDER BY
|
|
CASE WHEN $4::timestamptz IS NULL THEN time END DESC,
|
|
CASE WHEN $4::timestamptz IS NULL THEN id END DESC,
|
|
time, id
|
|
LIMIT $6
|
|
`
|
|
|
|
type ListEvidenceParams struct {
|
|
MonitorID uuid.UUID
|
|
AfterT *time.Time
|
|
AfterID *uuid.UUID
|
|
BeforeT *time.Time
|
|
BeforeID *uuid.UUID
|
|
MaxRows int32
|
|
}
|
|
|
|
type ListEvidenceRow struct {
|
|
ID uuid.UUID
|
|
Region string
|
|
CheckerID uuid.UUID
|
|
Time time.Time
|
|
Ok bool
|
|
StatusCode *int32
|
|
Error *string
|
|
}
|
|
|
|
// Newest first, a page from a keyset cursor (see paging.go).
|
|
func (q *Queries) ListEvidence(ctx context.Context, arg ListEvidenceParams) ([]ListEvidenceRow, error) {
|
|
rows, err := q.db.Query(ctx, listEvidence,
|
|
arg.MonitorID,
|
|
arg.AfterT,
|
|
arg.AfterID,
|
|
arg.BeforeT,
|
|
arg.BeforeID,
|
|
arg.MaxRows,
|
|
)
|
|
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
|
|
}
|