Throttled Event Logger
You operate a telemetry pipeline that receives named events from many services. To keep the output readable, the pipeline limits how often the same event may be forwarded. If an event was forwarded recently, the next occurrences are dropped until enough time has passed.
Design a class Logger that decides for each incoming event whether it should be forwarded.
The class provides:
Logger(): creates an empty logger with no history.shouldPrintMessage(timestamp: number, message: string) -> boolean: called with the current timestamp in seconds and an event name. Returntrueif the event should be forwarded at this timestamp, otherwise returnfalse.
An event should be forwarded only when one of the following holds:
- the same
messagehas never been forwarded before, or - at least 10 seconds have elapsed since the last time that exact
messagewas forwarded.
When the method returns true, the logger records this timestamp as the last forwarded time for that message. When it returns false, the stored time for that message does not change and later calls still compare against the last successful time.
Timestamps are provided in non decreasing order. The check uses the condition timestamp >= lastTimestamp + 10.
Throttled Event Logger
You operate a telemetry pipeline that receives named events from many services. To keep the output readable, the pipeline limits how often the same event may be forwarded. If an event was forwarded recently, the next occurrences are dropped until enough time has passed.