Chapter 13 — Platform & Admin¶
13.1 — Chapter summary¶
⚙ Process: no pipeline step — the platform surfaces manage the environment: master data (the grids), the item lifecycle, users & authentication, tenant management, settings, the planning date, and the audit trail.
13.2 — Master data & the grids¶
The Master Data grids (Settings → Master Data) are the planner's direct CRUD surface for every entity — items, sites, routes, BOM, customers, suppliers, zones, capacity, calendars, on-hand, sales returns, firm orders, conditions, quota overrides. All write through the same auto-save + inline-edit pattern with FK dropdowns.
| Grid | Route / surface | Key columns |
|---|---|---|
| Items | Master Data | name, xuid, type, price, cost, unit_cost, expiry |
| Sites | Master Data | name, xuid, site_class (managed/install/zone), zone_id, max_capacity_qty |
| Routes | Master Data | planning_type, stock_type, lead_time, push_quota, coverage_pct, network_role, origin (erp/statistical) |
| Supply orders (firm) | Master Data | po_number, line_number, order_type, qty, release_dt, arrival_dt, erp_status |
13.3 — Users, authentication & superadmin preemption¶
| Topic | How it works |
|---|---|
| JWT cookie auth | HttpOnly mirabelle_token cookie (the Oct 2026 pen-test hardening — no localStorage JWT); the middleware accepts header OR cookie; the frontend uses withCredentials |
| Superadmin preemption | If an email exists in master.superadmins, ONLY the superadmin password is accepted — the tenant password is never checked (no fallthrough) |
| Password sync | Changing your password propagates the hash to all stores (master + every tenant DB where the email exists) |
| Tenant isolation | The JWT's account_id determines which tenant DB every request hits; wrong account = confidently wrong results (the tenant-debug rule) |
13.4 — Tenant management¶
⚙ Process: the Tenant admin panel (route
/admin, superAdmin only). Run: the New Account button — fresh (empty) or copied from an existing tenant (the API equivalents are in 13.D). Provisioning is a background job — the panel shows its live phase in the status banner.
| Topic | Detail |
|---|---|
| DB-per-tenant | Each tenant has its own PostgreSQL database (the master.accounts registry holds db_name, schema_name, connection_params) and its own ClickHouse database (same name) |
| Provisioning | Account creation seeds the default structure (the base scenarios, the Default pipeline, the default workflow); new tenants can be created via the admin panel — fresh (empty) or copied (cloned) from an existing tenant |
| Isolation | Every connection resolves through the account context — a process can only write its own tenant's data (structural; there is no code path to another tenant) |
| Master DB | forecastai_master holds the account registry, the superadmin credentials, and the JWT secret — shared metadata, never business data |
A tenant database is created in one of two ways — the modal's two tabs, or the API flag:
Provision job empty → fresh tenant clone → copy of source → creating_database CREATE DATABASE (name sanitised: a–z 0–9 _ only, existing name refused) → cloning_data clone only pg_dump → pg_restore (full source copy) → initialising_schema init_schema() all PG tables + the CH database → seeding → registering + refreshing_cache → complete — tenant live (no restart needed; empty mode returns the generated admin login)
| Empty Account (created fresh) | Clone Existing (copied from another tenant) | |
|---|---|---|
| What lands in the DB | An empty schema, then the complete factory default graph | A full physical copy of the source tenant:
pg_dump --format=custom --no-owner --no-privileges | pg_restore --no-owner --no-acl
— master data, scenarios, pipelines, workflows, parameters, users, everything |
| Seeding | Factory parameter sets + the default structure (Default segment, base scenarios, the Default pipeline, the default workflow) + a generated tenant-admin login (returned by the job and stored in Settings → Auth) | Additive only: the copied scenario graph is preserved, never repointed to fresh
defaults; factory parameter keys added since the source was last updated are merged in
(ON CONFLICT DO NOTHING + deep-merge); canonical module DDL is applied on top |
| ClickHouse | Created by init_schema(), empty — the pipeline populates it |
Created fresh and empty — the clone copies PostgreSQL only; run the pipeline to rebuild the PIPE_ outputs |
| Constraints | db_name must be new (an existing name is refused); optional the customer add-on fills the tenant from a the customer source after seeding | Source must be active; the target schema name must equal the source's; the customer load cannot be combined with a clone |
| Failure hygiene | Any failed phase rolls the whole thing back: the created PG and ClickHouse databases are dropped and the account row is removed — no half-provisioned tenant survives. The rare survivor of a failed rollback surfaces in the panel's Orphaned databases block, droppable from there (it blocks re-using the same db_name). | |
The lifecycle after creation: Deactivate (soft — is_active=FALSE, the tenant
disappears from logins) or Drop DB (deactivate with the database permanently dropped in
PostgreSQL and ClickHouse plus the account row removed — type-the-name confirmation). Every account
row also has a Clone button that opens the same modal with the source pre-selected.

Fig. 30 — The Tenant admin panel (/admin): the Tenant Accounts
table (display name, database, schema, active status, created date, the per-row Clone /
Deactivate / Drop DB actions), the New Account button,
and the SuperAdmin Users panel. SuperAdmin-only surface. Captured 2026-11-11.

