// Code generated by sqlc. DO NOT EDIT. // versions: // sqlc v1.31.1 // source: latest_results.sql package store import ( "context" "time" "github.com/google/uuid" ) const latestResults = `-- name: LatestResults :many SELECT monitor_id, region, time, ok, latency_ms, status_code, error FROM latest_results WHERE org_id = $1 AND time > now() - interval '1 day' ` 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, of the last day. 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 latestResultsForMonitors = `-- name: LatestResultsForMonitors :many SELECT monitor_id, region, time, ok, latency_ms, status_code, error FROM latest_results WHERE org_id = $1 AND monitor_id = ANY ($2::uuid[]) AND time > now() - interval '1 day' ` type LatestResultsForMonitorsParams struct { OrgID uuid.UUID Ids []uuid.UUID } type LatestResultsForMonitorsRow struct { MonitorID uuid.UUID Region string Time time.Time Ok bool LatencyMs *int32 StatusCode *int32 Error *string } // LatestResults limited to the monitors ids. func (q *Queries) LatestResultsForMonitors(ctx context.Context, arg LatestResultsForMonitorsParams) ([]LatestResultsForMonitorsRow, error) { rows, err := q.db.Query(ctx, latestResultsForMonitors, arg.OrgID, arg.Ids) if err != nil { return nil, err } defer rows.Close() items := []LatestResultsForMonitorsRow{} for rows.Next() { var i LatestResultsForMonitorsRow 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 upsertLatestResults = `-- name: UpsertLatestResults :exec INSERT INTO latest_results (monitor_id, region, org_id, time, ok, latency_ms, status_code, error) SELECT DISTINCT ON (r.monitor_id, r.region) r.monitor_id, r.region, r.org_id, r.time, r.ok, r.latency_ms, r.status_code, r.error FROM jsonb_to_recordset($1::jsonb) AS r(monitor_id uuid, region text, org_id uuid, time timestamptz, ok boolean, latency_ms integer, status_code integer, error text) ORDER BY r.monitor_id, r.region, r.time DESC ON CONFLICT (monitor_id, region) DO UPDATE SET org_id = excluded.org_id, time = excluded.time, ok = excluded.ok, latency_ms = excluded.latency_ms, status_code = excluded.status_code, error = excluded.error WHERE latest_results.time <= excluded.time ` // latest_results holds the newest check result per monitor and region // (see migration 00055) and has row-level security. // Keeps the newest of rows, a JSON array of results, per monitor and // region; results arriving late do not replace newer ones. func (q *Queries) UpsertLatestResults(ctx context.Context, rows []byte) error { _, err := q.db.Exec(ctx, upsertLatestResults, rows) return err }