perf: load console pages in time for organizations with many monitors
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 11m10s
ci / test (pull_request) Successful in 12m19s
ci / build (pull_request) Successful in 18m13s
ci / image (pull_request) Successful in 5m20s
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 11m10s
ci / test (pull_request) Successful in 12m19s
ci / build (pull_request) Successful in 18m13s
ci / image (pull_request) Successful in 5m20s
Every console page that shows monitor status (monitors, incidents, monitor details, evidence) went through one loader that read, for all of the organization's monitors: - the newest result per monitor and region, by scanning a day of raw results; - the bars of the selected range, from raw results for ranges up to 24h, though only one page of monitors is shown; - when each monitor first reported, scanning two days of results. With 3000 monitors that took about a second, growing with the number of results. Now: - latest_results keeps the newest result per monitor and region, updated as results are stored (migration 00055 fills it from the last day); - bars are loaded only for the monitors a page shows; - first data is loaded per monitor on its page, and the monitor list asks only for the organization's earliest data, by index; - monitor, region and incident queries name the organization, so their indexes are used instead of row security filtering whole tables. Public status pages also read only their own monitors' latest results and hourly buckets instead of the whole organization's. 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
2a440b9c5e
commit
1138d555aa
22 files changed
+630
-182
No files matched your search
@@ -124,22 +124,65 @@ func (q *Queries) DailyBucketsForMonitors(ctx context.Context, arg DailyBucketsF
|
||||
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
|
||||
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 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)
|
||||
// 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
|
||||
}
|
||||
@@ -161,13 +204,14 @@ func (q *Queries) FirstDataDaily(ctx context.Context, orgID uuid.UUID) ([]FirstD
|
||||
const firstDataRecent = `-- name: FirstDataRecent :many
|
||||
SELECT monitor_id, region, min(time)::timestamptz AS first
|
||||
FROM check_results
|
||||
WHERE org_id = $1 AND time >= $2
|
||||
WHERE org_id = $1 AND monitor_id = $2 AND time >= $3
|
||||
GROUP BY 1, 2
|
||||
`
|
||||
|
||||
type FirstDataRecentParams struct {
|
||||
OrgID uuid.UUID
|
||||
Time time.Time
|
||||
OrgID uuid.UUID
|
||||
MonitorID uuid.UUID
|
||||
Time time.Time
|
||||
}
|
||||
|
||||
type FirstDataRecentRow struct {
|
||||
@@ -176,10 +220,10 @@ type FirstDataRecentRow struct {
|
||||
First time.Time
|
||||
}
|
||||
|
||||
// When each monitor and region first reported since $2, exactly. Used for
|
||||
// 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.Time)
|
||||
rows, err := q.db.Query(ctx, firstDataRecent, arg.OrgID, arg.MonitorID, arg.Time)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -203,6 +247,7 @@ SELECT monitor_id, bucket::timestamptz AS bucket, sum(total)::bigint AS total, s
|
||||
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
|
||||
`
|
||||
@@ -210,6 +255,7 @@ ORDER BY monitor_id, bucket
|
||||
type HourlyBucketsParams struct {
|
||||
OrgID uuid.UUID
|
||||
Bucket interface{}
|
||||
Ids []uuid.UUID
|
||||
}
|
||||
|
||||
type HourlyBucketsRow struct {
|
||||
@@ -222,7 +268,7 @@ type HourlyBucketsRow struct {
|
||||
|
||||
// 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)
|
||||
rows, err := q.db.Query(ctx, hourlyBuckets, arg.OrgID, arg.Bucket, arg.Ids)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -259,52 +305,6 @@ type InsertCheckResultsParams struct {
|
||||
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
|
||||
@@ -425,16 +425,16 @@ SELECT monitor_id, time_bucket($2::text::interval, time)::timestamptz AS bucket,
|
||||
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)
|
||||
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
|
||||
MonitorID *uuid.UUID
|
||||
OrgID uuid.UUID
|
||||
Width string
|
||||
Since time.Time
|
||||
Ids []uuid.UUID
|
||||
}
|
||||
|
||||
type RangeBucketsRow struct {
|
||||
@@ -451,7 +451,7 @@ func (q *Queries) RangeBuckets(ctx context.Context, arg RangeBucketsParams) ([]R
|
||||
arg.OrgID,
|
||||
arg.Width,
|
||||
arg.Since,
|
||||
arg.MonitorID,
|
||||
arg.Ids,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -483,16 +483,16 @@ SELECT monitor_id, time_bucket($2::text::interval, bucket)::timestamptz AS bucke
|
||||
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)
|
||||
AND monitor_id = ANY ($4::uuid[])
|
||||
GROUP BY 1, 2
|
||||
ORDER BY 1, 2
|
||||
`
|
||||
|
||||
type RangeBucketsDailyParams struct {
|
||||
OrgID uuid.UUID
|
||||
Width string
|
||||
Since interface{}
|
||||
MonitorID *uuid.UUID
|
||||
OrgID uuid.UUID
|
||||
Width string
|
||||
Since interface{}
|
||||
Ids []uuid.UUID
|
||||
}
|
||||
|
||||
type RangeBucketsDailyRow struct {
|
||||
@@ -509,7 +509,7 @@ func (q *Queries) RangeBucketsDaily(ctx context.Context, arg RangeBucketsDailyPa
|
||||
arg.OrgID,
|
||||
arg.Width,
|
||||
arg.Since,
|
||||
arg.MonitorID,
|
||||
arg.Ids,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -541,16 +541,16 @@ SELECT monitor_id, time_bucket($2::text::interval, bucket)::timestamptz AS bucke
|
||||
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)
|
||||
AND monitor_id = ANY ($4::uuid[])
|
||||
GROUP BY 1, 2
|
||||
ORDER BY 1, 2
|
||||
`
|
||||
|
||||
type RangeBucketsHourlyParams struct {
|
||||
OrgID uuid.UUID
|
||||
Width string
|
||||
Since interface{}
|
||||
MonitorID *uuid.UUID
|
||||
OrgID uuid.UUID
|
||||
Width string
|
||||
Since interface{}
|
||||
Ids []uuid.UUID
|
||||
}
|
||||
|
||||
type RangeBucketsHourlyRow struct {
|
||||
@@ -567,7 +567,7 @@ func (q *Queries) RangeBucketsHourly(ctx context.Context, arg RangeBucketsHourly
|
||||
arg.OrgID,
|
||||
arg.Width,
|
||||
arg.Since,
|
||||
arg.MonitorID,
|
||||
arg.Ids,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in new issue
Block a user