MEGAINFRA-WEBAPP/scripts/pb-add-fuel-prices.py

67 lines
2.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""Create the FUEL_PRICES collection on PocketBase.
One row per diesel price change. `effective_from` is the first day the
price applied (inclusive); the app resolves a fuel log's rate by picking
the latest row on or before the log's own date.
Until this collection has rows, every fuel figure falls back to
RATE_PER_LITRE in src/lib/metrics/constants.ts — i.e. exactly what the
app showed before dated pricing. Adding rows is what actually corrects
the historical numbers.
Idempotent: re-running when the collection exists is a no-op.
Credentials come from .env.local (or the environment):
python3 scripts/pb-add-fuel-prices.py
Rules match the other collections (scripts/pb-relax-rules.py):
list/view/create for any authenticated user, update/delete superuser only.
"""
import sys
from pb_env import http, superuser_token
TOKEN = superuser_token()
AUTH_RULE = "@request.auth.id != ''"
DEF = {
"name": "FUEL_PRICES",
"type": "base",
"fields": [
{"name": "effective_from", "type": "date", "required": True},
{"name": "price_per_litre", "type": "number", "required": True, "min": 0},
{"name": "notes", "type": "text", "required": False},
],
"indexes": [
"CREATE UNIQUE INDEX `idx_fuel_prices_effective_from` "
"ON `fuel_prices` (`effective_from`)",
],
"listRule": AUTH_RULE,
"viewRule": AUTH_RULE,
"createRule": AUTH_RULE,
"updateRule": None,
"deleteRule": None,
}
code, resp = http("POST", "/api/collections", DEF, TOKEN)
if code == 200:
print(f" ✓ FUEL_PRICES created → {resp['id']}")
elif code == 400:
code2, existing = http("GET", "/api/collections/FUEL_PRICES", token=TOKEN)
if code2 == 200:
print(f" • FUEL_PRICES already exists → {existing['id']}")
else:
sys.exit(f" ✗ FAILED: {resp}")
else:
sys.exit(f" ✗ FAILED: {code} {resp}")
print(
"\nNext: add one row per diesel price change in the PocketBase admin UI.\n"
" effective_from price_per_litre\n"
" 2026-08-05 25.65 <- whatever was true that month\n"
"Until rows exist, every fuel figure stays on the RATE_PER_LITRE fallback."
)