Build an in-memory event aggregator for chat events. Each event has user_id, chat_id, timestamp, and event_type. First, support querying the number of events in the last five minutes. Then support querying, for each user, how many chats are active in the last five minutes. A chat is active for a user if that user has a react event for the chat in the last five minutes and there is no later end_chat event for that same user and chat.
Function signature:
class MessageEventAggregator:
def add_event(self, user_id: str, chat_id: str, timestamp: int, event_type: str) -> None:
pass
def recent_event_count(self, now: int) -> int:
pass
def active_chat_count(self, user_id: str, now: int) -> int:
pass
Constraints:
-
Timestamps are integer seconds.
-
Events may arrive in increasing timestamp order for the base version.
-
A follow-up may allow out-of-order events.
-
Memory should not grow without bound as old chats and events age out.
-
event_type
is either
react
or
end_chat
.
Examples:
add react u1 c1 at 100, react u1 c2 at 120, end_chat u1 c1 at 130; active_chat_count('u1', 150) returns 1.
At now = 1000, events older than 700 are outside the five-minute window.