An incident monitor receives aggregated transaction logs from multiple merchants and watches for spikes in error responses. An alert represents an error spike for one merchant and one error status code.
You are given logs, an array of strings where each record has the format "<timestamp>,<merchantId>,<statusCode>,<count>":
timestamp: The time in seconds.merchantId: The merchant associated with the transactions.statusCode: HTTP status code200represents successful transactions; each4xxor5xxcode represents a distinct error.count: The number of transactions for that merchant and status code at that second.
The logs are sorted by nondecreasing timestamp, and no two records share the same (timestamp, merchantId, statusCode).
Process the logs in input order, including records sharing a timestamp. After processing a log at time T, the current 30-second window contains all processed records with timestamps in [T - 29, T] (inclusively). After each log, evaluate alerts only for that log's merchant, considering all error status codes previously observed for that merchant.
Each alert belongs to a (merchantId, statusCode) pair, where statusCode is an error code. It qualifies when the sum of that pair's error counts in the current window is at least 5. Emit a "TRIGGER" event when a pair first qualifies, but do not trigger an already active alert again. In this part, a triggered alert remains active for the rest of the input.
Return all events as a string array. Each event has the format "<timestamp>,<eventType>,<merchantId>,<statusCode>", without spaces. Sort the events by timestamp, merchant ID, numeric status code, and event type, all in ascending order. Return an empty array if no event occurs.
Constraints:
- 0 ≤
logs.length≤ 1,000. - 0 ≤
timestamp≤ 106. - 1 ≤
count≤ 1,000. - 1 ≤
merchantId.length≤ 30, and merchant IDs contain no commas or spaces.
Example 1:
Input: logs = ["10,merchant1,500,2", "10,merchant2,500,1", "15,merchant1,500,2", "20,merchant1,500,1", "20,merchant2,500,4"] Output: ["20,TRIGGER,merchant1,500", "20,TRIGGER,merchant2,500"] Explanation: At time 20, each merchant has five failures with status code 500 in its current 30-second window. Both alerts trigger, and the events are ordered by merchant ID.
Example 2:
Input: logs = ["1,alpha,500,3", "2,alpha,404,4", "3,alpha,500,2", "3,alpha,404,1", "3,beta,500,5"] Output: ["3,TRIGGER,alpha,404", "3,TRIGGER,alpha,500", "3,TRIGGER,beta,500"]
Example 3:
Input: logs = ["0,m,500,2", "29,m,500,2", "30,m,500,1", "31,m,500,4"] Output: ["31,TRIGGER,m,500"]
