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,27 @@
defmodule ArcadiaCloud.Billing.CloudBalanceSnapshot do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "cloud_balance_snapshots" do
field :provider, :string
field :month_to_date_balance_cents, :integer
field :account_balance_cents, :integer
field :month_to_date_usage_cents, :integer
field :generated_at, :utc_datetime
field :raw, :map
timestamps(type: :utc_datetime, updated_at: false)
end
@required ~w(provider generated_at)a
@optional ~w(month_to_date_balance_cents account_balance_cents month_to_date_usage_cents raw)a
def changeset(snap, attrs) do
snap
|> cast(attrs, @required ++ @optional)
|> validate_required(@required)
end
end

View File

@@ -0,0 +1,38 @@
defmodule ArcadiaCloud.Billing.CloudCostLine do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "cloud_cost_lines" do
field :invoice_period, :date
field :kind, :string
field :description, :string
field :qty, :decimal
field :unit, :string
field :unit_cost_cents, :integer
field :amount_cents, :integer
field :start_at, :utc_datetime
field :end_at, :utc_datetime
field :project_name, :string
field :category, :string
field :matched_at, :utc_datetime
field :raw, :map
belongs_to :invoice, ArcadiaCloud.Billing.CloudInvoice
belongs_to :resource, ArcadiaCloud.Cloud.CloudResource
timestamps(type: :utc_datetime, updated_at: false)
end
@required ~w(invoice_id invoice_period amount_cents)a
@optional ~w(resource_id kind description qty unit unit_cost_cents start_at end_at
project_name category matched_at raw)a
def changeset(line, attrs) do
line
|> cast(attrs, @required ++ @optional)
|> validate_required(@required)
end
end

View File

@@ -0,0 +1,31 @@
defmodule ArcadiaCloud.Billing.CloudInvoice do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "cloud_invoices" do
field :provider, :string
field :provider_invoice_id, :string
field :invoice_period, :date
field :amount_cents, :integer
field :status, :string, default: "open"
field :issued_at, :utc_datetime
field :csv_fetched_at, :utc_datetime
field :lines_ingested_at, :utc_datetime
field :raw, :map
timestamps(type: :utc_datetime)
end
@required ~w(provider provider_invoice_id invoice_period)a
@optional ~w(amount_cents status issued_at csv_fetched_at lines_ingested_at raw)a
def changeset(invoice, attrs) do
invoice
|> cast(attrs, @required ++ @optional)
|> validate_required(@required)
|> unique_constraint([:provider, :provider_invoice_id])
end
end