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

@@ -21,6 +21,52 @@ defmodule ArcadiaCloud.DigitalOcean.Client do
def list_volumes(opts \\ []), do: list_paginated("/volumes", "volumes", opts)
def list_floating_ips(opts \\ []), do: list_paginated("/floating_ips", "floating_ips", opts)
# ---- billing --------------------------------------------------------------
def get_balance(opts \\ []) do
request(:get, "/customers/my/balance", purpose: opts[:purpose] || "billing")
end
def list_billing_history(opts \\ []) do
list_paginated("/customers/my/billing_history", "billing_history",
Keyword.put(opts, :purpose, opts[:purpose] || "billing"))
end
def get_invoice_summary(invoice_uuid, opts \\ []) do
request(:get, "/customers/my/invoices/#{invoice_uuid}/summary",
purpose: opts[:purpose] || "billing")
end
@doc """
Fetch the CSV body for an invoice. Returns {:ok, csv_string} | {:error, _}.
"""
def fetch_invoice_csv(invoice_uuid, opts \\ []) do
purpose = opts[:purpose] || "billing"
with {:ok, token} <- Tokens.fetch(purpose) do
case Req.request(
method: :get,
url: @base <> "/customers/my/invoices/#{invoice_uuid}/csv",
headers: [
{"authorization", "Bearer " <> token},
{"accept", "text/csv"}
],
retry: :transient,
max_retries: 3,
decode_body: false
) do
{:ok, %Req.Response{status: 200, body: body}} when is_binary(body) ->
{:ok, body}
{:ok, %Req.Response{status: status, body: body}} ->
{:error, {:http, status, body}}
{:error, e} ->
{:error, {:transport, e}}
end
end
end
def create_project(name, purpose, description \\ "", opts \\ []) do
body = %{
name: name,