Cars: log what charging costs, and drag the provider readings

Three things, all on a car's page.

A Charging cost tab, which is the Fuel cost tab written for an electric
car: charges in kWh, consumption in kWh/100km beside km per kWh, cost per
km and price per kWh, and the same summary panel over the whole history.
It keeps the reference-point method too, and has to — a session records
the energy that went in, not what was left in the battery, so a given
number of kWh only maps to a distance between two charges that ended at
the same state. A charge to the car's usual full point plays the part of
the full tank; partial charges still count towards the cost and roll into
the next full one; and a charge taken without logging it leaves its
window uncomputed rather than reporting an implausibly good figure.
Sessions live in their own collection, the figures are derived on read
like the fuel ones, and logging a charge advances the odometer exactly as
a refill does. The tab switches off from the gear like every other, so a
petrol car need never see it.

The headline readings on the connected-service tab now drag into any
order, saved on drop. Stored on the car as metricOrder, like the
Information rows, rather than per device the way the collapsed cards are:
an arrangement is something everyone the car is shared with should see,
where a folded card is one browser's reading habit. Only what the
provider reported can be arranged, so a reading that turns up later joins
the end rather than displacing the arrangement.

Fuel is now Fuel cost, tab and heading, which is what the tab has always
been about and what pairs it with Charging cost.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
tajniak81
2026-08-17 22:17:52 +02:00
co-authored by Claude Opus 5
parent 2b6da642ad
commit 6191160f14
20 changed files with 1388 additions and 53 deletions
+27 -3
View File
@@ -281,8 +281,10 @@ const DESIRED = {
F.json("hidden_fields", 2000),
// The order the Information rows are laid out in, as field keys — the
// hidden ones included, so a row switched back on returns to where it was.
// Empty means the page's own default order.
// Empty means the page's own default order. metric_order is the same for
// the headline readings on the connected service's tab.
F.json("field_order", 2000),
F.json("metric_order", 2000),
// Owner of this car. Non-cascading on purpose: deleting a user must not
// wipe their cars (account deletion in me.go intentionally leaves cars).
// required:false at the DB level — the API always sets owner on create and
@@ -339,6 +341,25 @@ const DESIRED = {
F.text("notes"),
attachment(), // the pump receipt
],
// Charging sessions for an electric car. The EV counterpart of fuel_entries
// and deliberately the same shape: kWh where litres would be, a charge to the
// usual full point as the reference the windows are measured between, and
// consumption derived on read (models.ComputeChargingDerived).
charging_sessions: [
F.relation("car", "cars", true),
F.date("date", true),
F.number("km"), // odometer when plugging in
F.number("kwh"),
F.number("cost"),
// Charged to the car's usual full point — the reference point.
F.bool("full_charge"),
// The car was charged before this without being logged, so any window
// containing it is left uncomputed rather than reported as implausible.
F.bool("missed_session"),
F.text("location"), // "Home", "Ionity Koge"
F.text("notes"),
attachment(), // the charge point's receipt
],
// Workshop visits and repairs. Deliberately separate from service_records:
// that collection is the routine interval schedule (and drives next-service
// due), this one is unplanned/one-off garage work with a labour bill.
@@ -469,6 +490,7 @@ const INDEXES = {
// Every read of these is "…for this car", and the fuel history is walked in
// odometer order to build its efficiency windows.
fuel_entries: ["CREATE INDEX `idx_fuel_entries_car_km` ON `fuel_entries` (`car`, `km`)"],
charging_sessions: ["CREATE INDEX `idx_charging_sessions_car_km` ON `charging_sessions` (`car`, `km`)"],
maintenance_entries: ["CREATE INDEX `idx_maintenance_entries_car_date` ON `maintenance_entries` (`car`, `date`)"],
car_documents: ["CREATE INDEX `idx_car_documents_car_expiry` ON `car_documents` (`car`, `expiry_date`)"],
reminders: ["CREATE INDEX `idx_reminders_car_due` ON `reminders` (`car`, `due_date`)"],
@@ -504,6 +526,7 @@ async function main() {
"parts",
"car_shares",
"fuel_entries",
"charging_sessions",
"maintenance_entries",
"car_documents",
"reminders",
@@ -529,6 +552,7 @@ async function main() {
"parts",
"car_shares",
"fuel_entries",
"charging_sessions",
"maintenance_entries",
"car_documents",
"reminders",
@@ -539,8 +563,8 @@ async function main() {
console.log(
"\nDone. Collections ready: organizations, users, cars, service_records,\n" +
"technical_checks, parts, car_shares, fuel_entries, maintenance_entries,\n" +
"car_documents, reminders, control_audit.",
"technical_checks, parts, car_shares, fuel_entries, charging_sessions,\n" +
"maintenance_entries, car_documents, reminders, control_audit.",
);
console.log(
"Note: the legacy `sessions` collection is no longer used (auth moved to PocketBase\n" +