Exception Threshold Monitor
Problem
Design ExceptionThresholdMonitor to track recent service exceptions under one configured rolling-window policy for each independent exception category. Calls are sequential on one shared instance.
Requirements
- Use each category's policy. Both public methods use the named category's supplied
thresholdandwindowSeconds. - Record the named observation.
recordExceptionadds exactly one observation atoccurredAtSecondto the named configured category. - Preserve other categories when recording. A successful
recordExceptionchanges no other category's observations or latest successful time. - Count only the named category.
getRecentCountforms its result only from observations recorded for the named configured category. - Exclude the lower boundary. At time
t, an observation at exactlyt - windowSecondsis outside the rolling window. - Include the upper boundary. At time
t, an observation at exactlytis inside the rolling window. - Expire during count calls.
getRecentCountremoves expired observations for itsasOfSecondeven when no exception is recorded at that time. - Evaluate after insertion.
recordExceptionevaluates the recent count after adding the new observation. - Require strict exceedance.
recordExceptionreturnstrueexactly when the post-insert count is greater than the named category's threshold. Equality returnsfalse. - Reject an unknown record category.
recordExceptionraisesUnknownCategoryErrorwhen the category has no configured policy. - Reject an unknown count category.
getRecentCountraisesUnknownCategoryErrorwhen the category has no configured policy. - Keep unknown-category rejection atomic. Either error changes no configured category's observations or latest successful time.
- Keep recording amortized constant time.
recordExceptionruns in amortized time over a testcase operation sequence. - Keep counting amortized constant time.
getRecentCountruns in amortized time over a testcase operation sequence.
API
Each policy object contains category: string, threshold: integer, and windowSeconds: integer.
| Signature | Returns | Behavior |
|---|---|---|
ExceptionThresholdMonitor(policies: object[]) | Not applicable | Creates one empty observation history for every supplied unique category policy. |
recordException(category: string, occurredAtSecond: integer) | boolean | Records one configured-category occurrence and returns its post-insert strict threshold condition. |
getRecentCount(category: string, asOfSecond: integer) | integer | Returns the configured category's count in (asOfSecond - windowSeconds, asOfSecond]. |
Examples
For ExceptionThresholdMonitor([{"category":"A","threshold":5,"windowSeconds":1800}]):
| Step | Operation | Result |
|---|---|---|
| 1 | recordException("A", 0) | false |
| 2 | recordException("A", 1) | false |
| 3 | recordException("A", 2) | false |
| 4 | recordException("A", 3) | false |
| 5 | recordException("A", 4) | false |
| 6 | recordException("A", 5) | true |
| 7 | getRecentCount("A", 1800) | 5 |
| 8 | recordException("A", 1800) | true |
At time 1800, the occurrence at 0 is on the exclusive lower boundary and no longer counts. The final record restores the count to 6.
Constraints
policiescontains between 1 and 10,000 objects with unique categories.- A category is a nonempty string of at most 100 Unicode scalar values.
1 <= threshold <= 1,000,0001 <= windowSeconds <= 86,400-9,000,000,000,000,000 <= occurredAtSecond, asOfSecond <= 9,000,000,000,000,000- At most 200,000 public method calls occur per testcase.
- Null values, duplicate categories, and invalid policy fields are not supplied or judged.
Notes
For each category, time values across successful calls are nondecreasing. Different categories may use unrelated times. Testcase calls are sequential; concurrent access, wall-clock access, and out-of-order ingestion are outside scope.