diff --git a/README.md b/README.md index 5fe4e20..dd234ca 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ Field-ops **INC ticket** ingestion, geocoding, and read-schema that powers the | `migrations/02_import_meta.sql` | `tickets.import_meta` (per-dataset snapshot envelope metadata) + `fn_tickets_for_map` re-defined to expose it as `summary.freshness` (same signature — dashboard_api unchanged) | | `migrations/03_inc_columns.sql` | Unpacks `tickets.inc.raw` into **typed STORED generated columns** (status, cluster, region, team, owner, sla_status, mttr, lat/lng, is_* booleans, and EAT→`timestamptz` timestamps via `tickets.eat_ts()`). Computed for all rows + auto-populated on every ingest; `raw` stays the source of truth | | `migrations/04_inc_latlng.sql` | Redefines `latitude`/`longitude` to `COALESCE(feed, ST_Y/ST_X(geom))` so they're **populated from the geocoded position** (feed is always empty); precision per `geo_source` (`location` vs `cluster` centroid) | +| `migrations/05_inc_geography.sql` | Adds `geog geography(Point,4326)` (= `geom::geography`) + GiST index for **routing** — `ST_Distance`/`ST_DWithin`/KNN in real metres (nearest-vehicle, radius search) | | `import_tickets.py` | Ingests the **newest INC CSV** from the rustfs `tickets` bucket (`automations/inc/.csv`) and upserts on `ticket_id`; geocodes clusters + INC locations | | `run_migrations.py` | Applies `migrations/*.sql` in order (ledger: `tickets.schema_migrations`) | | `shared.py` | Minimal DB/logging helpers (self-contained — no tracksolid dependency) | diff --git a/migrations/05_inc_geography.sql b/migrations/05_inc_geography.sql new file mode 100644 index 0000000..f3427a7 --- /dev/null +++ b/migrations/05_inc_geography.sql @@ -0,0 +1,18 @@ +-- 05_inc_geography.sql — fleettickets · add a geography column for routing/distance +-- ───────────────────────────────────────────────────────────────────────────── +-- `geom` is geometry(Point,4326) — great for display, but distance on it is in +-- planar degrees. For real-world routing ("nearest vehicle", radius search) we want +-- geography, whose ST_Distance / ST_DWithin work in metres on the spheroid. +-- +-- `geog` is a STORED generated column = geom::geography (the cast is IMMUTABLE), so +-- it tracks geom automatically (incl. after geocode passes). GiST index supports +-- ST_DWithin(geog, vehicle::geography, metres) and KNN nearest-neighbour ordering. +-- Idempotent. +-- ───────────────────────────────────────────────────────────────────────────── + +SET search_path = tickets, public; + +ALTER TABLE tickets.inc + ADD COLUMN IF NOT EXISTS geog geography(Point, 4326) GENERATED ALWAYS AS (geom::geography) STORED; + +CREATE INDEX IF NOT EXISTS ix_inc_geog ON tickets.inc USING gist (geog);