askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
usage_events.json · 3.9 KB · 39 lines
| 1 | { |
| 2 | "name": "usage_events", |
| 3 | "title": "Usage Events", |
| 4 | "version": 4, |
| 5 | "storage": "append", |
| 6 | "description": "One measured thing happened: tokens consumed, a task run, a gigabyte-hour held. Append-only, because a bill must be reconstructable from the facts that produced it -- a customer disputing an invoice is entitled to the line items behind it, and a mutable usage table cannot answer that. event_id is a CALLER-supplied idempotency key and the most important field here: retries are the number one corruption source in metering, and a client that resends after a timeout must not be charged twice for one call. cost_minor stamps what the usage cost US at the moment it happened, which is what makes margin a fold over stored facts instead of a spreadsheet somebody maintains separately -- a billing system that knows revenue but not cost cannot answer which customer loses money. Rating never reads this collection: it reads the period summaries folded from it (object_billing), which is the boundary that lets the write side change later without touching pricing. v2 declares event_id unique per owner, enforced inside the write lock (docs/validation-and-logic.md): the hook's own look-before-write check runs before the lock, and two overlapping retries of one event both passed it and both landed. v4: subscription_id declares relation.requires update: usage is billed to the subscription it names.", |
| 7 | "unique": [ |
| 8 | {"fields": ["event_id"], "scope": ["owner_id"], |
| 9 | "message": "This usage event was already recorded. A retried request must not be billed twice -- that is what the event key is for."} |
| 10 | ], |
| 11 | "fields": [ |
| 12 | {"name": "id"}, |
| 13 | {"name": "event_id", "type": "text", "required": true, "label": "Event Key", |
| 14 | "help": "Caller-supplied idempotency key, unique per owner. Resending the same key records nothing -- a timeout retry must never bill twice."}, |
| 15 | {"name": "subscription_id", "label": "Subscription", |
| 16 | "relation": {"collection": "subscriptions", "display_field": "customer_name", "requires": "update"}}, |
| 17 | {"name": "metric", "type": "text", "required": true, "label": "Metric", |
| 18 | "help": "What was measured, matching a key in the plan's prices (ai.tokens, storage.gb_hours, tasks.run)."}, |
| 19 | {"name": "quantity", "type": "number", "required": true, "label": "Quantity", |
| 20 | "validation": {"exclusive_min": 0}, |
| 21 | "help": "How much, and more than zero: a correction to a bill is a credit, never a negative measurement that unmeasures something that already happened. A count or measure, not money -- may be fractional (gigabyte-hours), and read with Decimal wherever it is summed."}, |
| 22 | {"name": "unit", "type": "text", "label": "Unit", |
| 23 | "help": "What the quantity counts (tokens, gb_hours, requests). Documentation for humans; the metric name is what pricing matches on."}, |
| 24 | {"name": "cost_minor", "type": "integer", "default": "0", "label": "Our Cost", |
| 25 | "help": "What this usage cost the business, stamped at the moment it happened under the vendor price THEN -- later price changes must not restate history."}, |
| 26 | {"name": "occurred_at", "type": "datetime", "label": "Occurred"}, |
| 27 | {"name": "reference", "type": "text", "label": "Reference", |
| 28 | "help": "The task, request or object that caused it, so a line on a bill can be traced to the work."}, |
| 29 | {"name": "rated", "type": "boolean", "default": "false", "label": "Rated", |
| 30 | "help": "Set once folded into a period summary."}, |
| 31 | {"name": "owner_id", "type": "text", "label": "Owner"}, |
| 32 | {"name": "created_at", "type": "datetime", "read_only": true, "label": "Recorded"} |
| 33 | ], |
| 34 | "search": {"fields": ["metric", "reference"]}, |
| 35 | "forms": {"default": {"fields": ["subscription_id", "metric", "quantity", "unit", "reference"]}}, |
| 36 | "views": {"list_mode": "table", |
| 37 | "list_fields": ["occurred_at", "metric", "quantity", "cost_minor", "rated"], |
| 38 | "filter_fields": ["metric", "rated", "subscription_id"]} |
| 39 | } |