Boss

Boss: Responsive Sensor Dashboard

The boss
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 None sentinel 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 sends None to 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

  1. summarize(stats) (plain function). stats maps a sensor name to a list of values. Return a dict that maps each name to {"count": n, "min": lo, "max": hi, "avg": average}. Round avg to 1 decimal place.
  2. collect(queue, stats) (async). Take items off the queue until you get None. For each (name, value), append value to stats[name], creating the list on first use.
  3. guarded(name, readings, delay, queue, timeout, offline) (async). Await sensor(name, readings, delay, queue) inside asyncio.timeout(timeout). On TimeoutError, append name to offline. Readings sent before the timeout stay on the queue.
  4. dashboard(sensors, timeout) (async). sensors maps a name to (readings, delay). In order:
    1. make a fresh asyncio.Queue(), an empty stats dict and an empty offline list;
    2. start collect(queue, stats) with asyncio.create_task;
    3. in an asyncio.TaskGroup, create one guarded(...) task per sensor;
    4. after the group, await queue.put(None), then await the collector task;
    5. return {"stats": summarize(stats), "offline": sorted(offline)}.