Mobile clients batch their events, so they reach the pipeline out of order. Build sessions incrementally as events arrive.
A session is a run of one user's events in which each event is at most gap seconds after the previous one, in time order. A gap of exactly gap keeps the session together; anything longer splits it.
Write a class Sessionizer:
- •
Sessionizer(gap) — gap in seconds. - •
add(user, t) — record one event: a user id and an integer timestamp in seconds. Events can arrive in any order, and the same timestamp can arrive more than once (each one counts). - •
sessions() — the current sessions as a list of (user, start, end, count) tuples, sorted by user, then start.
sessions() can be called at any point, and must always reflect every event added so far. That means a late event can join two sessions into one, when it lands within gap of both.
Python 3.13 in your browser — the standard library plus pandas and numpy; no pip installs.