How Much Error Budget Is Left?
A service promises an availability SLO of sloPercent (for example 99.9). Over a 30-day window it served totalRequests requests and failedRequests of them failed.
First compute allowedFailures = totalRequests * (100 - sloPercent) / 100 and round it to 2 decimals with Math.round(x * 100) / 100. Use this rounded value for everything that follows.
Return { allowedFailures, remainingFailures, consumedPercent, breached } where remainingFailures = allowedFailures - failedRequests (may be negative), consumedPercent = failedRequests / allowedFailures * 100 and breached is failedRequests > allowedFailures. Round remainingFailures and consumedPercent to 2 decimals the same way. If allowedFailures is 0, consumedPercent is 0 when nothing failed and 100 otherwise.
Example 1
Input: sloPercent = 99.9, totalRequests = 1000000, failedRequests = 250 Output: {"allowedFailures":1000,"remainingFailures":750,"consumedPercent":25,"breached":false} Explanation: 0.1% of a million is 1000 allowed failures; 250 used is 25%.
Example 2
Input: sloPercent = 99, totalRequests = 5000, failedRequests = 60 Output: {"allowedFailures":50,"remainingFailures":-10,"consumedPercent":120,"breached":true}
Constraints
- 90 <= sloPercent < 100
- 1 <= totalRequests <= 10^9
- 0 <= failedRequests <= totalRequests
Hint 1
Floating-point maths makes 100 - 99.9 slightly less than 0.1, which is why the statement rounds allowedFailures first.
Good practice for: Site Reliability Engineer, DevOps Engineer, Cloud Engineer, Backend Developer, MLOps Engineer
Run checks your code on 3 sample tests. Submit runs all 8 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.