feat(store): query results and incidents for arbitrary time ranges
Refs #59 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
b3f19e20ad
commit
b210c8e5c0
5 files changed
+182
No files matched your search
@@ -243,6 +243,122 @@ func (q *Queries) LatestResultsForMonitor(ctx context.Context, arg LatestResults
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user