Status Runs with groupby
Hard · Iterators
itertools.groupby(iterable, key) splits an iterable into groups of consecutive items that share the same key. It does not sort first, so the same key can show up in several groups. That makes it a perfect fit for "runs" in a timeline.
A monitor checks a service and records [minute, status] pairs, sorted by minute. Write status_runs(checks) that returns one dict per run of the same status:
- "status": the status of the run
- "start" and "end": the first and last minute of the run
- "count": how many checks are in the run
An empty list gives [].
Next: Bundles Within Budget