At most one open incident per monitor is enforced by a partial unique index. Refs #15, #16 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
200 lines
4.4 KiB
Go
200 lines
4.4 KiB
Go
// 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 latestResultsForMonitor = `-- name: LatestResultsForMonitor :many
|
|
SELECT DISTINCT ON (region) region, ok, error
|
|
FROM check_results
|
|
WHERE org_id = $1 AND monitor_id = $2 AND time > now() - interval '1 day'
|
|
ORDER BY region, time DESC
|
|
`
|
|
|
|
type LatestResultsForMonitorParams struct {
|
|
OrgID uuid.UUID
|
|
MonitorID uuid.UUID
|
|
}
|
|
|
|
type LatestResultsForMonitorRow struct {
|
|
Region string
|
|
Ok bool
|
|
Error *string
|
|
}
|
|
|
|
func (q *Queries) LatestResultsForMonitor(ctx context.Context, arg LatestResultsForMonitorParams) ([]LatestResultsForMonitorRow, error) {
|
|
rows, err := q.db.Query(ctx, latestResultsForMonitor, arg.OrgID, arg.MonitorID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []LatestResultsForMonitorRow{}
|
|
for rows.Next() {
|
|
var i LatestResultsForMonitorRow
|
|
if err := rows.Scan(&i.Region, &i.Ok, &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
|
|
}
|