// 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 checkerStats = `-- name: CheckerStats :one SELECT count(*)::bigint AS total, count(*) FILTER (WHERE NOT ok)::bigint AS failed, coalesce(avg(latency_ms), 0)::real AS avg_latency_ms FROM check_results WHERE org_id = $1 AND checker_id = $2 AND time > now() - interval '1 hour' ` type CheckerStatsParams struct { OrgID uuid.UUID CheckerID uuid.UUID } type CheckerStatsRow struct { Total int64 Failed int64 AvgLatencyMs float32 } // How much one instance checked for the organization in the last hour. func (q *Queries) CheckerStats(ctx context.Context, arg CheckerStatsParams) (CheckerStatsRow, error) { row := q.db.QueryRow(ctx, checkerStats, arg.OrgID, arg.CheckerID) var i CheckerStatsRow err := row.Scan(&i.Total, &i.Failed, &i.AvgLatencyMs) return i, err } 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 earliestDataDaily = `-- name: EarliestDataDaily :one SELECT bucket::timestamptz AS first FROM check_results_1d WHERE org_id = $1 ORDER BY bucket LIMIT 1 ` // The day the organization's monitors first reported, if any did. func (q *Queries) EarliestDataDaily(ctx context.Context, orgID uuid.UUID) (time.Time, error) { row := q.db.QueryRow(ctx, earliestDataDaily, orgID) var first time.Time err := row.Scan(&first) return first, err } const earliestDataRecent = `-- name: EarliestDataRecent :one SELECT f.time FROM monitors m CROSS JOIN LATERAL ( SELECT c.time FROM check_results c WHERE c.org_id = $1 AND c.monitor_id = m.id AND c.time >= $2 ORDER BY c.time LIMIT 1 ) f WHERE m.org_id = $1 ORDER BY f.time LIMIT 1 ` type EarliestDataRecentParams struct { OrgID uuid.UUID Time time.Time } // When the organization's monitors first reported since $2, exactly: the // first result of each monitor is found through its index, so the cost // does not grow with the days of results. func (q *Queries) EarliestDataRecent(ctx context.Context, arg EarliestDataRecentParams) (time.Time, error) { row := q.db.QueryRow(ctx, earliestDataRecent, arg.OrgID, arg.Time) var time time.Time err := row.Scan(&time) return time, err } const firstDataDaily = `-- name: FirstDataDaily :many SELECT monitor_id, region, min(bucket)::timestamptz AS first FROM check_results_1d WHERE org_id = $1 AND monitor_id = $2 GROUP BY 1, 2 ` type FirstDataDailyParams struct { OrgID uuid.UUID MonitorID uuid.UUID } type FirstDataDailyRow struct { MonitorID uuid.UUID Region string First time.Time } // When each region of a monitor first reported, to the day. func (q *Queries) FirstDataDaily(ctx context.Context, arg FirstDataDailyParams) ([]FirstDataDailyRow, error) { rows, err := q.db.Query(ctx, firstDataDaily, arg.OrgID, arg.MonitorID) if err != nil { return nil, err } defer rows.Close() items := []FirstDataDailyRow{} for rows.Next() { var i FirstDataDailyRow if err := rows.Scan(&i.MonitorID, &i.Region, &i.First); err != nil { return nil, err } items = append(items, i) } if err := rows.Err(); err != nil { return nil, err } return items, nil } const firstDataRecent = `-- name: FirstDataRecent :many SELECT monitor_id, region, min(time)::timestamptz AS first FROM check_results WHERE org_id = $1 AND monitor_id = $2 AND time >= $3 GROUP BY 1, 2 ` type FirstDataRecentParams struct { OrgID uuid.UUID MonitorID uuid.UUID Time time.Time } type FirstDataRecentRow struct { MonitorID uuid.UUID Region string First time.Time } // When each region of a monitor first reported since $3, exactly. Used for // data younger than the daily aggregate's resolution. func (q *Queries) FirstDataRecent(ctx context.Context, arg FirstDataRecentParams) ([]FirstDataRecentRow, error) { rows, err := q.db.Query(ctx, firstDataRecent, arg.OrgID, arg.MonitorID, arg.Time) if err != nil { return nil, err } defer rows.Close() items := []FirstDataRecentRow{} for rows.Next() { var i FirstDataRecentRow if err := rows.Scan(&i.MonitorID, &i.Region, &i.First); 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, 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 >= $2 AND ($3::uuid[] IS NULL OR monitor_id = ANY ($3::uuid[])) GROUP BY monitor_id, bucket ORDER BY monitor_id, bucket ` type HourlyBucketsParams struct { OrgID uuid.UUID Bucket interface{} Ids []uuid.UUID } 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, arg.Ids) 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 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 listResults = `-- name: ListResults :many SELECT time, region, ok, latency_ms, status_code, error, checker_id, (region || '|' || checker_id::text)::text AS position FROM check_results WHERE org_id = $1 AND monitor_id = $2 AND ($3::timestamptz IS NULL OR time <= $3 AND (time, region || '|' || checker_id::text) < ($3, $4::text)) AND ($5::timestamptz IS NULL OR time >= $5 AND (time, region || '|' || checker_id::text) > ($5, $6::text)) ORDER BY CASE WHEN $5::timestamptz IS NULL THEN time END DESC, CASE WHEN $5::timestamptz IS NULL THEN region || '|' || checker_id::text END DESC, time, region || '|' || checker_id::text LIMIT $7 ` type ListResultsParams struct { OrgID uuid.UUID MonitorID uuid.UUID AfterT *time.Time AfterS *string BeforeT *time.Time BeforeS *string MaxRows int32 } type ListResultsRow struct { Time time.Time Region string Ok bool LatencyMs *int32 StatusCode *int32 Error *string CheckerID uuid.UUID Position string } // A monitor's results newest first, a page from a keyset cursor (see // paging.go) whose string key is region and checker. func (q *Queries) ListResults(ctx context.Context, arg ListResultsParams) ([]ListResultsRow, error) { rows, err := q.db.Query(ctx, listResults, arg.OrgID, arg.MonitorID, arg.AfterT, arg.AfterS, arg.BeforeT, arg.BeforeS, arg.MaxRows, ) if err != nil { return nil, err } defer rows.Close() items := []ListResultsRow{} for rows.Next() { var i ListResultsRow if err := rows.Scan( &i.Time, &i.Region, &i.Ok, &i.LatencyMs, &i.StatusCode, &i.Error, &i.CheckerID, &i.Position, ); 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 monitor_id = ANY ($4::uuid[]) GROUP BY 1, 2 ORDER BY 1, 2 ` type RangeBucketsParams struct { OrgID uuid.UUID Width string Since time.Time Ids []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.Ids, ) 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 rangeBucketsDaily = `-- name: RangeBucketsDaily :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_1d WHERE org_id = $1 AND bucket >= $3 AND monitor_id = ANY ($4::uuid[]) GROUP BY 1, 2 ORDER BY 1, 2 ` type RangeBucketsDailyParams struct { OrgID uuid.UUID Width string Since interface{} Ids []uuid.UUID } type RangeBucketsDailyRow struct { MonitorID uuid.UUID Bucket time.Time Total int64 OkCount int64 AvgLatencyMs float32 } // The daily aggregate rebucketed by width, for ranges of months. func (q *Queries) RangeBucketsDaily(ctx context.Context, arg RangeBucketsDailyParams) ([]RangeBucketsDailyRow, error) { rows, err := q.db.Query(ctx, rangeBucketsDaily, arg.OrgID, arg.Width, arg.Since, arg.Ids, ) if err != nil { return nil, err } defer rows.Close() items := []RangeBucketsDailyRow{} for rows.Next() { var i RangeBucketsDailyRow 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 monitor_id = ANY ($4::uuid[]) GROUP BY 1, 2 ORDER BY 1, 2 ` type RangeBucketsHourlyParams struct { OrgID uuid.UUID Width string Since interface{} Ids []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.Ids, ) 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 regionBuckets = `-- name: RegionBuckets :many SELECT region, time_bucket($2::text::interval, time)::timestamptz AS bucket, count(*)::bigint AS total, count(*) FILTER (WHERE ok)::bigint AS ok_count FROM check_results WHERE org_id = $1 AND monitor_id = $3 AND time >= $4 GROUP BY 1, 2 ORDER BY 1, 2 ` type RegionBucketsParams struct { OrgID uuid.UUID Width string MonitorID uuid.UUID Since time.Time } type RegionBucketsRow struct { Region string Bucket time.Time Total int64 OkCount int64 } // One monitor's raw results per region. func (q *Queries) RegionBuckets(ctx context.Context, arg RegionBucketsParams) ([]RegionBucketsRow, error) { rows, err := q.db.Query(ctx, regionBuckets, arg.OrgID, arg.Width, arg.MonitorID, arg.Since, ) if err != nil { return nil, err } defer rows.Close() items := []RegionBucketsRow{} for rows.Next() { var i RegionBucketsRow if err := rows.Scan( &i.Region, &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 regionBucketsDaily = `-- name: RegionBucketsDaily :many SELECT region, time_bucket($2::text::interval, 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 = $3 AND bucket >= $4 GROUP BY 1, 2 ORDER BY 1, 2 ` type RegionBucketsDailyParams struct { OrgID uuid.UUID Width string MonitorID uuid.UUID Since interface{} } type RegionBucketsDailyRow struct { Region string Bucket time.Time Total int64 OkCount int64 } func (q *Queries) RegionBucketsDaily(ctx context.Context, arg RegionBucketsDailyParams) ([]RegionBucketsDailyRow, error) { rows, err := q.db.Query(ctx, regionBucketsDaily, arg.OrgID, arg.Width, arg.MonitorID, arg.Since, ) if err != nil { return nil, err } defer rows.Close() items := []RegionBucketsDailyRow{} for rows.Next() { var i RegionBucketsDailyRow if err := rows.Scan( &i.Region, &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 regionBucketsHourly = `-- name: RegionBucketsHourly :many SELECT region, time_bucket($2::text::interval, bucket)::timestamptz AS bucket, sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count FROM check_results_1h WHERE org_id = $1 AND monitor_id = $3 AND bucket >= $4 GROUP BY 1, 2 ORDER BY 1, 2 ` type RegionBucketsHourlyParams struct { OrgID uuid.UUID Width string MonitorID uuid.UUID Since interface{} } type RegionBucketsHourlyRow struct { Region string Bucket time.Time Total int64 OkCount int64 } func (q *Queries) RegionBucketsHourly(ctx context.Context, arg RegionBucketsHourlyParams) ([]RegionBucketsHourlyRow, error) { rows, err := q.db.Query(ctx, regionBucketsHourly, arg.OrgID, arg.Width, arg.MonitorID, arg.Since, ) if err != nil { return nil, err } defer rows.Close() items := []RegionBucketsHourlyRow{} for rows.Next() { var i RegionBucketsHourlyRow if err := rows.Scan( &i.Region, &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 resultsByChecker = `-- name: ResultsByChecker :many SELECT r.time, r.monitor_id, r.region, r.ok, r.latency_ms, r.status_code, r.error FROM check_results r WHERE r.org_id = $1 AND r.checker_id = $2 AND ($3::timestamptz IS NULL OR r.time <= $3 AND (r.time, r.monitor_id::text || '|' || r.region) < ($3, $4::text)) AND ($5::timestamptz IS NULL OR r.time >= $5 AND (r.time, r.monitor_id::text || '|' || r.region) > ($5, $6::text)) ORDER BY CASE WHEN $5::timestamptz IS NULL THEN r.time END DESC, CASE WHEN $5::timestamptz IS NULL THEN r.monitor_id::text || '|' || r.region END DESC, r.time, r.monitor_id::text || '|' || r.region LIMIT $7 ` type ResultsByCheckerParams struct { OrgID uuid.UUID CheckerID uuid.UUID AfterT *time.Time AfterS *string BeforeT *time.Time BeforeS *string MaxRows int32 } type ResultsByCheckerRow struct { Time time.Time MonitorID uuid.UUID Region string Ok bool LatencyMs *int32 StatusCode *int32 Error *string } // The organization's results run by one checker instance, newest first, a // page from a keyset cursor whose string key is monitor and region. func (q *Queries) ResultsByChecker(ctx context.Context, arg ResultsByCheckerParams) ([]ResultsByCheckerRow, error) { rows, err := q.db.Query(ctx, resultsByChecker, arg.OrgID, arg.CheckerID, arg.AfterT, arg.AfterS, arg.BeforeT, arg.BeforeS, arg.MaxRows, ) if err != nil { return nil, err } defer rows.Close() items := []ResultsByCheckerRow{} for rows.Next() { var i ResultsByCheckerRow if err := rows.Scan( &i.Time, &i.MonitorID, &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 }