feat(store): add monitors, regions, agents and check result hypertable
check_results has no row-level security because TimescaleDB refuses compression and continuous aggregates on such tables. Every results query takes org_id as a parameter, which a test enforces. Refs #10, #13 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
55559c8b0a
commit
00658d49a2
12 files changed
+1093
No files matched your search
@@ -0,0 +1,161 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: check_results.sql
|
||||
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type InsertCheckResultsParams struct {
|
||||
Time time.Time
|
||||
OrgID uuid.UUID
|
||||
MonitorID uuid.UUID
|
||||
Region string
|
||||
CheckerID uuid.UUID
|
||||
Ok bool
|
||||
LatencyMs *int32
|
||||
StatusCode *int32
|
||||
Error *string
|
||||
}
|
||||
|
||||
const latestResults = `-- name: LatestResults :many
|
||||
SELECT DISTINCT ON (monitor_id, region) monitor_id, region, time, ok, latency_ms, status_code, error
|
||||
FROM check_results
|
||||
WHERE org_id = $1 AND time > now() - interval '1 day'
|
||||
ORDER BY monitor_id, region, time DESC
|
||||
`
|
||||
|
||||
type LatestResultsRow struct {
|
||||
MonitorID uuid.UUID
|
||||
Region string
|
||||
Time time.Time
|
||||
Ok bool
|
||||
LatencyMs *int32
|
||||
StatusCode *int32
|
||||
Error *string
|
||||
}
|
||||
|
||||
// Newest result per monitor and region in the org.
|
||||
func (q *Queries) LatestResults(ctx context.Context, orgID uuid.UUID) ([]LatestResultsRow, error) {
|
||||
rows, err := q.db.Query(ctx, latestResults, orgID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []LatestResultsRow{}
|
||||
for rows.Next() {
|
||||
var i LatestResultsRow
|
||||
if err := rows.Scan(
|
||||
&i.MonitorID,
|
||||
&i.Region,
|
||||
&i.Time,
|
||||
&i.Ok,
|
||||
&i.LatencyMs,
|
||||
&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 recentResults = `-- name: RecentResults :many
|
||||
SELECT time, region, ok, latency_ms, status_code, error
|
||||
FROM check_results
|
||||
WHERE org_id = $1 AND monitor_id = $2 AND time > $3
|
||||
ORDER BY time DESC
|
||||
LIMIT 500
|
||||
`
|
||||
|
||||
type RecentResultsParams struct {
|
||||
OrgID uuid.UUID
|
||||
MonitorID uuid.UUID
|
||||
Time time.Time
|
||||
}
|
||||
|
||||
type RecentResultsRow struct {
|
||||
Time time.Time
|
||||
Region string
|
||||
Ok bool
|
||||
LatencyMs *int32
|
||||
StatusCode *int32
|
||||
Error *string
|
||||
}
|
||||
|
||||
func (q *Queries) RecentResults(ctx context.Context, arg RecentResultsParams) ([]RecentResultsRow, error) {
|
||||
rows, err := q.db.Query(ctx, recentResults, arg.OrgID, arg.MonitorID, arg.Time)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []RecentResultsRow{}
|
||||
for rows.Next() {
|
||||
var i RecentResultsRow
|
||||
if err := rows.Scan(
|
||||
&i.Time,
|
||||
&i.Region,
|
||||
&i.Ok,
|
||||
&i.LatencyMs,
|
||||
&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 uptimeSince = `-- name: UptimeSince :many
|
||||
SELECT monitor_id, sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count
|
||||
FROM check_results_1h
|
||||
WHERE org_id = $1 AND bucket >= $2
|
||||
GROUP BY monitor_id
|
||||
`
|
||||
|
||||
type UptimeSinceParams struct {
|
||||
OrgID uuid.UUID
|
||||
Bucket interface{}
|
||||
}
|
||||
|
||||
type UptimeSinceRow struct {
|
||||
MonitorID uuid.UUID
|
||||
Total int64
|
||||
OkCount int64
|
||||
}
|
||||
|
||||
// Uptime per monitor from the hourly aggregate, including the not yet
|
||||
// materialized most recent hours (real-time aggregation).
|
||||
func (q *Queries) UptimeSince(ctx context.Context, arg UptimeSinceParams) ([]UptimeSinceRow, error) {
|
||||
rows, err := q.db.Query(ctx, uptimeSince, arg.OrgID, arg.Bucket)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []UptimeSinceRow{}
|
||||
for rows.Next() {
|
||||
var i UptimeSinceRow
|
||||
if err := rows.Scan(&i.MonitorID, &i.Total, &i.OkCount); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
Reference in new issue
Block a user