"""Follow one month of traffic through the six steps. Run it from this folder: python3 trace.py

It uses the lesson's own functions and adds no rules of its own: each step calls the function the table in the lesson shows, and this file only prints what it
returns. The prices are the lesson's round toy prices, not a quote.
"""
from rent_vs_own import (DAYS, GPU_HOUR, HOURS_PER_MONTH, PEOPLE, PRICE_IN, PRICE_OUT, REPLIES_PER_GPU_SECOND, SECONDS_PER_MONTH, SPARE, TOKENS_IN, TOKENS_OUT,
                         break_even, busy_share, dollars, gpus_to_pay_for, monthly_own, monthly_rent, replies_for, tokens_words)

TOKENS = 300 * 10**9  # tokens sent in a month, input and output together
HOURS = 24            # the hours of each day in which all the traffic falls

replies = replies_for(TOKENS)
print(f"1. demand  {TOKENS:,} tokens a month = {replies:,} replies of {TOKENS_IN:,} in + {TOKENS_OUT} out")

rent = monthly_rent(replies)
print(f"2. rent    {replies:,} replies at ${PRICE_IN} in and ${PRICE_OUT} out per million tokens = ${dollars(rent):,} a month")

gpus = gpus_to_pay_for(replies, HOURS)
per_second = replies / (DAYS * HOURS * 3600)
print(f"3. GPUs    traffic in {HOURS} hours a day = {per_second:.1f} replies a second at the peak; lesson 29's plan: {gpus - SPARE} GPUs + {SPARE} spare = {gpus}")

own = monthly_own(gpus)
print(f"4. own     {gpus} GPUs x {HOURS_PER_MONTH} hours x ${GPU_HOUR} = ${dollars(gpus * HOURS_PER_MONTH * GPU_HOUR):,}, plus ${PEOPLE:,} for people = ${dollars(own):,} a month")

busy = busy_share(replies, gpus)
print(f"5. busy    {replies:,} replies / ({gpus} GPUs x {REPLIES_PER_GPU_SECOND:.2f} a second x {SECONDS_PER_MONTH:,} seconds) = {busy:.1%}")
print(f"           per million tokens: owning ${own / (TOKENS // 10**6):.4f}, renting ${rent / (TOKENS // 10**6):.4f}")

cross = break_even(HOURS)
if cross:
    print(f"6. cross   owning first costs no more than renting at {tokens_words(cross['tokens'])} tokens a month, with {cross['gpus']} GPUs paid for there")
    print(f"           at {tokens_words(TOKENS)}: owning is ${dollars(rent - own):,} {'cheaper' if own < rent else 'dearer'} than renting")
else:
    print(f"6. cross   no crossing in {HOURS} hours a day: owning stays dearer however many tokens are sent")
    print(f"           at {tokens_words(TOKENS)}: owning is ${dollars(own - rent):,} {'dearer' if own > rent else 'cheaper'} than renting")

print("\nthe same tokens, with all the traffic in fewer hours a day")
for hours in (18, 12, 6):
    gpus = gpus_to_pay_for(replies, hours)
    cross = break_even(hours)
    print(f"  {hours:>2} hours: {gpus} GPUs, owning ${dollars(monthly_own(gpus)):,} against renting ${dollars(rent):,}, busy {busy_share(replies, gpus):.1%}, "
          f"crossing {tokens_words(cross['tokens']) + ' tokens' if cross else 'none'}")

print("\nthe same traffic, with other costs for the people (traffic all day)")
for people in (0, 10_000, 20_000):
    cross = break_even(24, people=people)
    print(f"  ${people:>6,} a month: crossing at {tokens_words(cross['tokens'])} tokens, with {cross['gpus']} GPUs")
