Add first-response times to the weekly support report
Node · Node · intermediate · modification
The weekly support report now shows how fast each queue answers. `ticketRepo.listWeekTickets` also selects each ticket's first-response time in whole seconds (null while the ticket has no agent reply). The new `src/reports/responseTimes.js` turns one queue's times into `{ answered, unanswered, medianSeconds, p90Seconds }`, with the mean of the two middle values for an even count and a nearest-rank p90, and `weeklySupportReport.js` adds that summary to every queue row. A queue with nothing answered shows null rather than a misleading 0. Tests for the helper and for the report rows are in `test/reports/responseTimes.test.js` and `test/reports/weeklySupportReport.test.js`.
Helpdesk backend on Node 22 (CommonJS) with Express 4 and PostgreSQL 16 through `pg` 8. Existing and unchanged: `src/db.js` exports the shared `pg` Pool, created with `options: '-c timezone=Europe/Berlin'`, so date arithmetic in SQL (such as the `+ interval '7 days'` week end) runs in Berlin time; `GET /api/reports/support/weekly` (`src/routes/reports.js`) checks `weekStart` (a Monday 00:00 Europe/Berlin, passed on as an ISO 8601 string with its offset), calls `buildWeeklySupportReport` and hands any error to the app's error handler with `next(err)`; the support dashboard reads this JSON, and its new columns ship separately. Tables: `support_queues (id integer primary key, name text not null)`, queues are never deleted; `tickets (id integer primary key, queue_id integer not null references support_queues, created_at timestamptz not null, first_response_at timestamptz null, solved_at timestamptz null)`. `first_response_at` is set once, by the existing reply handler, when the first agent reply is posted, and a CHECK constraint keeps it at or after `created_at`. The product started in 2019, so no time difference comes anywhere near the `integer` range. At most 30 queues and 20,000 tickets are opened in a week in total. The repo has no database-backed tests (`npm test` runs without a database), so SQL changes are checked by hand at review.
Requirements
- Each queue row of the weekly support report (`buildWeeklySupportReport`) gains `firstResponse: { answered, unanswered, medianSeconds, p90Seconds }`, computed over that queue's tickets opened in the report's week. The existing fields (`queueId`, `name`, `opened`, `solved`) and the queue order stay as they are.
- A ticket's first-response time is the whole number of seconds, rounded down, from its creation (`created_at`) to its first agent reply (`first_response_at`). A ticket with no agent reply yet is left out of both times and counted in `unanswered`; `answered + unanswered` equals `opened`.
- `medianSeconds`: with an odd number of answered tickets, the middle value of their sorted times; with an even number, the mean of the two middle values, rounded to the nearest whole second, a half second rounded up.
- `p90Seconds` is the nearest-rank 90th percentile: sort the answered times in ascending order and take the value at 1-based rank ceil(0.9 x n), where n is the number of answered tickets. One answered ticket gives its own time; ten give the 9th smallest.
- A queue with no answered tickets in the week (no tickets, or none answered yet) reports `medianSeconds: null` and `p90Seconds: null`, never 0.
- The new numbers have tests that run with the existing `npm test` (`node --test`).
Files touched
- src/repos/ticketRepo.js
- src/reports/responseTimes.js
- src/reports/weeklySupportReport.js
- test/reports/responseTimes.test.js
- test/reports/weeklySupportReport.test.js