#!/usr/bin/env python3
"""Build the "Where are tickets stuck?" dashboard from a ticket export.
Usage: python3 output/build.py [tickets.csv] [dashboard.html]
Defaults: sample/tickets.csv and output/dashboard.html.
Every number on the page is computed here from the CSV. Nothing is typed in.
Python standard library only. The page needs no network and no JavaScript.
"""
import csv
import html
import sys
from datetime import date, timedelta
from pathlib import Path
STAGES = ["Queue", "Build", "Review", "Blocked", "Done"] # process order
OPEN_STAGES = STAGES[:-1]
DATE_COLUMNS = ["opened", "stage_entered", "closed"]
csv_path = Path(sys.argv[1] if len(sys.argv) > 1 else "sample/tickets.csv")
html_path = Path(sys.argv[2] if len(sys.argv) > 2 else "output/dashboard.html")
def day(text):
return date.fromisoformat(text.strip())
def nice(d):
return f"{d:%b} {d.day}, {d.year}"
def short(d):
return f"{d:%b} {d.day}"
def plural(n, word):
return f"{n} {word}" if n == 1 else f"{n} {word}s"
# 1. Read every row.
with csv_path.open(newline="", encoding="utf-8") as f:
rows = [{k: (v or "").strip() for k, v in r.items()} for r in csv.DictReader(f)]
rows_read = len(rows)
# 2. Data through = the latest date anywhere in the file (never today's date).
data_through = max(day(r[c]) for r in rows for c in DATE_COLUMNS if r[c])
week_earlier = data_through - timedelta(days=7)
# 3. Count an exact duplicate row once.
seen, unique, duplicate_ids = set(), [], []
for r in rows:
key = tuple(r.values())
if key in seen:
duplicate_ids.append(r["id"])
else:
seen.add(key)
unique.append(r)
# 4. Only rows where kind is Ticket count.
tickets = [r for r in unique if r["kind"] == "Ticket"]
other_kinds = {}
for r in unique:
if r["kind"] != "Ticket":
other_kinds[r["kind"]] = other_kinds.get(r["kind"], 0) + 1
not_tickets = sum(other_kinds.values())
# 5. Open now: closed is blank.
open_now = [t for t in tickets if not t["closed"]]
# 6. Open 7 days earlier: opened on or before that day, not closed on or before it.
open_then = [
t for t in tickets
if day(t["opened"]) <= week_earlier and (not t["closed"] or day(t["closed"]) > week_earlier)
]
change = len(open_now) - len(open_then)
# 7. Open tickets by stage. A stage outside the list is unmapped, never fixed.
by_stage = {s: 0 for s in OPEN_STAGES}
unmapped = {}
for t in open_now:
if t["stage"] in by_stage:
by_stage[t["stage"]] += 1
else:
unmapped.setdefault(t["stage"], []).append(t["id"])
unmapped_count = sum(len(ids) for ids in unmapped.values())
bars_total = sum(by_stage.values())
accent = max(OPEN_STAGES, key=lambda s: by_stage[s]) # stage with the most open tickets
# 8. Age = data-through date minus opened. Average is rounded once, at display.
for t in open_now:
t["age"] = (data_through - day(t["opened"])).days
average_age_exact = sum(t["age"] for t in open_now) / len(open_now) if open_now else 0
average_age = int(average_age_exact + 0.5)
# 9. Ten oldest open tickets, oldest first.
oldest = sorted(open_now, key=lambda t: (day(t["opened"]), t["id"]))[:10]
in_accent = sum(1 for t in oldest if t["stage"] == accent)
# Print every number used anywhere.
print(f"rows read: {rows_read}")
print(f"data through: {data_through.isoformat()} ({nice(data_through)})")
print(f"exact duplicate rows counted once: {len(duplicate_ids)} ({', '.join(duplicate_ids) or 'none'})")
print(f"rows that are not tickets, excluded: {not_tickets} ({', '.join(f'{k} {v}' for k, v in other_kinds.items()) or 'none'})")
print(f"tickets: {len(tickets)}")
print(f"open tickets now: {len(open_now)}")
print(f"open tickets 7 days earlier ({week_earlier.isoformat()}): {len(open_then)}")
print(f"change: {change:+d}")
for s in OPEN_STAGES:
print(f"open in {s}: {by_stage[s]}")
print(f"open with an unmapped stage: {unmapped_count} ({'; '.join(f'{k}: {', '.join(v)}' for k, v in unmapped.items()) or 'none'})")
print(f"bars plus unmapped: {bars_total} + {unmapped_count} = {bars_total + unmapped_count}")
print(f"stage with the most open tickets: {accent} ({by_stage[accent]})")
print(f"average age of open tickets: {average_age} days (exact {average_age_exact:.4f}, rounded once)")
print("ten oldest open tickets:")
for t in oldest:
print(f" {t['id']} | {t['title']} | {t['stage']} | {t['owner']} | opened {t['opened']} | {t['age']} days")
print(f"of those ten, in {accent}: {in_accent}")
# 10. Who to nudge: owners of open tickets in the bottleneck stage, oldest ticket first.
stuck = sorted((t for t in open_now if t["stage"] == accent), key=lambda t: (-t["age"], t["id"]))
nudge = {}
for t in stuck:
nudge.setdefault(t["owner"], []).append(t) # owners end up ordered by their oldest ticket
print(f"bottleneck stage (most open tickets): {accent}")
print(f"who to nudge: {len(nudge)} owners, {len(stuck)} tickets in {accent}")
for owner, ts in nudge.items():
print(f" {owner}: {len(ts)} in {accent} | " + ", ".join(f"{t['id']} {t['age']} days" for t in ts))
# Titles state the finding, built from the computed numbers.
verdict_lead = " is the bottleneck:"
verdict_rest = f"{by_stage[accent]} of {len(open_now)} open tickets, {in_accent} of the {len(oldest)} oldest."
if change > 0:
trend = f"{change} more than a week ago"
elif change < 0:
trend = f"{-change} fewer than a week ago"
else:
trend = "the same as a week ago"
context_line = (
f"{plural(len(open_now), 'ticket')} {'is' if len(open_now) == 1 else 'are'} open, {trend} "
f"({len(open_then)} on {short(week_earlier)}). Average age of open tickets: {average_age} days."
)
nudge_title = f"Who to nudge: {'1 person' if len(nudge) == 1 else f'{len(nudge)} people'} with tickets in {accent}"
bars_title = f"Where the {len(open_now)} open tickets sit"
table_title = (
f"The {len(oldest)} oldest open tickets: {in_accent} in {accent}, the oldest has waited {oldest[0]['age']} days"
if oldest else "No open tickets"
)
e = html.escape
longest = max(by_stage.values()) or 1
bars_html = ""
for s in OPEN_STAGES:
width = by_stage[s] / longest * 100 # bars start at zero
cls = "bar accent" if s == accent else "bar"
bars_html += (
f'
\n'
)
if unmapped:
parts = [f"“{e(k)}” ({e(', '.join(v))})" for k, v in unmapped.items()]
unmapped_html = (
f"{unmapped_count} unmapped, not fixed and not in a bar: {'; '.join(parts)}. "
f"{bars_total} in the bars + {unmapped_count} unmapped = {len(open_now)} open."
)
else:
unmapped_html = f"No unmapped stages. The bars add up to {bars_total} open."
nudge_html = ""
for owner, ts in nudge.items():
chips = "".join(f'{e(t["id"])} {t["age"]}d' for t in ts)
nudge_html += (
f'{e(owner)}'
f'{plural(len(ts), "ticket")}'
f'{chips}\n'
)
oldest_age = oldest[0]["age"] if oldest else 1
rows_html = ""
for t in oldest:
hot = " hot" if t["stage"] == accent else ""
width = t["age"] / oldest_age * 100 # age bars start at zero
rows_html += (
f'| {e(t["id"])} | '
f'{e(t["title"])} | '
f'{e(t["stage"])} | '
f'{e(t["owner"])} | '
f'{short(day(t["opened"]))} | '
f'{t["age"]} days'
f' |
\n'
)
dup_text = f"{plural(len(duplicate_ids), 'exact duplicate row')} ({', '.join(duplicate_ids)}) counted once" if duplicate_ids else "no duplicate rows"
kind_text = (
f"{plural(not_tickets, 'row')} with another kind ({', '.join(f'{k}: {v}' for k, v in other_kinds.items())}) left out"
if not_tickets else "no rows of another kind"
)
unmapped_text = (
"; ".join(f'"{k}" on {", ".join(v)}' for k, v in unmapped.items())
+ " is counted as open and reported as unmapped. It was not changed to another stage and is not in any bar."
if unmapped else "No stage was unmapped."
)
definitions = [
("Rows", f"{rows_read}", "Data rows read from the file, not counting the header.", "all columns"),
("Data through", nice(data_through), "The latest date anywhere in the file. Today's date is never used.", "opened, stage_entered, closed"),
("Ticket", f"{len(tickets)}", f"A row where kind is Ticket, with {dup_text} and {kind_text}.", "kind, and every column for the duplicate check"),
("Open tickets now", f"{len(open_now)}", "Tickets where closed is blank.", "kind, closed"),
("Open tickets 7 days earlier", f"{len(open_then)}", f"Tickets opened on or before {nice(week_earlier)} and not closed on or before that day.", "kind, opened, closed"),
("Open tickets by stage", ", ".join(f"{s} {by_stage[s]}" for s in OPEN_STAGES), f"Open tickets grouped by stage, in process order. {unmapped_text}", "kind, closed, stage"),
("Bottleneck", f"{accent}, with {by_stage[accent]} open tickets", "The stage with the most open tickets. It is a count, not a judgment about the people in that stage.", "kind, closed, stage"),
("Who to nudge", f"{plural(len(nudge), 'owner')}, {plural(len(stuck), 'ticket')}", f"Every owner of an open ticket in {accent}, with each of those tickets and its age. Owners are ordered by their oldest ticket.", "kind, closed, stage, owner, opened"),
("Average age of open tickets", f"{average_age} days", f"Data-through date minus opened, averaged over open tickets. Exact value {average_age_exact:.4f}, rounded once, at display.", "kind, closed, opened"),
("Ten oldest open tickets", f"{oldest[0]['id']} is the oldest, opened {nice(day(oldest[0]['opened']))}" if oldest else "none", "Open tickets sorted by opened, oldest first. Age is the data-through date minus opened, in days. The small bar next to each age starts at zero and is as long as the age.", "kind, closed, opened"),
]
defs_html = "".join(
f"{e(n)}: {e(v)}{e(d)} Columns: {e(c)}.\n"
for n, v, d, c in definitions
)
page = f"""
Where are tickets stuck? Data through {nice(data_through)}
{e(accent)}{e(verdict_lead)} {e(verdict_rest)}
{e(context_line)}
{e(bars_title)}
{bars_html}
{unmapped_html}
{e(table_title)}
| ID | Ticket | Stage | Owner | Opened | Age, days since opened |
{rows_html}
Definitions and how rows were counted
{defs_html}
"""
html_path.parent.mkdir(parents=True, exist_ok=True)
html_path.write_text(page, encoding="utf-8")
# The same definitions as a Markdown file, next to the page.
md = ["# Definitions", "", f"Data through {nice(data_through)} ยท {rows_read} rows. Written by build.py from {csv_path.name}.", ""]
for n, v, d, c in definitions:
md.append(f"- **{n}: {v}.** {d} Columns: {c}.")
(html_path.parent / "definitions.md").write_text("\n".join(md) + "\n", encoding="utf-8")
print(f"wrote {html_path} and {html_path.parent / 'definitions.md'}")