Build Call History from Asterisk's call records #81

Merged
Patrick merged 1 commit from patrick/call-history into main 2026-11-16 14:38:03 -05:00 AGit
Owner

Nothing ever wrote call_logs. The /api/call-logs endpoint had no caller, so Call History was always empty, there was no data for billing, and recordings had nothing to attach to. A Dallas client's call couldn't be traced for the same reason.

How it works

  • Asterisk → Postgres. cdr_adaptive_odbc writes every call that reaches the dialplan, answered or not, to a new asterisk_cdr table. It uses the same ODBC connection as realtime. Calls PJSIP rejects before the dialplan (the SIP scanners) never get a record.

  • Postgres → Call History. orbital:ingest-cdrs runs every minute. It groups records by linkedid and rebuilds one call_logs row per call, so it's idempotent and a late record corrects the call. Each row gets:

    • the client, DID, caller and direction
    • a status: completed, missed, busy, failed or AI unavailable
    • start, answer and end times, duration and billable seconds
    • the extension, queue or AI persona that handled it, and the outcome (ai, extension, queue, voicemail, ai_unavailable)
    • a per-leg breakdown in metadata

    Raw records are kept for 30 days after ingest.

  • Tagging. The from-trunk dispatcher tags inbound calls with the client, routing rule, DID and the carrier's SIP Call-ID (what edge recordings are keyed by). Calls between a client's own extensions are attributed from their tenant_N context.

  • AI calls. The AI leg now carries X-Orbital-Call (the linkedid). The worker sends it with the call context, and sends the transcript (caller and agent turns only) when the session ends, so AI calls get their persona and transcript whichever arrives first.

  • UI. Call History (portal) and Call Logs (admin) get:

    • status labels and colours
    • h:mm:ss durations
    • a Billable column (admin)
    • a status filter (admin)
    • a Transcript viewer

Checked against the Asterisk 22 source, not only tests

  • adaptive_odbc writes disposition as a number (8 = answered). It writes an unanswered leg's answer time as the epoch. The ingester normalises both, and the tests use that real format.
  • Columns Asterisk has no value for (id, ingested_at) are left out of its INSERT.
  • alias start => started_at is the right syntax (end is reserved in SQL).
  • Custom CDR() variables carry over to a channel's later CDRs. CHANNEL(pjsip,call-id) exists.
  • Migrations run pre-upgrade, before Asterisk restarts. config-sync also reloads cdr_adaptive_odbc with every dialplan reload as a safety net.

Not in this PR

  • Recording. Kamailio never asks rtpengine to record, and Dallas has no LiveKit egress, so there are no recordings to attach yet. That's a separate edge change in orbital-setup.
  • Calls before this ships. They aren't recoverable.

Tests

  • CallHistoryIngestTest: 14 tests.
  • Full suite: 514 passed, 8 skipped.

🤖 Generated with Claude Code

