# /// script
# requires-python = ">=3.10"
# ///
# Copyright (c) 2026 Aayush Sharma · SPDX-License-Identifier: MIT
# https://thedataengineering.com/lab-code/LICENSE.txt
"""Price a workload from the metering.

One workload billed three ways: quantity x rate, with a minimum billing
increment (billed = ceil(q / g) x g x r), and after batching jobs into each
billed slot. Then effective price = rate / utilization for a cheap, idle
engine against a pricier, busy one.
"""
import argparse, json, math

p = argparse.ArgumentParser(description=__doc__.splitlines()[0])
p.add_argument("--jobs", type=int, default=3, help="Jobs per run.")
p.add_argument("--job-seconds", type=float, default=5, help="Seconds each job runs.")
p.add_argument("--runs-per-day", type=int, default=1000, help="Runs per day.")
p.add_argument("--minimum", type=float, default=60, help="Billing increment g, seconds (the minimum charge).")
p.add_argument("--rate", type=float, default=0.0005, help="Price per second of compute.")
p.add_argument("--batch", type=int, default=12, help="Jobs packed into one billed slot.")
p.add_argument("--json", help="Also write the results to this JSON file.")
args = p.parse_args()

n = args.jobs * args.runs_per_day                       # jobs per day
q = args.job_seconds
a = n * q * args.rate                                   # (a) quantity x rate
b = n * math.ceil(q / args.minimum) * args.minimum * args.rate          # (b) each job rounded up to g
slots = math.ceil(n / args.batch)
c = slots * math.ceil(args.batch * q / args.minimum) * args.minimum * args.rate   # (c) batch jobs per slot
bills = [{"method": "(a) quantity x rate", "per_day": a}, {"method": "(b) rounded up to the minimum", "per_day": b},
         {"method": f"(c) {args.batch} jobs per slot", "per_day": c}]
for r in bills: print(f"{r['method']:<32} {r['per_day']:8.2f} per day   ({r['per_day'] / a:5.1f}x quantity x rate)")

engines = [{"engine": "cheap rate, 20% utilized", "rate": 1.0, "utilization": 0.2},
           {"engine": "3x the rate, 80% utilized", "rate": 3.0, "utilization": 0.8}]
for e in engines:
    e["effective"] = e["rate"] / e["utilization"]
    print(f"{e['engine']:<28} rate {e['rate']:.1f}  utilization {e['utilization']:.0%}  effective price {e['effective']:.2f}")

if args.json:
    with open(args.json, "w") as f:
        json.dump({"tables": {"bills": bills, "engines": engines, "summary": [{"rounding_x": b / a, "batched_x": c / a}]}}, f)
