Usage & Cost¶
Per-app usage records and billing rollups. Usage is metered per app; billing is aggregated per group. Costs are derived from the pricing table (the rate in effect at record creation is snapshotted onto each row).
Environment
These routes take no environment query parameter — the API process is
bound to a single environment and resolves it server-side.
Scope
All routes are caller-scoped. A tenant sees only their own group's apps;
a platform-admin caller (platform_wide) spans every group. Cross-tenant
lookups return 404 (existence-hiding), never 403.
Endpoints¶
| Method | Path | Scope | Description |
|---|---|---|---|
GET |
/api/usage/{app_name} |
read | Raw usage records for an app |
GET |
/api/usage/{app_name}/summary |
read | Billing summary for one app |
GET |
/api/usage/report/all |
read | Usage across every app the caller can see |
GET |
/api/usage/group/{group_slug}/summary |
read | Per-group invoice-shaped rollup |
POST |
/api/admin/usage/backup-storage/repair |
platform-admin | Close the open backup_storage records of bundles that no longer exist (dry run unless apply) |
The last route is not caller-scoped: it works across every group and is described under Repair open backup-storage records.
Usage records¶
| Query | Type | Default | Notes |
|---|---|---|---|
record_type |
string | — | Filter by record type (e.g. build, container_runtime) |
since |
ISO-8601 | — | Only records started at/after this timestamp (422 if unparseable) |
status |
string | — | Filter by record status |
limit |
int | 100 |
Max 500 |
Response: 200 OK
{
"records": [
{
"id": "550e8400-...",
"app_name": "gallery",
"branch": "main",
"service_name": "web",
"record_type": "container_runtime",
"status": "completed",
"started_at": "2026-07-20T10:00:00Z",
"completed_at": "2026-07-20T11:00:00Z",
"duration_seconds": 3600,
"memory_mb": 256,
"rate_applied": 0.018,
"cost": 0.018
}
],
"count": 1
}
App summary¶
Aggregates the app's records into totals per record type. since is optional
(422 if unparseable).
Response: 200 OK
{
"app_name": "gallery",
"environment": "dev",
"builds": {
"count": 5,
"total_seconds": 900,
"total_minutes": 15.0,
"cost": 0.05
},
"container_runtime": {
"web": {
"count": 24,
"total_hours": 24.0,
"memory_mb": 256,
"gb_hours": 6.0,
"cost": 0.432
}
},
"active_containers": [
{"service_name": "web", "instance_id": "…", "memory_mb": 256, "started_at": "2026-07-20T10:00:00Z"}
],
"backups": {
"count": 3,
"total_seconds": 42,
"total_size_mb": 128,
"cost": 0.0012
}
}
container_runtime is keyed by service name, so a multi-service app shows a
per-service breakdown. active_containers lists what is running right now,
regardless of since.
Report — all visible apps¶
Returns usage grouped by (app_name, record_type) across every app the caller
can see. Platform admins span all groups; tenants are filtered to their own
group's apps.
{
"environment": "dev",
"records": [
{
"app_name": "gallery",
"record_type": "container_runtime",
"count": 24,
"total_seconds": 86400,
"max_memory_mb": 256,
"total_cost": 0.4320
}
]
}
Group summary (invoice rollup)¶
The per-group billing unit. Aggregates every usage record owned by the
group — keyed on the stamped group_id, so the roll-up survives app deletion
or rehoming — into a per-app / per-record-type breakdown plus a grand total.
This is the invoice-shaped view the dashboard and mecca usage group consume.
A caller may only read their own group's bill: an unknown slug, or a slug
belonging to another group (for a non-platform_wide caller), returns 404.
| Query | Type | Notes |
|---|---|---|
since |
ISO-8601 | Optional lower bound on started_at (422 if unparseable) |
Response: 200 OK
{
"group_slug": "acme",
"records": [
{
"app_name": "gallery",
"record_type": "container_runtime",
"count": 24,
"total_seconds": 86400,
"total_cost": 0.4320
},
{
"app_name": "gallery",
"record_type": "build",
"count": 5,
"total_seconds": 900,
"total_cost": 0.0500
}
],
"total_cost": 0.4820
}
Repair open backup-storage records¶
A bundle's storage is metered as a backup_storage record that opens when the
backup completes. Deleting or pruning a bundle now closes its record. Bundles
deleted or pruned before that change left their records open, and an open
record is priced to a later purge. This route closes those whose bundle no longer
exists. The operator's door to it is appmecca admin usage repair-backup-storage
-e <env> --close-at retention-window|now|zero [--apply], which walks every page.
Scope: platform admin only (an account on the platform-admin list). It is not
group-scoped and takes no app or branch filter: it looks at every open
backup_storage record in the environment that no managed bundle owns.
Request body (JSON; any other field is refused):
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
close_at |
string | yes | — | What past usage the closed records bill: retention_window, now or zero. There is no default, because this is a billing decision |
apply |
boolean | no | false |
false is a dry run: nothing is written. true closes the records |
limit |
int | no | 200 |
Records examined per call, 1 to 500 |
after_id |
UUID | no | — | Paging cursor: examine records with an id after this one (pass the last response's next_after_id) |
close_at sets each record's completed_at:
close_at |
Closes at | Bills |
|---|---|---|
retention_window |
the record's started_at + 30 days (the prune's retention window), but never later than now |
the span held, at most 30 days |
now |
the moment of the call | everything until the repair |
zero |
the record's started_at |
nothing (duration_seconds 0) |
Each candidate is an active backup_storage record with a key and no
app_backups row for that key. It is put in one class:
| Class | Meaning | Closed by apply? |
|---|---|---|
to_close |
the key is a bundle prefix and nothing is stored under it in the system bucket | yes |
kept_bundle_present |
objects still exist under the prefix (for example a bundle written before the retention row existed); rightly open | no |
skipped_unrecognised |
the key is not a bundle prefix (a flat .sql.gz key, another environment's key, a malformed key); never touched |
no |
skipped_unreadable |
the bucket could not be listed; an unknown is never closed | no |
Response: 200 OK
{
"close_at": "retention_window",
"apply": false,
"examined": 2,
"to_close": 1,
"kept_bundle_present": 1,
"skipped_unrecognised": 0,
"skipped_unreadable": 0,
"closed": 0,
"s3_error": null,
"rows": [
{
"id": "550e8400-...",
"app_name": "gallery",
"backup_key": "backups/dev/gallery/main/manual/20260101-000000",
"started_at": "2026-01-01T00:00:00+00:00",
"verdict": "to_close"
},
{
"id": "6ba7b810-...",
"app_name": "gallery",
"backup_key": "backups/dev/gallery/main/manual/20260102-000000",
"started_at": "2026-01-02T00:00:00+00:00",
"verdict": "kept_bundle_present"
}
],
"next_after_id": null
}
examinedand the four class counts cover this page.closedis how many records this call closed (always0on a dry run).rowslists up to 100 of the page's records. On an applied call everyto_closerow is marked"closed": true, including one whose close matched nothing because a prune closed the record first.closedcounts exactly: only the records this call actually closed.s3_erroris the message when the S3 client could not be built; every candidate with a bundle-prefix key is thenskipped_unreadable(a key that is not a bundle prefix staysskipped_unrecognised). It isnullotherwise.next_after_idis set only on a full page (the page returnedlimitrecords). Pass it asafter_idfor the next call.nullmeans this was the last page.
Refusals:
| Status | When |
|---|---|
| 401 | no credentials |
| 403 | the caller is not a platform admin; the account is disabled; or the caller is a platform admin whose access is limited (read_only). A limited admin is refused even for a dry run: the route is a POST and is not marked as a limited read. An API key is always refused: a key carries no account |
| 422 | close_at missing or not one of the three values; limit outside 1 to 500; after_id not a UUID; an unknown field in the body |
Idempotent. A record is closed only while it is still active, so repeating an
applied call, or racing a prune, closes nothing twice. A dry run is repeatable and
returns the same classes until something changes.
Audit. Every call, dry run or applied, is recorded in the admin audit log as
backup_storage_repair, with close_at, apply, after_id, examined,
closed and the four class counts. Run it on production only on the product
owner's instruction.