Implement Components Availability API Endpoint #22
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description:
This PR introduces the implementation of an API endpoint to retrieve the availability of components, calculated monthly for the last 12 months. Below is a summary of the changes:
Added:
Data Structures:
ComponentAvailability and MonthlyAvailability structs to encapsulate data about component availability.
Handler Function:
GetComponentsAvailabilityHandler to process HTTP GET requests and return availability data for components.
Calculation Logic:
calculateAvailability function to compute the monthly availability of components based on incident data, considering:
Only incidents with a specific impact level.
Accurate handling of incident periods that span multiple months or fall partially outside the calculation period.
Helper Functions:
minTime and maxTime for date range calculations.
hoursInMonth to calculate the total hours in a specific month.
Enhanced Functionality:
Sorting: Availability data is sorted by year and month in descending order.
Edge Case Handling: Incidents overlapping the calculation window are adjusted to fit within the period.
TODO for the next Issue:
Add filters for the GET request to allow querying specific components or timeframes.
Impact:
This feature provides critical insight into the performance and reliability of components over time, aiding in monitoring and reporting efforts.
Tests:
`status-dashboard-v3$ go test ./internal/api/v2/... -v
=== RUN TestGetIncidentsHandler
v2_test.go:22: start initialisation
2024-11-26T12:59:12.619+0100 DEBUG v2/v2.go:44 retrieve incidents
2024/11/26 12:59:12 status-dashboard-v3/internal/db/db.go:64
[0.048ms] [rows:2] SELECT * FROM "incident_component_relation" WHERE "incident_component_relation"."incident_id" IN (1,2)
2024/11/26 12:59:12 status-dashboard-v3/internal/db/db.go:64
[0.026ms] [rows:2] SELECT "id" FROM "component" WHERE "component"."id" IN (150,151)
2024/11/26 12:59:12 status-dashboard-v3/internal/db/db.go:64
[0.015ms] [rows:2] SELECT * FROM "incident_status" WHERE "incident_status"."incident_id" IN (1,2)
2024/11/26 12:59:12 status-dashboard-v3/internal/db/db.go:64
[0.662ms] [rows:2] SELECT * FROM "incident"
[GIN] 2024/11/26 - 12:59:12 | 200 | 775.692µs | | GET "/v2/incidents"
--- PASS: TestGetIncidentsHandler (0.00s)
=== RUN TestReturn404Handler
v2_test.go:44: start initialisation
[GIN] 2024/11/26 - 12:59:12 | 404 | 4.491µs | | GET "/anyendpoint"
--- PASS: TestReturn404Handler (0.00s)
=== RUN TestGetComponentsAvailabilityHandler
v2_test.go:57: start initialisation
2024-11-26T12:59:12.620+0100 DEBUG v2/v2.go:390 retrieve availability of components
[GIN] 2024/11/26 - 12:59:12 | 200 | 520.714µs | | GET "/v2/availability"
--- PASS: TestGetComponentsAvailabilityHandler (0.00s)
=== RUN TestCalculateAvailability
v2_test.go:162: Test 'Test case: September (66.66667%)- October (94.08602%)': Calculated availability: [{Year:2023 Month:12 Percentage:100} {Year:2024 Month:1 Percentage:100} {Year:2024 Month:2 Percentage:100} {Year:2024 Month:3 Percentage:100} {Year:2024 Month:4 Percentage:100} {Year:2024 Month:5 Percentage:100} {Year:2024 Month:6 Percentage:100} {Year:2024 Month:7 Percentage:100} {Year:2024 Month:8 Percentage:100} {Year:2024 Month:9 Percentage:66.66667} {Year:2024 Month:10 Percentage:94.08602} {Year:2024 Month:11 Percentage:100}]
--- PASS: TestCalculateAvailability (0.00s)
PASS
ok github.com/stackmon/otc-status-dashboard/internal/api/v2 0.005s`