"""
Practice: batching and serving under load, measured. Lab 12's numbers for one model serving many people
(09_batching.py and serving.py): tokens a second in a batch, the typical (median) wait in a load test, a
queue served one at a time, and when the static-batching "bus" leaves.

Fill in the TODOs, then run the tests:
    pytest test_batch_practice.py        (locally)
or press "Run tests" on the chapter page.
"""


def throughput(tokens, wall_s, batch):
    """A batch of `batch` replies wrote `tokens` tokens in `wall_s` seconds, all starting together.
    Return (tokens a second in total, tokens a second for each reply), both rounded to whole numbers."""
    # TODO
    return (0, 0)


def p50(values):
    """The median the lab reports: nearest-rank, so always one of the values (the lower middle one
    when the count is even)."""
    # TODO
    return 0.0


def summarise(requests):
    """Each request is (arrival, start, first_word, done), in seconds. Return a dict of:
    - "first": typical (p50) time from arrival to the first word,
    - "whole": typical time from arrival to the last word,
    - "write": typical time from the first word to the last (writing the reply),
    - "maxwait": the longest time from arrival to the first word."""
    # TODO
    return {"first": 0.0, "whole": 0.0, "write": 0.0, "maxwait": 0.0}


def one_at_a_time(arrivals, durations):
    """A server that runs one request at a time, in arrival order. Request i arrives at arrivals[i]
    and takes durations[i] seconds once it starts. Return a list of (start, done) per request."""
    # TODO
    return []


def static_batch(waiting, now, size=8, max_wait=0.5):
    """Static batching, as the lab's scheduler. The server is free from `now`. `waiting` holds the
    sorted arrival times of every request not yet started (some may arrive after `now`). The bus
    leaves at the first moment t >= now when any of these holds:
    - `size` requests have arrived,
    - the oldest request has waited `max_wait` seconds,
    - every request has arrived (nobody else is coming).
    Return (t, how many board): those that have arrived by t, at most `size`."""
    # TODO
    return (now, 0)
