feat(store): query daily buckets, per-region history and first data
Refs #61 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
273a958d76
commit
24147ec31d
3 files changed
+354
No files matched your search
@@ -98,6 +98,80 @@ func (q *Queries) DailyBucketsForMonitors(ctx context.Context, arg DailyBucketsF
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const firstDataDaily = `-- name: FirstDataDaily :many
|
||||
SELECT monitor_id, region, min(bucket)::timestamptz AS first
|
||||
FROM check_results_1d
|
||||
WHERE org_id = $1
|
||||
GROUP BY 1, 2
|
||||
`
|
||||
|
||||
type FirstDataDailyRow struct {
|
||||
MonitorID uuid.UUID
|
||||
Region string
|
||||
First time.Time
|
||||
}
|
||||
|
||||
// When each monitor and region first reported, to the day.
|
||||
func (q *Queries) FirstDataDaily(ctx context.Context, orgID uuid.UUID) ([]FirstDataDailyRow, error) {
|
||||
rows, err := q.db.Query(ctx, firstDataDaily, orgID)
|
||||
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 time >= $2
|
||||
GROUP BY 1, 2
|
||||
`
|
||||
|
||||
type FirstDataRecentParams struct {
|
||||
OrgID uuid.UUID
|
||||
Time time.Time
|
||||
}
|
||||
|
||||
type FirstDataRecentRow struct {
|
||||
MonitorID uuid.UUID
|
||||
Region string
|
||||
First time.Time
|
||||
}
|
||||
|
||||
// When each monitor and region first reported since $2, 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.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,
|
||||
(sum(avg_latency_ms * total) / nullif(sum(total), 0))::real AS avg_latency_ms
|
||||
@@ -301,6 +375,64 @@ func (q *Queries) RangeBuckets(ctx context.Context, arg RangeBucketsParams) ([]R
|
||||
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 ($4::uuid IS NULL OR monitor_id = $4)
|
||||
GROUP BY 1, 2
|
||||
ORDER BY 1, 2
|
||||
`
|
||||
|
||||
type RangeBucketsDailyParams struct {
|
||||
OrgID uuid.UUID
|
||||
Width string
|
||||
Since interface{}
|
||||
MonitorID *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.MonitorID,
|
||||
)
|
||||
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,
|
||||
@@ -409,6 +541,166 @@ func (q *Queries) RecentResults(ctx context.Context, arg RecentResultsParams) ([
|
||||
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 uptimeSince = `-- name: UptimeSince :many
|
||||
SELECT monitor_id, sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count
|
||||
FROM check_results_1h
|
||||
|
||||
@@ -66,6 +66,11 @@ type Querier interface {
|
||||
// System scope: resolves a request to a page by verified custom domain or
|
||||
// by slug.
|
||||
FindPublishedPage(ctx context.Context, arg FindPublishedPageParams) (StatusPage, error)
|
||||
// When each monitor and region first reported, to the day.
|
||||
FirstDataDaily(ctx context.Context, orgID uuid.UUID) ([]FirstDataDailyRow, error)
|
||||
// When each monitor and region first reported since $2, exactly. Used for
|
||||
// data younger than the daily aggregate's resolution.
|
||||
FirstDataRecent(ctx context.Context, arg FirstDataRecentParams) ([]FirstDataRecentRow, error)
|
||||
GetAPIToken(ctx context.Context, id uuid.UUID) (ApiToken, error)
|
||||
GetAPITokenByHash(ctx context.Context, tokenHash []byte) (ApiToken, error)
|
||||
// System scope.
|
||||
@@ -148,9 +153,15 @@ type Querier interface {
|
||||
OrgIDBySlug(ctx context.Context, slug string) (uuid.UUID, error)
|
||||
// Raw results bucketed by width since a point in time, for short ranges.
|
||||
RangeBuckets(ctx context.Context, arg RangeBucketsParams) ([]RangeBucketsRow, error)
|
||||
// The daily aggregate rebucketed by width, for ranges of months.
|
||||
RangeBucketsDaily(ctx context.Context, arg RangeBucketsDailyParams) ([]RangeBucketsDailyRow, error)
|
||||
// The hourly aggregate rebucketed by width, for ranges of days.
|
||||
RangeBucketsHourly(ctx context.Context, arg RangeBucketsHourlyParams) ([]RangeBucketsHourlyRow, error)
|
||||
RecentResults(ctx context.Context, arg RecentResultsParams) ([]RecentResultsRow, error)
|
||||
// One monitor's raw results per region.
|
||||
RegionBuckets(ctx context.Context, arg RegionBucketsParams) ([]RegionBucketsRow, error)
|
||||
RegionBucketsDaily(ctx context.Context, arg RegionBucketsDailyParams) ([]RegionBucketsDailyRow, error)
|
||||
RegionBucketsHourly(ctx context.Context, arg RegionBucketsHourlyParams) ([]RegionBucketsHourlyRow, error)
|
||||
RegroupMonitors(ctx context.Context, arg RegroupMonitorsParams) error
|
||||
RemoveMembership(ctx context.Context, arg RemoveMembershipParams) (int64, error)
|
||||
// Moves the subgroups of a group that is about to be deleted.
|
||||
|
||||
@@ -77,3 +77,54 @@ WHERE org_id = $1 AND bucket >= sqlc.arg(since)
|
||||
AND (sqlc.narg(monitor_id)::uuid IS NULL OR monitor_id = sqlc.narg(monitor_id))
|
||||
GROUP BY 1, 2
|
||||
ORDER BY 1, 2;
|
||||
|
||||
-- name: RangeBucketsDaily :many
|
||||
-- The daily aggregate rebucketed by width, for ranges of months.
|
||||
SELECT monitor_id, time_bucket(sqlc.arg(width)::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 >= sqlc.arg(since)
|
||||
AND (sqlc.narg(monitor_id)::uuid IS NULL OR monitor_id = sqlc.narg(monitor_id))
|
||||
GROUP BY 1, 2
|
||||
ORDER BY 1, 2;
|
||||
|
||||
-- name: RegionBuckets :many
|
||||
-- One monitor's raw results per region.
|
||||
SELECT region, time_bucket(sqlc.arg(width)::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 = sqlc.arg(monitor_id) AND time >= sqlc.arg(since)
|
||||
GROUP BY 1, 2
|
||||
ORDER BY 1, 2;
|
||||
|
||||
-- name: RegionBucketsHourly :many
|
||||
SELECT region, time_bucket(sqlc.arg(width)::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 = sqlc.arg(monitor_id) AND bucket >= sqlc.arg(since)
|
||||
GROUP BY 1, 2
|
||||
ORDER BY 1, 2;
|
||||
|
||||
-- name: RegionBucketsDaily :many
|
||||
SELECT region, time_bucket(sqlc.arg(width)::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 = sqlc.arg(monitor_id) AND bucket >= sqlc.arg(since)
|
||||
GROUP BY 1, 2
|
||||
ORDER BY 1, 2;
|
||||
|
||||
-- name: FirstDataDaily :many
|
||||
-- When each monitor and region first reported, to the day.
|
||||
SELECT monitor_id, region, min(bucket)::timestamptz AS first
|
||||
FROM check_results_1d
|
||||
WHERE org_id = $1
|
||||
GROUP BY 1, 2;
|
||||
|
||||
-- name: FirstDataRecent :many
|
||||
-- When each monitor and region first reported since $2, exactly. Used for
|
||||
-- data younger than the daily aggregate's resolution.
|
||||
SELECT monitor_id, region, min(time)::timestamptz AS first
|
||||
FROM check_results
|
||||
WHERE org_id = $1 AND time >= $2
|
||||
GROUP BY 1, 2;
|
||||
Reference in new issue
Block a user