Skip to content

[Feature] Add platform availability summary endpoint - #139

Merged
Aloento merged 1 commit into
mainfrom
feat/availability-summary
Dec 1, 2026
Merged

Aloento merged 1 commit into
mainfrom
feat/availability-summary

Conversation

@Aloento

@Aloento Aloento commented Dec 1, 2026

Copy link
Copy Markdown
Member

Closes #112. Depends on #138; merge #138 first.

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.

Summary

Adds a public GET /v2/availability/summary endpoint: platform-level availability for the trailing six calendar months (ending at request time), with per-month percentages, an overall duration-weighted percentage, and affected-component counts. Reuses the existing outage query (db.GetComponentOutages) and the merge/overlap helpers from internal/api/v2/v2.go, and is registered public at internal/api/routes.go:115, consistent with the docs/events.md update. The new logic is well-structured and covered by thorough unit tests in internal/api/v2/availability_summary_test.go.

I did not find a blocking bug in the code I could read. Findings:

Warning

  1. componentActiveInMonth (internal/api/v2/availability_summary.go:74-85) depends on GetComponentsWithValues returning soft-deleted rows — the whole deletion-aware lifecycle logic (excluded for the month of deletion, included in components_total for earlier months) only works if db.GetComponentsWithValues (internal/db/db.go:751) returns components including soft-deleted ones with DeletedAt populated. The unit tests call buildAvailabilitySummary with hand-built db.Components, so they pass regardless of what the DB layer returns. I could not verify the query's filter in this environment. If it filters out deleted components, the DeletedAt branch is dead code (not wrong — the result is the same), but components_total would drop components deleted before the window, contradicting the "survived the month" intent. Please confirm GetComponentsWithValues does not exclude deleted_at IS NOT NULL rows, or add an integration test (e.g. under tests/) asserting a soft-deleted component's contribution.

Suggestion

  1. Window.Months is always 6 by construction (internal/api/v2/availability_summary.go:155-160) since summaryMonths always emits exactly availabilitySummaryWindowMonths labels, including the zero-overlap first instant of a month (tested in TestAvailabilitySummaryWindow). That's fine, but the field then carries no information beyond the constant — consider documenting that in the OpenAPI spec (openapi.yaml currently has no summary schema; the endpoint appears unlisted there — worth adding for consistency with the per-component availability endpoint).

  2. Overall percentage vs. monthly percentages — the code correctly derives the overall value from raw duration sums rather than averaging rounded monthly values (good, and the April/lifecycle test pins the weighted value to 92.32737). Just make sure the docs' "weighted by component-time" wording matches this, since readers might expect a simple average.

Process

  • CI for the head commit was still queued/in progress (go-test, build, ent-codegen all pending) at review time, so I could not confirm the test suite and build pass; the unit tests look complete and should cover the new code paths.
  • The endpoint is public and unrate-limited, same as the existing GET /v2/availability — consistent with the rest of the v2 surface, no action needed.

Verdict: approve, with the item 1 verification recommended before or right after merge.

@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.

Summary

