Add cost history to the Costs page: what it cost each month, and what changed

Each cost item's name, amount, currency, period and kind are kept as versions (cost_item_versions), and Hetzner's estimate is written down once a day (cost_snapshots), so a past month's figure and the changes between can be worked out. History card with monthly bars per currency, month-by-month figures and a list of added, changed and removed items.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-11-04 23:57:59 +01:00
1 parent b29c0b7d04
commit 9f366c37de
15 files changed
+9716 -3

No files matched your search

+26 -2
View File
@@ -24,8 +24,8 @@ what the app actually creates.
| **File** | `DATABASE_PATH`, default `../data/homelab-manager.sqlite` (relative to `server/`; `/app/data/...` in Docker). The folder is created if missing. |
| **Foreign keys** | Switched on for every connection (`PRAGMA foreign_keys = ON`), so the `ON DELETE` rules below are enforced. |
| **Writes** | SQLite lets one writer in at a time and refuses a second at once ("database is locked"). While a transaction of the app is open, its other writes wait for it ([`server/src/db/writeGate.ts`](server/src/db/writeGate.ts)) rather than being refused — a refused write also leaves its connection unable to commit the next transaction. Reads are never held; a wait over 10 seconds is given up. A lock held by *another* process (a backup tool reading the file, a second copy of the app) is still refused as before. |
| **Migrations** | SQL files in [`server/drizzle/`](server/drizzle) (`0000` … `0084`), applied automatically when the server starts. Which ones have run is recorded in the table `__drizzle_migrations`. |
| **Tables** | 93 application tables, plus drizzle's own `__drizzle_migrations`. |
| **Migrations** | SQL files in [`server/drizzle/`](server/drizzle) (`0000` … `0085`), applied automatically when the server starts. Which ones have run is recorded in the table `__drizzle_migrations`. |
| **Tables** | 95 application tables, plus drizzle's own `__drizzle_migrations`. |
**Not in the database, but beside it in the data folder:** the backups of the database itself (`backups/homelab-manager-….sqlite`, taken daily and on request; see [BACKUP.md](BACKUP.md)), and, only while a restore is waiting or has just happened, `restore-pending.sqlite` / `restore-pending.json` and `restore-result.json`.
@@ -1003,6 +1003,30 @@ Things paid for by hand (a domain, a VPN, a licence, a subscription), one row ea
| `note` | text | |
| `created_at`, `updated_at` | text NOT NULL | ISO. |
#### `cost_item_versions`
What an item of `cost_items` was at each time — the Costs page's history. One row for each stretch in which its name, amount, currency, period and kind were what they are: opened when the item is added or any of those change, closed (`valid_to`) when they change again or the item is removed. A change of only the note or the billing day makes no row. `item_id` is a **soft reference** (no foreign key): the rows stay when the item is removed, which is the point. Kept in step with `cost_items` by `services/costHistory.ts` whenever an item is saved and every six hours; an item that was there before the table existed is given a row from its `created_at`, at the amount it has now.
| Column | Type | Notes |
|---|---|---|
| `id` | integer PK | |
| `item_id` | integer NOT NULL | The item. Index `cost_item_versions_item_idx`. |
| `name`, `currency`, `period`, `category` | text NOT NULL | As they were. |
| `amount` | real NOT NULL | As it was, per period, with VAT. |
| `valid_from` | text NOT NULL | ISO. For an item's first row, when it was added. |
| `valid_to` | text | ISO; null while it is in force. |
#### `cost_snapshots`
What can be read rather than typed — a Hetzner project's monthly estimate — as it was on each day, written once a day (a second time the same day replaces the first). The estimate moves with the servers and cannot be worked out afterwards. Primary key (`day`, `key`).
| Column | Type | Notes |
|---|---|---|
| `day` | text NOT NULL | YYYY-MM-DD (UTC). |
| `key` | text NOT NULL | The source: `hetzner:<integration id>`. Kept after the integration is removed. |
| `name`, `currency` | text NOT NULL | The project's name and the currency as they were. |
| `monthly` | real NOT NULL | Per month, with VAT, at list prices. |
#### `hetzner_state`
How a Hetzner Cloud project (an integration of type `hetzner`; a token belongs to one project) is right now, one row each, replaced at every read (every five minutes). `integration_id` is the primary key and a foreign key with **cascade**. There is no history table.