Nothing ever wrote `call_logs`. The `/api/call-logs` endpoint had no caller, so Call History was always empty, there was no data for billing, and recordings had nothing to attach to. A Dallas client's call couldn't be traced for the same reason. ## How it works - **Asterisk → Postgres.** `cdr_adaptive_odbc` writes every call that reaches the dialplan, answered or not, to a new `asterisk_cdr` table. It uses the same ODBC connection as realtime. Calls PJSIP rejects before the dialplan (the SIP scanners) never get a record. - **Postgres → Call History.** `orbital:ingest-cdrs` runs every minute. It groups records by `linkedid` and rebuilds one `call_logs` row per call, so it's idempotent and a late record corrects the call. Each row gets: - the client, DID, caller and direction - a status: completed, missed, busy, failed or **AI unavailable** - start, answer and end times, duration and **billable seconds** - the extension, queue or AI persona that handled it, and the outcome (`ai`, `extension`, `queue`, `voicemail`, `ai_unavailable`) - a per-leg breakdown in `metadata` Raw records are kept for 30 days after ingest. - **Tagging.** The from-trunk dispatcher tags inbound calls with the client, routing rule, DID and the carrier's SIP Call-ID (what edge recordings are keyed by). Calls between a client's own extensions are attributed from their `tenant_N` context. - **AI calls.** The AI leg now carries `X-Orbital-Call` (the linkedid). The worker sends it with the call context, and sends the transcript (caller and agent turns only) when the session ends, so AI calls get their persona and transcript whichever arrives first. - **UI.** Call History (portal) and Call Logs (admin) get: - status labels and colours - `h:mm:ss` durations - a Billable column (admin) - a status filter (admin) - a Transcript viewer ## Checked against the Asterisk 22 source, not only tests - `adaptive_odbc` writes `disposition` as a number (8 = answered). It writes an unanswered leg's answer time as the epoch. The ingester normalises both, and the tests use that real format. - Columns Asterisk has no value for (`id`, `ingested_at`) are left out of its INSERT. - `alias start => started_at` is the right syntax (`end` is reserved in SQL). - Custom `CDR()` variables carry over to a channel's later CDRs. `CHANNEL(pjsip,call-id)` exists. - Migrations run `pre-upgrade`, before Asterisk restarts. config-sync also reloads `cdr_adaptive_odbc` with every dialplan reload as a safety net. ## Not in this PR - **Recording.** Kamailio never asks rtpengine to record, and Dallas has no LiveKit egress, so there are no recordings to attach yet. That's a separate edge change in orbital-setup. - **Calls before this ships.** They aren't recoverable. ## Tests - `CallHistoryIngestTest`: 14 tests. - Full suite: 514 passed, 8 skipped. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Build Call History from Asterisk's call records
All checks were successful
Application Tests / frontend (pull_request) Successful in 26s
Application Tests / security-audit (pull_request) Successful in 2m25s
Application Tests / php (pull_request) Successful in 3m6s
Application Tests / tag (pull_request) Has been skipped
Application Tests / publish-images (., Dockerfile, laravel) (pull_request) Has been skipped
Application Tests / publish-images (agent-worker, Dockerfile, agent-worker) (pull_request) Has been skipped
Application Tests / publish-images (docker/asterisk, Dockerfile, asterisk) (pull_request) Has been skipped
Application Tests / publish-images (docker/asterisk-config-sync, Dockerfile, asterisk-config-sync) (pull_request) Has been skipped
Application Tests / publish-images (docker/haraka, Dockerfile, haraka) (pull_request) Has been skipped
Application Tests / publish-images (docker/whisper-local, Dockerfile, whisper-local) (pull_request) Has been skipped
Application Tests / scan-images (agent-worker) (pull_request) Has been skipped
Application Tests / scan-images (asterisk) (pull_request) Has been skipped
Application Tests / scan-images (asterisk-config-sync) (pull_request) Has been skipped
Application Tests / scan-images (haraka) (pull_request) Has been skipped
Application Tests / scan-images (laravel) (pull_request) Has been skipped
Application Tests / scan-images (whisper-local) (pull_request) Has been skipped
Application Tests / publish-helm (pull_request) Has been skipped
Application Tests / publish-archives (pull_request) Has been skipped
c52cf97001
Nothing ever wrote call_logs: the /api/call-logs endpoint had no caller,
so Call History, billing data and recording matching were always empty.

Asterisk now writes every call that reaches the dialplan, answered or
not, to Postgres (cdr_adaptive_odbc -> asterisk_cdr). A scheduled
orbital:ingest-cdrs groups the records by linkedid into one call_logs
row per call: client, DID, caller, direction, status (completed, missed,
busy, failed, ai_unavailable), start/answer/end, duration and billable
seconds, and the extension, queue or AI persona that took it.

- The dispatcher tags inbound calls with client, rule, DID and the
  carrier's SIP Call-ID; calls between a client's own extensions are
  attributed from their tenant context.
- The AI leg carries X-Orbital-Call (the linkedid); the worker reports
  it with the call context, and sends the transcript when the session
  ends, so AI calls get their persona and transcript.
- Call History and Call Logs show the new statuses, h:mm:ss durations,
  billable time, and a transcript viewer.
- config-sync reloads cdr_adaptive_odbc with the dialplan, in case
  Asterisk started before the table existed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
calltheory/orbital!81
No description provided.