Repository navigation
[Feature] Add platform availability summary endpoint - #139
Conversation
There was a problem hiding this comment.
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
componentActiveInMonth(internal/api/v2/availability_summary.go:74-85) depends onGetComponentsWithValuesreturning soft-deleted rows — the whole deletion-aware lifecycle logic (excluded for the month of deletion, included incomponents_totalfor earlier months) only works ifdb.GetComponentsWithValues(internal/db/db.go:751) returns components including soft-deleted ones withDeletedAtpopulated. The unit tests callbuildAvailabilitySummarywith hand-builtdb.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, theDeletedAtbranch is dead code (not wrong — the result is the same), butcomponents_totalwould drop components deleted before the window, contradicting the "survived the month" intent. Please confirmGetComponentsWithValuesdoes not excludedeleted_at IS NOT NULLrows, or add an integration test (e.g. undertests/) asserting a soft-deleted component's contribution.
Suggestion
-
Window.Monthsis always 6 by construction (internal/api/v2/availability_summary.go:155-160) sincesummaryMonthsalways emits exactlyavailabilitySummaryWindowMonthslabels, including the zero-overlap first instant of a month (tested inTestAvailabilitySummaryWindow). That's fine, but the field then carries no information beyond the constant — consider documenting that in the OpenAPI spec (openapi.yamlcurrently has no summary schema; the endpoint appears unlisted there — worth adding for consistency with the per-component availability endpoint). -
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-codegenall 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.
a7f9bad to
172bb7a
Compare
There was a problem hiding this comment.
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) andinternal/db/db.go(GetComponentsWithValues, line 751) - Why it matters: The whole month-level design depends on seeing deleted component rows:
componentActiveInMonthis 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", andcomponents_totalcounts 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 fromdbInst.GetComponentsWithValues(ctx)— a query that predates this feature and is presumably used by other endpoints that want only live components. If that query carries aWHERE c.deleted_at IS NULLfilter (or equivalent), then a component deleted in, say, May would disappear from the list entirely and would be missing from January–April as well, undercountingcomponents_total, the monthly denominators, and the overall percentage for every month before the deletion. The unit tests exercisebuildAvailabilitySummarydirectly with in-memorydb.Componentslices, so they would not catch this. - Fix: Check the SQL of
GetComponentsWithValuesininternal/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 theirDeletedAtset.
Suggestions
Response shape: window.months is redundant
- File:
internal/api/v2/availability_summary.go, lines ~155–158 Window.Monthsis 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 indocs/events.mdas 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/endidentically. Minor, butsummaryMonthscould be derived from thestartthe 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 inavailability_summary_test.gofor 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 returnstimestampvalues that are scanned intotime.Timewithout an explicit timezone inGetComponentOutages(internal/db/availability.go). All arithmetic here is done in UTC, so as long as the DB driver scans in UTC consistently (as the existingGET /v2/availabilityendpoint already assumes), this is fine — just flagging that the summary inherits that assumption.
Minor / nits
- The docs section in
docs/events.mdis 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) vsavailabilityWindowMonths(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.
172bb7a to
27aeb3b
Compare
Closes #112. Depends on #138; merge #138 first.