Repository navigation
[Fix] Bound availability windows and merge overlapping outages - #138
Conversation
ErrorAn error occurred while processing your request: |
There was a problem hiding this comment.
Review: Availability endpoint rework (PR #138)
Summary
Reworks GET /v2/availability: outage intervals (impact 3) are now loaded per-component, merged, and clipped to the requested window; ongoing incidents count to the window end; soft-deleted incidents are ignored; monthly denominators use the month∩window overlap; the response gains a window field. Unit tests are thorough (window validation, month enumeration, interval merging, clamping/rounding, ordering). CI is still pending (Analyze (go) queued), so nothing below was verified by a build. Overall the approach is sound; findings are questions/warnings rather than confirmed bugs.
Findings
Warning
-
No status filter in the outage query — cancelled outages still count as downtime
File:internal/db/availability.go(lines 11–23,componentOutagesQuery)
The query filters only oni.impact = 3andi.deleted_at IS NULL. An impact-3 incident that was later cancelled (or reclassified) still books full downtime for its interval, and an ongoing cancelled incident counts to the window end. The docs say only impact-3 incidents "contribute" and never mention excluding non-terminal/cancelled statuses, but the incidents list endpoint treats cancelled events specially, so this is likely a gap.
Fix: if cancelled incidents should not reduce availability, add e.g.AND i.status <> 'cancelled'(or the equivalent terminal-status exclusion used elsewhere ininternal/db); otherwise state the behavior explicitly indocs/events.md. -
"Components are ordered by
id" may not hold
Files:internal/api/v2/v2.go(~line 1750–1765,GetComponentsAvailabilityHandler);docs/events.md
The docs and PR description promise deterministic component order byid, but the handler builds the response in the order returned by the components query (for index, comp := range components) with no visible sort in the diff.
Fix: verify the components query hasORDER BY id, or sort in Go (e.g.sort.Slicebycomp.ID) before building the response. A regression here breaks the documented contract silently. -
Integration tests were not executed
The PR notestests/v2_test.goonly compiles; Docker was unavailable. The new SQL (GREATEST/LEAST/COALESCEclipping, theCOALESCE(i.end_date, $2) > i.start_dateguard, ordering byoutage_start, outage_endthatmergeComponentOutagesrelies on) is only exercised through unit tests with fabricated rows.
Fix: run the integration suite (or at least the availability query against a seeded DB) before merging, since the merge logic assumes the DB rows arrive sorted per component.
Suggestion
-
nowis truncated to the second in the effective window
File:internal/api/v2/v2_test.go(line ~36,truncatedNow := now.Truncate(time.Second)) impliesavailabilityWindowtruncatesnow.
Consequences: the exposedwindow.endhas second precision, ongoing-incident downtime and current-month denominators lose up to ~1 s each. Harmless in practice, but a bit surprising for a value derived fromtime.Now().UTC().
Fix: keep full precision, or document the truncation indocs/events.mdand onAvailabilityWindow. -
PR description mentions OpenAPI updates, but only docs/handler/tests appear changed
The description says "the docs and OpenAPI describe the full month series", yet the visible changed files aredocs/events.md,internal/api/v2/v2.go, andinternal/api/v2/v2_test.go. If an OpenAPI spec exists in the repo, it now lacks the newwindow: {start, end, months}response field.
Fix: update the OpenAPI schema for the availability response, or adjust the PR description. -
window.monthssemantics
internal/api/v2/v2.go(line ~1768):Months: len(months)is the count of reported (calendar months intersected by the window), not 12 for a custom window. That matches the docs, but consumers may assumemonths== window duration. Consider renaming to e.g.monthCountor adding a comment onAvailabilityWindowto make the distinction explicit.
Verdict
No confirmed correctness, security, or build-breaking issues found. Findings 1–3 are worth resolving before or shortly after merge (especially the cancelled-incident behavior and the unrun integration tests), but nothing here requires blocking on its own.
There was a problem hiding this comment.
Review: Availability rework for GET /v2/availability
Summary. The change moves impact-3 outage aggregation out of the SQL into the API layer: intervals are fetched clipped to the window (internal/db/availability.go), merged per component (mergeComponentOutages, internal/api/v2/v2.go:1794), and per-month availability uses the month∩window overlap as denominator (buildMonthlyAvailability, v2.go:1877). Windows are capped at now, limited to 12 months by duration, and the response gains a window field. Docs and tests are updated consistently, including the edge case that a duration-limited window can span 13 calendar month labels.
The window validation logic I could see (v2.go:1835-1853) handles the tricky cases correctly: start == end is rejected (!start.Before(end)), which also protects availabilityPercentage from division by zero, and the 12-month cap is expressed with end.After(start.AddDate(0, 12, 0)), matching the "same day, different months" test in v2_test.go. The SQL correctly clips ongoing incidents (COALESCE(i.end_date, $2)) and excludes soft-deleted incidents (deleted_at IS NULL).
Verdict: approve, with the caveats below — none are things I can see are wrong, but two could not be verified from the diff.
Warning (verify before relying on the guarantee)
- Component ordering guarantee (
v2.gohandler, ~lines 1750-1770). The docs (docs/events.md) and the PR description now promise "Components are ordered byid", but the handler buildsdatain the orderGetComponentsreturns, and I found no explicitsorton the components slice in the changed code (the onlysort.Slicecalls are for intervals atv2.go:1804and for monthly entries atv2.go:1950). If the DB result order is not guaranteed to beidorder, the documented behavior is not actually enforced. Fix: sortcomponentsby ID before the loop (or confirmGetComponentsorders by ID and note it in a comment).
Suggestions
-
$1::timestamp/$2::timestampcasts incomponentOutagesQuery(internal/db/availability.go:12-23). Casting a parameter totimestamp(rather thantimestamptz) ties the comparison to the session timezone if the incident columns aretimestamptz. This pattern likely predates this PR, but since this file is central to the change, consider passing timestamptz parameters (orAT TIME ZONE 'UTC') so the clipping cannot drift if the DB session timezone is ever non-UTC. -
CI was not green at review time. The
buildcheck was stillqueuedand the author notes the integration tests intests/v2_test.gowere only compiled, not executed (Docker unavailable). Re-run the pipeline (and the integration tests) before merging to confirm the SQL against a real database.
Non-issues checked
- Division by zero: unreachable,
start == endis rejected andavailabilityMonthsonly contains months intersecting the window. - Response shape: adding
windowis additive;datakeeps its previous shape. - Ongoing/soft-deleted incidents: handled in SQL as documented.
- 13-month-label windows: explicitly documented rather than a bug.
I could not execute the code or the DB queries in this environment; findings 1 and 3 are the ones that need a follow-up confirmation.
Availability now merges impact-3 outage intervals per component before booking downtime, clips ongoing incidents at the window end, ignores soft-deleted incidents, and uses the overlap between each month and the requested window as the denominator. Windows are capped at now and limited to 12 months of duration, and the response exposes the effective window. Component order is deterministic, and the docs and OpenAPI describe the full month series.
Closes #100, #101, #102, #104, #105, #106, #107, #108, #109
Verification:
go test ./internal/... -count 1go vet ./...tests/v2_test.gocompile; Docker is unavailable here, so they were not executed.