
Boss
Boss: Responsive Sensor Dashboard

Boss Fight!
Every passing test breaks a shield.
Storm season! The guild's weather sensors on every island stream readings back to the lighthouse. Some sensors are quick, some are sluggish, and one on Fog Isle keeps stalling. The dashboard must stay responsive no matter what.
sensor(name, readings, delay, queue) is ready. For each reading, it
waits delay seconds and then puts (name, value) on the queue.
The plan
You have built every piece of this before:
- The collector is the courier from The Parcel Queue: it reads
the queue until a
Nonesentinel arrives. - The guard is the helper from Checkpoint: Beacon Watch: it runs
one sensor inside its own
asyncio.timeout(...). - The dashboard starts the collector as a background task, runs
the guards in a
TaskGroup, then sendsNoneto stop the collector.
Build and test the pieces one at a time, in the order below. The tests are in the same order, so you can watch them turn green.
Assignment
summarize(stats)(plain function).statsmaps a sensor name to a list of values. Return a dict that maps each name to{"count": n, "min": lo, "max": hi, "avg": average}. Roundavgto 1 decimal place.collect(queue, stats)(async). Take items off the queue until you getNone. For each(name, value), appendvaluetostats[name], creating the list on first use.guarded(name, readings, delay, queue, timeout, offline)(async). Awaitsensor(name, readings, delay, queue)insideasyncio.timeout(timeout). OnTimeoutError, appendnametooffline. Readings sent before the timeout stay on the queue.dashboard(sensors, timeout)(async).sensorsmaps a name to(readings, delay). In order:- make a fresh
asyncio.Queue(), an emptystatsdict and an emptyofflinelist; - start
collect(queue, stats)withasyncio.create_task; - in an
asyncio.TaskGroup, create oneguarded(...)task per sensor; - after the group,
await queue.put(None), then await the collector task; - return
{"stats": summarize(stats), "offline": sorted(offline)}.
- make a fresh
summarize(stats)(plain function).statsmaps a sensor name to a list of values. Return a dict that maps each name to{"count": n, "min": lo, "max": hi, "avg": average}. Roundavgto 1 decimal place.collect(queue, stats)(async). Take items off the queue until you getNone. For each(name, value), appendvaluetostats[name], creating the list on first use.guarded(name, readings, delay, queue, timeout, offline)(async). Awaitsensor(name, readings, delay, queue)insideasyncio.timeout(timeout). OnTimeoutError, appendnametooffline. Readings sent before the timeout stay on the queue.dashboard(sensors, timeout)(async).sensorsmaps a name to(readings, delay). In order:- make a fresh
asyncio.Queue(), an emptystatsdict and an emptyofflinelist; - start
collect(queue, stats)withasyncio.create_task; - in an
asyncio.TaskGroup, create oneguarded(...)task per sensor; - after the group,
await queue.put(None), then await the collector task; - return
{"stats": summarize(stats), "offline": sorted(offline)}.
- make a fresh
main.py Python sleeping
Press Run to try your code, then Check to test it.