Files
arcadia-cloud/config/config.exs
Giuliano Silvestro 501e333811 Phase 3: usage metering
usage_records — one metered observation per (deployment, resource,
period, granularity). resource_kind is the BILLING kind (droplet_hours,
spaces_gb_month, ...) so it joins straight to plan_items. sub_attribution
jsonb column present from day one for forward-compat (sub-billing) but
never written in phase 3.

metering_config — per-billing-kind granularity (daily default; flip to
hourly when fine-grained billing is wanted) + retention_days.

ArcadiaCloud.Metering:
- meter_day/1 — walks every deployment-attributed resource, maps its
  inventory kind to a billing kind, records that day's usage. Idempotent
  via the unique (deployment, resource, period, granularity) index.
- usage_for_period/3 — SUM(qty) GROUP BY billing kind over a date range,
  returns the %{kind => qty} map the quote engine takes as :usage.

Uniform accrual model so everything downstream is just SUM(qty):
- droplet      -> droplet_hours, 24/day when active (0 when off)
- spaces_bucket / snapshot / droplet_backup -> *_gb_month, size/days so
  the month sums to GB-months
- dns_zone     -> dns_zones, 1/days so the month sums to the zone count

MeteringWorker — Oban cron 01:10 UTC daily, meters the previous
complete day.

Smoke verified: a pilot deployment with 2 droplets + 2 DNS zones + 3
snapshots attributed; 10 days metered -> 70 usage records; aggregation
gave droplet_hours 480 (2x24x10), dns_zones 0.65 (2x10/31),
snapshot_gb_month 15.48; fed into the quote engine against Studio — all
within allowance so $50 base, no overage (correct for a light partial
month).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-20 15:23:12 +10:00

80 lines
2.8 KiB
Elixir

# This file is responsible for configuring your application
# and its dependencies with the aid of the Config module.
#
# This configuration file is loaded before any dependency and
# is restricted to this project.
# General application configuration
import Config
config :arcadia_cloud,
ecto_repos: [ArcadiaCloud.Repo],
generators: [timestamp_type: :utc_datetime, binary_id: true]
# Configure the endpoint
config :arcadia_cloud, ArcadiaCloudWeb.Endpoint,
url: [host: "localhost"],
adapter: Bandit.PhoenixAdapter,
render_errors: [
formats: [json: ArcadiaCloudWeb.ErrorJSON],
layout: false
],
pubsub_server: ArcadiaCloud.PubSub,
live_view: [signing_salt: "4W6q5pDB"]
# Configure Elixir's Logger
config :logger, :default_formatter,
format: "$time $metadata[$level] $message\n",
metadata: [:request_id]
# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason
# Guardian — JWTs are issued by arcadia-app. arcadia-cloud only verifies them.
# Issuer and secret_key MUST match arcadia-app's Arcadia.Guardian config.
config :arcadia_cloud, ArcadiaCloud.Guardian,
issuer: "arcadia",
verify_issuer: true
# Oban — provisioning, sync, billing, gateway-event consumption queues
config :arcadia_cloud, Oban,
engine: Oban.Engines.Basic,
queues: [
provisioning: 5,
cloud_sync_fast: 5,
cloud_sync_full: 3,
cloud_sync_slow: 1,
cloud_billing: 1,
metering: 2,
default: 5
],
plugins: [
{Oban.Plugins.Cron,
crontab: [
# ProjectsWorker first so attribution is fresh before resource syncs
{"*/15 * * * *", ArcadiaCloud.Sync.ProjectsWorker},
{"*/15 * * * *", ArcadiaCloud.Sync.DropletsWorker},
{"*/15 * * * *", ArcadiaCloud.Sync.DomainsWorker},
{"*/15 * * * *", ArcadiaCloud.Sync.VolumesWorker},
{"*/15 * * * *", ArcadiaCloud.Sync.FloatingIpsWorker},
{"*/15 * * * *", ArcadiaCloud.Sync.FirewallsWorker},
{"*/15 * * * *", ArcadiaCloud.Sync.LoadBalancersWorker},
# Snapshots change slowly; hourly is enough and reduces API churn
{"33 * * * *", ArcadiaCloud.Sync.SnapshotsWorker},
# Backups also slow-moving; hourly per-droplet walk
{"41 * * * *", ArcadiaCloud.Sync.BackupsWorker},
# Drift sweep — offset past the :15 resource syncs so it sees fresh data
{"20 * * * *", ArcadiaCloud.Sync.DriftDetectionWorker},
# Metering — daily at 01:10 UTC, meters the previous complete day
{"10 1 * * *", ArcadiaCloud.Sync.MeteringWorker},
# Billing: hourly balance, daily invoice discovery
{"7 * * * *", ArcadiaCloud.Sync.BalanceWorker},
{"23 2 * * *", ArcadiaCloud.Sync.BillingHistoryWorker}
]}
],
repo: ArcadiaCloud.Repo
# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{config_env()}.exs"