Fig. 31 — Creating a fresh tenant: the New Account modal in Empty Account mode — the two creation modes side by side, the display name auto-filling the db name, the schema name, and the the customer add-on box (optional source fill, fresh tenants only). Captured 2026-11-11.

Fig. 32 — Copying an existing tenant: the same modal in Clone Existing mode — the clone-source dropdown lists every active account; the target gets a new display name + db name and becomes a full copy of the source. Captured 2026-11-11.
The level of security — what the isolation gives, layer by layer (all verified in code):
| Layer | What it enforces |
|---|---|
| Structural DB isolation (the strongest level) | One PostgreSQL database + one ClickHouse database per tenant. Every connection resolves through the per-request account context (JWT account_id → account cache → ContextVar → get_conn()) — a request is physically connected to one tenant database, so a query cannot leak into another tenant: there is no code path to another tenant's data, only to another tenant's login |
| Authentication | JWT cookie auth (13.3) — the token carries the account_id; logout revokes the token in the tenant's own revoked_tokens |
| Authorization | Every admin operation (provision, clone, deactivate, drop, superadmins, user assignments, migrations) requires the superadmin role — anything else gets 403. Within a tenant, the role hierarchy + segment scoping applies (13.3) |
| Superadmin preemption | An email registered in master.superadmins accepts ONLY the superadmin password (13.3) — a tenant password can never escalate to the admin panel |
| Per-tenant users | Each tenant's users live in its own DB (sys_users); a user sees only the accounts assigned via master.user_accounts (the account switcher); a tenant admin has no admin-panel route |
| Master DB | Shared metadata only — account registry, superadmin credentials, the JWT secret, user→account assignments; never business data |
| Audit | Every provisioning / clone / deactivate / drop / orphan-drop / superadmin / user-assignment action writes a row to the master DB's sys_audit_log (who, what, before/after, outcome) |
| Configurable hardening | Per-account connection_params can point a tenant at its own PostgreSQL host/port/user/password/sslmode — a tenant can live on a physically separate server; db names are sanitised (letters/digits/underscore only) and existing names are refused |
| Honest limits | Isolation is at the DATABASE level, not per-tenant PostgreSQL roles — the app connects with the configured server credentials (override per account to harden); no row-level security (one schema per tenant DB); the admin panel is a trusted superAdmin surface by design |
13.5 — Settings, planning date & audit¶
| Surface | What it manages |
|---|---|
| Settings page | The parameter system (config_parameters — the parameter SETS by type: forecast, outlier, characterization, backtesting, supply, MEIO, alerts…), the YAML config viewer, the UI settings (currency, precision, theme) |
| Planning date | The pinned virtual "today" per pipeline (scenario.planning_date, pipeline_id = −1 for global) — the backbone of the no-wall-clock discipline (14.2) |
| Audit | The Audit Log (route /audit) + the Process Log (route /logs) — every config change and every pipeline run is recorded; the OpenTelemetry traces for the backend |
13.D — For developers — the admin APIs¶
The UI paths above cover the administrator's loop; the equivalents for scripts and add-ons (every one requires the superadmin role):
| Call | Purpose |
|---|---|
POST /api/admin/accounts · GET /api/admin/accounts/{job_id}/provision-status |
Create a tenant (fresh; add clone_from_account_id to copy an existing one) and poll the background job to complete. |
POST /api/admin/accounts/{account_id}/clone · DELETE /api/admin/accounts/{account_id} |
The per-row Clone / Drop DB actions. |
GET/DELETE /api/admin/orphan-databases |
The Orphaned databases block (survivors of a failed rollback). |
GET/POST /api/admin/superadmins · GET /api/admin/users (+ /users/assignments, PUT /users/{email}/accounts) |
The SuperAdmin users panel + user→account assignments. |
POST /api/admin/cache/refresh · POST /api/admin/run-migrations |
Cache refresh after tenant changes; manual migration runner. |
POST /api/auth/change-password |
The password change that syncs the hash to every store (13.3). |
13.T — Step-by-step test walkthrough¶
Platform administration (superadmin surfaces).
| # | Where to look / what to click | You should see |
|---|---|---|
| 1 | /admin → New Account (Empty Account) |
fill display/db/schema names → Create → the provision job streams its phases live |
| 2 | Poll the job to complete | the tenant is live with seeded defaults + a generated admin login (no restart) |
| 3 | Clone Existing: pick a source account → Create | a full PG copy; its ClickHouse starts EMPTY → run the pipeline to rebuild |
| 4 | Users (/users or admin panel): create a user, assign roles + segments |
login as them → scoping applies (segments, permissions) |
| 5 | Settings: pick the parameter set type → edit | auto-save; changes picked up by the next run |
| 6 | Settings → Planning Date: pin a date per pipeline | runs and archives anchor on the pinned Monday |

The accounts table + per-row actions.

The provisioning modal in empty mode. nts table + per-row actions.*

The provisioning modal in empty mode.