"""
Practice: deadlocks and connection pools. Lab 05's logic for scripts 02_deadlocks.py and
05_pool_deadlock.py (bank.lock_order, and the arithmetic behind the tables): the order a transfer
locks its rows in, finding a cycle of waiting the way Postgres's deadlock check does, what a
deadlock costs under load, and when a pool of connections can wait for itself.

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


def lock_order(src, dst, ordered):
    """The order a transfer from `src` to `dst` locks the two account rows.
    ordered=False: request order, the sender first (src, dst).
    ordered=True: lowest account number first, whichever way the money goes."""
    # TODO
    raise NotImplementedError


def find_cycle(waits_for):
    """`waits_for` maps each waiting transaction to the one holding the lock it wants,
    e.g. {"A": "B", "B": "A"}. Return the transactions in a cycle, starting from the
    first one found by following the arrows from each key in order, as a list
    (["A", "B"]); return [] if nobody is waiting in a circle.
    This is the question Postgres's deadlock check answers after deadlock_timeout."""
    # TODO
    raise NotImplementedError


def transfers_locking(pairs, ordered):
    """Each transfer in `pairs` ((src, dst) tuples) holds its first row and waits for its
    second, all at the same moment. Build the waits-for map {transfer index: index of the
    transfer holding the row it wants} and return find_cycle() of it. Rows are taken in
    lock_order(); if two transfers want the same first row, the earlier one (lower index)
    gets it and the later one waits for it, holding nothing."""
    # TODO
    raise NotImplementedError


def deadlocks_in_progress(deadlocks, deadlock_timeout_s, seconds):
    """On average, how many deadlocks were going on at any moment: each one lasts about
    deadlock_timeout, so deadlocks x timeout ÷ the run's length."""
    # TODO
    raise NotImplementedError


def per_transfer(deadlocks, transfers):
    """Deadlocks per transfer, as a fraction."""
    # TODO
    raise NotImplementedError


def hang_threshold(pool_size, connections_per_request=2):
    """The fewest requests at once that can hang a pool for good, when each request takes
    its connections one at a time and holds them until it has them all. They're stuck
    once every connection is held and nobody has all they need: each can hold at most
    connections_per_request - 1. A request that needs one connection can't hang it:
    return math.inf."""
    # TODO
    raise NotImplementedError


def burst(requests, pool_size, connections_per_request=2):
    """Model of 05's Exercise 1: `requests` arrive together and each takes one connection
    while any are free. Then, repeatedly, a request that can get all the connections it
    still needs takes them, finishes and frees everything it held; waiting requests then
    take their first connection. Return how many finish."""
    # TODO
    raise NotImplementedError


def failed_share(finished, errors):
    """Of the requests that got an answer, the share that were errors."""
    # TODO
    raise NotImplementedError


def implied_hold_ms(connections, per_second):
    """Little's law (Chapter 1) turned around: with every connection busy, each request
    holds one for connections ÷ throughput. In ms."""
    # TODO
    raise NotImplementedError