Adds a public GET /v2/availability/summary endpoint (issue #112) that aggregates per-component outage data into a platform-level, trailing-six-months availability report. The change is well structured: new handler in internal/api/v2/availability_summary.go, a one-line route registration, thorough unit tests covering window math, component lifecycle edge cases, overlap merging and the empty case, plus a detailed spec section in docs/events.md. The logic matches the documented semantics in the cases I could trace, and the reuse of mergeComponentOutages/availabilityPercentage/overlapDuration from v2.go keeps the summary consistent with GET /v2/availability.

One finding below looks like a real correctness bug and must be verified/fixed before merging; the rest are suggestions.


Warning (must verify before merging)

GetComponentsWithValues may not return soft-deleted components, which would break the documented lifecycle semantics

  • File: internal/api/v2/availability_summary.go (handler, lines ~42–49) and internal/db/db.go (GetComponentsWithValues, line 751)
  • Why it matters: The whole month-level design depends on seeing deleted component rows: componentActiveInMonth is only meaningful if a component deleted mid-window still appears in the list (so it is excluded for its deletion month and later, but still counted for the months it was alive). The documented spec says this explicitly ("a component deleted during a month is excluded from that entire month", and components_total counts components "active in at least one month"), and the tests encode it ("component 1 is still active in earlier months"). However, the handler gets its component list from dbInst.GetComponentsWithValues(ctx) — a query that predates this feature and is presumably used by other endpoints that want only live components. If that query carries a WHERE c.deleted_at IS NULL filter (or equivalent), then a component deleted in, say, May would disappear from the list entirely and would be missing from January–April as well, undercounting components_total, the monthly denominators, and the overall percentage for every month before the deletion. The unit tests exercise buildAvailabilitySummary directly with in-memory db.Component slices, so they would not catch this.
  • Fix: Check the SQL of GetComponentsWithValues in internal/db/db.go. If it filters out soft-deleted rows, add (or reuse) a query that returns all components including soft-deleted ones — e.g. GetAllComponentsWithValues — and switch the summary handler to it. Add a test (or at least a code comment) pinning down that soft-deleted components must be present in the list with their DeletedAt set.

Suggestions

Response shape: window.months is redundant

  • File: internal/api/v2/availability_summary.go, lines ~155–158
  • Window.Months is always the constant 6, so it is a constant JSON field. It's harmless and documented, but it adds a field consumers can't act on. Consider dropping it or at least not advertising it in docs/events.md as part of the contract.

Slight duplication between availabilitySummaryWindow and summaryMonths

  • File: internal/api/v2/availability_summary.go, lines ~54–79
  • Both compute the "start of the current month" from now/end identically. Minor, but summaryMonths could be derived from the start the window already computed to guarantee the labels and the window can never drift apart (e.g. if the constants are changed independently later).

Clamping / NaN safety is delegated to availabilityPercentage

  • File: internal/api/v2/availability_summary.go, lines ~149, ~157
  • The summary relies on availabilityPercentage (v2.go:1930) to clamp to [0, 100] and handle a zero denominator. That is fine as long as it keeps those properties; if you add a test in availability_summary_test.go for a pathological input (e.g. an outage longer than the window, which the SQL should prevent but the merge layer shouldn't assume), it would lock in the guarantee that "downtime never exceeds the denominator" at the Go level too.

Timezone handling

  • File: internal/api/v2/availability_summary.go, line ~36
  • time.Now().UTC() is correct, but note the DB query returns timestamp values that are scanned into time.Time without an explicit timezone in GetComponentOutages (internal/db/availability.go). All arithmetic here is done in UTC, so as long as the DB driver scans in UTC consistently (as the existing GET /v2/availability endpoint already assumes), this is fine — just flagging that the summary inherits that assumption.

Minor / nits

  • The docs section in docs/events.md is excellent and very precise; I was able to verify the code against it line by line and they agree (including the zero-denominator-at-month-boundary case).
  • availabilitySummaryWindowMonths (6) vs availabilityWindowMonths (12) in v2.go — the similar names are fine, but a one-line comment cross-referencing them could help future readers.

Verdict

request_changes — the lifecycle behavior hinges on GetComponentsWithValues including soft-deleted component rows. If it filters them out, the endpoint silently undercounts components and availability for every month before a deletion. Please verify the query and, if needed, use an all-components query (and add a regression test at the handler/query boundary). The rest of the implementation is clean and ready to go.

Add public GET /v2/availability/summary reporting platform availability for
the trailing six calendar months, including the current partial month.

Monthly denominators follow the component lifecycle: a component created
during a month counts for the whole month and a component deleted during a
month is excluded from it. Overall availability is weighted by component-time
from raw durations, and the report always contains six newest-first months.

Reuses GetComponentOutages and mergeComponentOutages; the per-component
GET /v2/availability endpoint is unchanged.
@Aloento
Aloento force-pushed the feat/availability-summary branch from 172bb7a to 27aeb3b Compare December 1, 2026 12:06
@Aloento
Aloento merged commit e3911aa into main Dec 1, 2026
13 checks passed
@Aloento
Aloento deleted the feat/availability-summary branch December 1, 2026 12:08
@stackmon stackmon deleted a comment from ecosquad-autoreview Bot Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[api] Add a platform availability summary endpoint

1 participant