ArcadiaCloud.Analytics — the operator margin view. Revenue (tenant
invoices, ex-GST) vs COGS (DO cost lines), margin = revenue - COGS.
- margin_summary/1 — overall P&L + per-tenant + per-deployment margin
for a month.
- by_kind/1 — revenue and COGS broken down by resource kind (separate
axes; billing kinds and DO kinds don't 1:1).
- live_accrual/0 — current-month unbilled: runs the quote engine with
partial metered usage per active subscription. "What tenants are
racking up right now before the rollup."
COGS-to-tenant attribution uses COALESCE(deployment.tenant_id,
resource.tenant_id) — a resource in a deployment bills to the
deployment's tenant; standalone resources fall back to their own
tenant_id (skyai-internal infra).
API (platform_admin only):
- GET /api/v1/dashboard/margin?period=YYYY-MM-DD
- GET /api/v1/dashboard/accrual
Schema fix: cloud_resources.tenant_id and cloud_projects.tenant_id were
binary_id (UUID) while cloud_deployments / tenant_invoices use string.
Migrated both to text — a UUID is a valid string, arcadia also uses
non-UUID tenant slugs ("platform-admin"), and the type alignment lets
the analytics COALESCE join work. Side benefit: kills the phase-1 bug
where a non-UUID tenant_id claim crashed the inventory query.
Smoke verified against real ingested April DO COGS: pilot deployment on
Studio with 5 droplets, metered + rolled up — by_tenant shows
dashboard-pilot rev $71.12 / COGS $30.53 / margin $40.59 (57.1%);
overall P&L revenue $71.12 vs all-April COGS $86.92 = -$15.80 (the
pilot's revenue doesn't cover Sky AI's full April infra — correct, the
rest is internal/unattributed).
Phase 3 complete: catalog, deployments+subscriptions, quote engine,
metering, invoice rollup, cost-vs-revenue dashboard.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
41 lines
1.2 KiB
Elixir
41 lines
1.2 KiB
Elixir
defmodule ArcadiaCloud.Cloud.CloudResource do
|
|
use Ecto.Schema
|
|
import Ecto.Changeset
|
|
|
|
@primary_key {:id, :binary_id, autogenerate: true}
|
|
@foreign_key_type :binary_id
|
|
|
|
schema "cloud_resources" do
|
|
field :provider, :string
|
|
field :provider_id, :string
|
|
field :kind, :string
|
|
field :name, :string
|
|
field :region, :string
|
|
field :status, :string
|
|
field :size_slug, :string
|
|
field :tenant_id, :string
|
|
field :deployment_id, :binary_id
|
|
field :tags, {:array, :string}, default: []
|
|
field :attrs, :map, default: %{}
|
|
field :first_seen_at, :utc_datetime
|
|
field :last_seen_at, :utc_datetime
|
|
field :stale_strike_count, :integer, default: 0
|
|
field :deleted_at, :utc_datetime
|
|
|
|
belongs_to :cloud_project, ArcadiaCloud.Cloud.CloudProject
|
|
|
|
timestamps(type: :utc_datetime)
|
|
end
|
|
|
|
@required ~w(provider provider_id kind name status first_seen_at last_seen_at)a
|
|
@optional ~w(region size_slug cloud_project_id tenant_id deployment_id tags attrs
|
|
stale_strike_count deleted_at)a
|
|
|
|
def changeset(resource, attrs) do
|
|
resource
|
|
|> cast(attrs, @required ++ @optional)
|
|
|> validate_required(@required)
|
|
|> unique_constraint([:provider, :provider_id])
|
|
end
|
|
end
|