Phase 1 cost ingestion: balance + invoices + CSV parse + resource match

Three new schemas:
- cloud_balance_snapshots — hourly MTD balance/usage poll for live-accrual.
- cloud_invoices — header per provider invoice, with ingest status flags.
- cloud_cost_lines — per-line-item COGS, FK to cloud_resources where matched.

Three new Oban workers (queue: cloud_billing):
- BalanceWorker (hourly) records a snapshot.
- BillingHistoryWorker (daily) discovers invoices via /v2/customers/my/
  billing_history, upserts headers, enqueues an InvoiceIngestWorker for
  each not-yet-ingested invoice.
- InvoiceIngestWorker (per-invoice) fetches /invoices/:uuid/csv, parses
  with NimbleCSV (header-keyed so column order shifts don't break us),
  replaces the invoice's line set, then matches lines to cloud_resources
  by (kind, name) — case-insensitive, name extracted from "name (size)"
  description format.

DigitalOcean.Client gains get_balance / list_billing_history /
get_invoice_summary / fetch_invoice_csv. The CSV endpoint returns text/csv
so we bypass Req's body decoder.

Cron additions: BalanceWorker hourly at :07, BillingHistoryWorker daily
at 02:23.

API:
- GET /api/v1/billing/balance — latest snapshot, platform_admin only.
- GET /api/v1/billing/cost-lines?period=YYYY-MM-DD&kind&limit — per-line
  COGS, platform_admin only.

Live smoke against real DO billing API surfaced and fixed three CSV-format
gotchas: column headers use underscores not spaces (group_description,
project_name), USD column has $ prefix, dates use "YYYY-MM-DD HH:MM:SS
+0000" format (space separator + RFC822 offset).

Verified: 137 historical invoices discovered going back to 2014;
April 2026 invoice (33 lines, $86.92 total) ingested with 6/33 lines
matched to current cloud_resources. Unmatched lines are correctly
historic droplets, Spaces buckets (not yet synced), and GST.

NimbleCSV ~> 1.2 added as a dep.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-19 22:20:50 +10:00
parent 53b664558d
commit 0079f98bb5
14 changed files with 826 additions and 2 deletions

View File

@@ -0,0 +1,69 @@
defmodule ArcadiaCloud.Repo.Migrations.CreateBilling do
use Ecto.Migration
def change do
# Hourly snapshot of DO account balance + month-to-date usage.
# Drives the live-accrual side of the cost dashboard.
create table(:cloud_balance_snapshots, primary_key: false) do
add :id, :binary_id, primary_key: true
add :provider, :string, null: false
add :month_to_date_balance_cents, :integer
add :account_balance_cents, :integer
add :month_to_date_usage_cents, :integer
add :generated_at, :utc_datetime, null: false
add :raw, :map
timestamps(type: :utc_datetime, updated_at: false)
end
create index(:cloud_balance_snapshots, [:provider, :generated_at])
# Monthly invoice summary headers (one row per provider invoice).
create table(:cloud_invoices, primary_key: false) do
add :id, :binary_id, primary_key: true
add :provider, :string, null: false
add :provider_invoice_id, :string, null: false
add :invoice_period, :date, null: false
add :amount_cents, :integer
add :status, :string, default: "open", null: false
add :issued_at, :utc_datetime
add :csv_fetched_at, :utc_datetime
add :lines_ingested_at, :utc_datetime
add :raw, :map
timestamps(type: :utc_datetime)
end
create unique_index(:cloud_invoices, [:provider, :provider_invoice_id])
create index(:cloud_invoices, [:invoice_period])
# Per-line-item COGS. One row per CSV line of a provider invoice.
# Matched to cloud_resources where possible by (kind, name, region).
create table(:cloud_cost_lines, primary_key: false) do
add :id, :binary_id, primary_key: true
add :invoice_id, references(:cloud_invoices, type: :binary_id, on_delete: :delete_all),
null: false
add :resource_id, references(:cloud_resources, type: :binary_id, on_delete: :nilify_all)
add :invoice_period, :date, null: false
add :kind, :string
add :description, :string
add :qty, :decimal
add :unit, :string
add :unit_cost_cents, :integer
add :amount_cents, :integer, null: false
add :start_at, :utc_datetime
add :end_at, :utc_datetime
add :project_name, :string
add :category, :string
add :matched_at, :utc_datetime
add :raw, :map
timestamps(type: :utc_datetime, updated_at: false)
end
create index(:cloud_cost_lines, [:invoice_id])
create index(:cloud_cost_lines, [:resource_id])
create index(:cloud_cost_lines, [:invoice_period])
create index(:cloud_cost_lines, [:kind])
end
end