Refs #59 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
451 lines
10 KiB
Go
451 lines
10 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"
|
|
)
|
|
|
|
const dailyBuckets = `-- name: DailyBuckets :many
|
|
SELECT bucket::timestamptz AS bucket, sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count
|
|
FROM check_results_1d
|
|
WHERE org_id = $1 AND monitor_id = $2 AND bucket >= $3
|
|
GROUP BY bucket
|
|
ORDER BY bucket
|
|
`
|
|
|
|
type DailyBucketsParams struct {
|
|
OrgID uuid.UUID
|
|
MonitorID uuid.UUID
|
|
Bucket interface{}
|
|
}
|
|
|
|
type DailyBucketsRow struct {
|
|
Bucket time.Time
|
|
Total int64
|
|
OkCount int64
|
|
}
|
|
|
|
func (q *Queries) DailyBuckets(ctx context.Context, arg DailyBucketsParams) ([]DailyBucketsRow, error) {
|
|
rows, err := q.db.Query(ctx, dailyBuckets, arg.OrgID, arg.MonitorID, arg.Bucket)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []DailyBucketsRow{}
|
|
for rows.Next() {
|
|
var i DailyBucketsRow
|
|
if err := rows.Scan(&i.Bucket, &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
|
|
}
|
|
|
|
const dailyBucketsForMonitors = `-- name: DailyBucketsForMonitors :many
|
|
SELECT monitor_id, bucket::timestamptz AS bucket, sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count
|
|
FROM check_results_1d
|
|
WHERE org_id = $1 AND monitor_id = ANY ($3::uuid[]) AND bucket >= $2
|
|
GROUP BY monitor_id, bucket
|
|
ORDER BY monitor_id, bucket
|
|
`
|
|
|
|
type DailyBucketsForMonitorsParams struct {
|
|
OrgID uuid.UUID
|
|
Bucket interface{}
|
|
Ids []uuid.UUID
|
|
}
|
|
|
|
type DailyBucketsForMonitorsRow struct {
|
|
MonitorID uuid.UUID
|
|
Bucket time.Time
|
|
Total int64
|
|
OkCount int64
|
|
}
|
|
|
|
func (q *Queries) DailyBucketsForMonitors(ctx context.Context, arg DailyBucketsForMonitorsParams) ([]DailyBucketsForMonitorsRow, error) {
|
|
rows, err := q.db.Query(ctx, dailyBucketsForMonitors, arg.OrgID, arg.Bucket, arg.Ids)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []DailyBucketsForMonitorsRow{}
|
|
for rows.Next() {
|
|
var i DailyBucketsForMonitorsRow
|
|
if err := rows.Scan(
|
|
&i.MonitorID,
|
|
&i.Bucket,
|
|
&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
|
|
}
|
|
|
|
const hourlyBuckets = `-- name: HourlyBuckets :many
|
|
SELECT monitor_id, bucket::timestamptz AS bucket, sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count,
|
|
(sum(avg_latency_ms * total) / nullif(sum(total), 0))::real AS avg_latency_ms
|
|
FROM check_results_1h
|
|
WHERE org_id = $1 AND bucket >= $2
|
|
GROUP BY monitor_id, bucket
|
|
ORDER BY monitor_id, bucket
|
|
`
|
|
|
|
type HourlyBucketsParams struct {
|
|
OrgID uuid.UUID
|
|
Bucket interface{}
|
|
}
|
|
|
|
type HourlyBucketsRow struct {
|
|
MonitorID uuid.UUID
|
|
Bucket time.Time
|
|
Total int64
|
|
OkCount int64
|
|
AvgLatencyMs float32
|
|
}
|
|
|
|
// Per monitor and hour since $2, across all regions.
|
|
func (q *Queries) HourlyBuckets(ctx context.Context, arg HourlyBucketsParams) ([]HourlyBucketsRow, error) {
|
|
rows, err := q.db.Query(ctx, hourlyBuckets, arg.OrgID, arg.Bucket)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []HourlyBucketsRow{}
|
|
for rows.Next() {
|
|
var i HourlyBucketsRow
|
|
if err := rows.Scan(
|
|
&i.MonitorID,
|
|
&i.Bucket,
|
|
&i.Total,
|
|
&i.OkCount,
|
|
&i.AvgLatencyMs,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
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 rangeBuckets = `-- name: RangeBuckets :many
|
|
SELECT monitor_id, time_bucket($2::text::interval, time)::timestamptz AS bucket,
|
|
count(*)::bigint AS total, count(*) FILTER (WHERE ok)::bigint AS ok_count,
|
|
coalesce(avg(latency_ms), 0)::real AS avg_latency_ms
|
|
FROM check_results
|
|
WHERE org_id = $1 AND time >= $3
|
|
AND ($4::uuid IS NULL OR monitor_id = $4)
|
|
GROUP BY 1, 2
|
|
ORDER BY 1, 2
|
|
`
|
|
|
|
type RangeBucketsParams struct {
|
|
OrgID uuid.UUID
|
|
Width string
|
|
Since time.Time
|
|
MonitorID *uuid.UUID
|
|
}
|
|
|
|
type RangeBucketsRow struct {
|
|
MonitorID uuid.UUID
|
|
Bucket time.Time
|
|
Total int64
|
|
OkCount int64
|
|
AvgLatencyMs float32
|
|
}
|
|
|
|
// Raw results bucketed by width since a point in time, for short ranges.
|
|
func (q *Queries) RangeBuckets(ctx context.Context, arg RangeBucketsParams) ([]RangeBucketsRow, error) {
|
|
rows, err := q.db.Query(ctx, rangeBuckets,
|
|
arg.OrgID,
|
|
arg.Width,
|
|
arg.Since,
|
|
arg.MonitorID,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []RangeBucketsRow{}
|
|
for rows.Next() {
|
|
var i RangeBucketsRow
|
|
if err := rows.Scan(
|
|
&i.MonitorID,
|
|
&i.Bucket,
|
|
&i.Total,
|
|
&i.OkCount,
|
|
&i.AvgLatencyMs,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const rangeBucketsHourly = `-- name: RangeBucketsHourly :many
|
|
SELECT monitor_id, time_bucket($2::text::interval, bucket)::timestamptz AS bucket,
|
|
sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count,
|
|
coalesce(sum(avg_latency_ms * total) / nullif(sum(total), 0), 0)::real AS avg_latency_ms
|
|
FROM check_results_1h
|
|
WHERE org_id = $1 AND bucket >= $3
|
|
AND ($4::uuid IS NULL OR monitor_id = $4)
|
|
GROUP BY 1, 2
|
|
ORDER BY 1, 2
|
|
`
|
|
|
|
type RangeBucketsHourlyParams struct {
|
|
OrgID uuid.UUID
|
|
Width string
|
|
Since interface{}
|
|
MonitorID *uuid.UUID
|
|
}
|
|
|
|
type RangeBucketsHourlyRow struct {
|
|
MonitorID uuid.UUID
|
|
Bucket time.Time
|
|
Total int64
|
|
OkCount int64
|
|
AvgLatencyMs float32
|
|
}
|
|
|
|
// The hourly aggregate rebucketed by width, for ranges of days.
|
|
func (q *Queries) RangeBucketsHourly(ctx context.Context, arg RangeBucketsHourlyParams) ([]RangeBucketsHourlyRow, error) {
|
|
rows, err := q.db.Query(ctx, rangeBucketsHourly,
|
|
arg.OrgID,
|
|
arg.Width,
|
|
arg.Since,
|
|
arg.MonitorID,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []RangeBucketsHourlyRow{}
|
|
for rows.Next() {
|
|
var i RangeBucketsHourlyRow
|
|
if err := rows.Scan(
|
|
&i.MonitorID,
|
|
&i.Bucket,
|
|
&i.Total,
|
|
&i.OkCount,
|
|
&i.AvgLatencyMs,
|
|
); 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
|
|
}
|