Realtime limit-breach alerts¶
Goal: a live risk monitor that shows a row the instant a limit is breached and removes it the instant the breach clears: no polling, no scanning. The filter is the alert: if a row is in your subscription, it is currently breached.
This is the sharpest use of focus filtering (oof): you subscribe to the breach condition and let the Center push membership changes to you.
Prerequisites¶
- The WebSocket server is enabled (
ami.websocket.port=3410): see Configuration. - A
LIMITSPUBLICtable with aBooleanbreachedcolumn. Create it (see Concepts → Creating a streamable table):
How breached is maintained (a computed column, a trigger, or an upstream feed) is a Center-side concern; the WebSocket just watches the result. For a manual test, insert a row with breached false and flip it to true over REST.
Subscribe to the breach condition¶
With oof:true, the Center re-evaluates WHERE breached == true whenever breached changes:
- A limit crosses its threshold (
breachedflips totrue) → you receive anadd: raise the alert. - The breach clears (
breachedflips back tofalse) → you receive adelete: retire the alert. - A breached limit's
valuekeeps moving while still breached → normalupdates: refresh the displayed number.
The initial rows, sent as add entries before the snapshot frame, are exactly the limits breached right now, so a monitor that starts mid-session is immediately correct.
Client handling¶
An add before the snapshot frame is a limit that was already breached; an add after it is a new breach. The sample notifies only on the second kind.
The same load happens again if the server's connection to the Center drops. The server sends a status frame with connected:false, then one with connected:true. It then sends every limit that is breached now as an add, followed by a new snapshot frame. A breach that cleared during the outage is not sent as a delete. So the sample collects each load until its snapshot frame and compares it with the alerts it holds:
- A held alert missing from the reload cleared during the outage, so it is dismissed.
- A row in the reload that is not held breached during the outage, so it raises a notification.
- A row that is in both is still breached, so it is kept without a second notification.
Why not oof:false¶
Without focus filtering the alert list would only ever grow: rows enter at snapshot/add time, but a cleared breach is never removed (no delete), so the monitor would keep showing stale, already-resolved alerts. Membership is the entire point here, so oof:true is required.
Why not just poll REST¶
You could poll SELECT … WHERE breached == true over the REST API on a timer, but you would trade latency for load: a breach isn't visible until the next poll, and every poll re-runs the query whether anything changed or not. The WebSocket subscription pushes the transition the moment it happens and sends nothing in between.
Notes¶
- The condition can be anything the Center can express over the row's columns.
value > thresholdor a status flag works in theWHERE. The Center re-checks theWHEREonly when a column it reads changes, so a time-based condition, such as a staleness check, does not retire an alert by itself: compute it into a column that is updated, and filter on that. - One table per subscription. To monitor several conditions, open a subscription per table (or fold them into one
LIMITStable with ametriccolumn, as here).