Skip to content

[Fix] Bound availability windows and merge overlapping outages - #138

Merged
Aloento merged 3 commits into
mainfrom
fix/availability-window-and-overlap
Dec 1, 2026
Merged

Aloento merged 3 commits into
mainfrom
fix/availability-window-and-overlap

Conversation

@Aloento

@Aloento Aloento commented Dec 1, 2026

Copy link
Copy Markdown
Member

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 1
  • go vet ./...
  • Integration tests in tests/v2_test.go compile; Docker is unavailable here, so they were not executed.

@ecosquad-autoreview

Copy link
Copy Markdown

Error

An error occurred while processing your request:

Model response did not contain a tool call even though tool choice was required.

ecosquad-autoreview[bot]
ecosquad-autoreview Bot previously approved these changes Dec 1, 2026

@ecosquad-autoreview ecosquad-autoreview Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. 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 on i.impact = 3 and i.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 in internal/db); otherwise state the behavior explicitly in docs/events.md.

  2. "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 by id, 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 has ORDER BY id, or sort in Go (e.g. sort.Slice by comp.ID) before building the response. A regression here breaks the documented contract silently.

  3. Integration tests were not executed
    The PR notes tests/v2_test.go only compiles; Docker was unavailable. The new SQL (GREATEST/LEAST/COALESCE clipping, the COALESCE(i.end_date, $2) > i.start_date guard, ordering by outage_start, outage_end that mergeComponentOutages relies 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

  1. now is truncated to the second in the effective window
    File: internal/api/v2/v2_test.go (line ~36, truncatedNow := now.Truncate(time.Second)) implies availabilityWindow truncates now.
    Consequences: the exposed window.end has 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 from time.Now().UTC().
    Fix: keep full precision, or document the truncation in docs/events.md and on AvailabilityWindow.

  2. 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 are docs/events.md, internal/api/v2/v2.go, and internal/api/v2/v2_test.go. If an OpenAPI spec exists in the repo, it now lacks the new window: {start, end, months} response field.
    Fix: update the OpenAPI schema for the availability response, or adjust the PR description.

  3. window.months semantics
    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 assume months == window duration. Consider renaming to e.g. monthCount or adding a comment on AvailabilityWindow to 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.

@ecosquad-autoreview ecosquad-autoreview Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

  1. Component ordering guarantee (v2.go handler, ~lines 1750-1770). The docs (docs/events.md) and the PR description now promise "Components are ordered by id", but the handler builds data in the order GetComponents returns, and I found no explicit sort on the components slice in the changed code (the only sort.Slice calls are for intervals at v2.go:1804 and for monthly entries at v2.go:1950). If the DB result order is not guaranteed to be id order, the documented behavior is not actually enforced. Fix: sort components by ID before the loop (or confirm GetComponents orders by ID and note it in a comment).

Suggestions

  1. $1::timestamp / $2::timestamp casts in componentOutagesQuery (internal/db/availability.go:12-23). Casting a parameter to timestamp (rather than timestamptz) ties the comparison to the session timezone if the incident columns are timestamptz. This pattern likely predates this PR, but since this file is central to the change, consider passing timestamptz parameters (or AT TIME ZONE 'UTC') so the clipping cannot drift if the DB session timezone is ever non-UTC.

  2. CI was not green at review time. The build check was still queued and the author notes the integration tests in tests/v2_test.go were 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 == end is rejected and availabilityMonths only contains months intersecting the window.
  • Response shape: adding window is additive; data keeps 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.

@Aloento
Aloento merged commit 402c1c3 into main Dec 1, 2026
14 checks passed
@Aloento
Aloento deleted the fix/availability-window-and-overlap branch December 1, 2026 12:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment