# metrics.py: MTTR by priority, SLA adherence, and the breach list from sample-remediation-log.csv.
# usage: python3 metrics.py sample-remediation-log.csv
import csv, sys
from datetime import date

def d(s):
    y, m, dd = map(int, s.split("-"))
    return date(y, m, dd)

ttr = {"P1": [], "P2": [], "P3": [], "P4": []}
on_time, total, breaches = 0, 0, []
for row in csv.DictReader(open(sys.argv[1])):
    days = (d(row["resolved_date"]) - d(row["found_date"])).days
    ttr[row["priority"]].append(days)
    total += 1
    if d(row["resolved_date"]) <= d(row["due_date"]):
        on_time += 1
    else:
        breaches.append(f"{row['id']} ({row['priority']})")

for p, days in ttr.items():
    print(f"MTTR {p}: {sum(days) / len(days):.1f} days  (n={len(days)})")
every = [x for v in ttr.values() for x in v]
print(f"MTTR blended: {sum(every) / len(every):.1f} days")
print(f"SLA adherence: {on_time}/{total} = {on_time * 100 // total}%")
print(f"breaches: {', '.join(breaches)}")
