{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "purpose",
      "metadata": {},
      "source": [
        "# Support response report\n",
        "Run the next cell with `support-tickets.csv` in the same directory. All data is synthetic.\n",
        "A first response is late only when it exceeds its target. Missing times are not measurements.\n",
        "This does not measure whether unanswered tickets have already breached their target."
      ]
    },
    {
      "cell_type": "code",
      "id": "report",
      "metadata": {},
      "execution_count": null,
      "outputs": [],
      "source": [
        "import csv\n",
        "from collections import Counter\n",
        "from pathlib import Path\n",
        "from html import escape\n",
        "\n",
        "with Path(\"support-tickets.csv\").open(newline=\"\", encoding=\"utf-8\") as source:\n",
        "    tickets = list(csv.DictReader(source))\n",
        "measured = [t for t in tickets if t[\"first_response_min\"] != \"\"]\n",
        "late = [t for t in measured\n",
        "        if float(t[\"first_response_min\"]) > float(t[\"sla_target_min\"])]\n",
        "rate = len(late) / len(measured) if measured else None\n",
        "rate_text = f\"{rate:.1%}\" if rate is not None else \"No measured responses\"\n",
        "categories = Counter(t[\"category\"] for t in tickets)\n",
        "print(f\"Tickets: {len(tickets)}\")\n",
        "print(f\"Measured first responses: {len(measured)}\")\n",
        "print(f\"Late first responses: {len(late)} ({rate_text})\")\n",
        "\n",
        "rows = \"\".join(\n",
        "    f\"<tr><th scope='row'>{escape(category)}</th><td>{count}</td></tr>\"\n",
        "    for category, count in sorted(categories.items(), key=lambda item: (-item[1], item[0]))\n",
        ")\n",
        "report = f\"\"\"<!doctype html>\n",
        "<html lang=\"en\"><meta charset=\"utf-8\">\n",
        "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n",
        "<title>Support response report</title>\n",
        "<style>\n",
        "body {{ font: 1.1rem/1.6 system-ui; max-width: 44rem; margin: 3rem auto;\n",
        "        padding: 1.5rem; color: #17212e; background: #f4f7fb; }}\n",
        "section {{ padding: 1.5rem; background: white; border-radius: 1rem; }}\n",
        "table {{ border-collapse: collapse; width: 100%; }}\n",
        "th, td {{ text-align: left; padding: .65rem; border-bottom: 1px solid #ccd4df; }}\n",
        "</style>\n",
        "<h1>Support response report</h1>\n",
        "<p>Synthetic demonstration data. Missing response times are excluded from the rate.</p>\n",
        "<section><h2>{rate_text} of measured first responses were late</h2>\n",
        "<p>{len(tickets)} tickets · {len(measured)} measured responses · {len(late)} late responses</p>\n",
        "<table><caption>Ticket volume by category</caption>\n",
        "<thead><tr><th scope=\"col\">Category</th><th scope=\"col\">Tickets</th></tr></thead>\n",
        "<tbody>{rows}</tbody></table></section>\n",
        "</html>\"\"\"\n",
        "Path(\"support-report.html\").write_text(report, encoding=\"utf-8\")\n",
        "print(\"Saved support-report.html\")\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3 (ipykernel)",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
