All checks were successful
CI Trade-In / changes (pull_request) Successful in 7s
CI / changes (pull_request) Successful in 8s
CI Trade-In / frontend-checks (pull_request) Has been skipped
CI / backend-tests (pull_request) Has been skipped
CI / frontend-tests (pull_request) Has been skipped
CI / openapi-codegen-check (pull_request) Has been skipped
CI Trade-In / backend-tests (pull_request) Successful in 1m36s
Финальный PR issue #2045 (BE-3): GET /api/v1/trade-in/location-coef для LocationDrawer. FDW foreign table -> локальное зеркало osm_poi_ekb_local (TRUNCATE+INSERT, тот же паттерн что cad_buildings_local/cadastral_geo_match, избегает ~1.16s/row FDW round-trip) -> straight-line POI-скоринг, портированный из Site Finder poi_score.py::compute_poi_weighted_top7 (CATEGORY_WEIGHTS as-is, радиус 1200м для квартир вместо Ptica 2000м для участков). score->coef - новая MVP-эвристика (0.95..1.05, не откалибрована на реальных дельтах). Graceful fallback (не 500, не фабрикуем факторы): пустая/не отрефрешенная osm_poi_ekb_local или отсутствие lat/lon у оценки -> coef=1.0, factors=[], geo_source="unavailable". Scheduler: source=osm_poi_ekb_refresh, daily, зарегистрирован и в боевом dispatch (scheduler.py), и в kit-registry (product_handlers.py) - иначе test_kit_registry_completeness падает на ship-dark инварианте (#2192). Frontend wiring (mappers.ts/LocationDrawer.tsx) - вне scope, отдельная задача после проверки endpoint'а curl'ом на деплое.
109 lines
4 KiB
Python
109 lines
4 KiB
Python
"""OSM POI local-mirror refresh (#2045 BE-3, LocationDrawer location-coef).
|
|
|
|
Populates `osm_poi_ekb_local` (empty at deploy, migration 169) via a single bulk scan of the
|
|
`gendesign_osm_poi_ekb` FDW foreign table (migration 168 — live view of gendesign's
|
|
`osm_poi_ekb` table, Site Finder's OSM POI registry, GRANTed to `tradein_fdw_reader` in PR-A
|
|
of #2045, already merged + deployed on gendesign).
|
|
|
|
WHY a local mirror (perf fact measured for the analogous gendesign_cad_buildings FDW — see
|
|
`app/tasks/cadastral_geo_match.py`): a per-request FDW nearest-POI query pays a per-row FDW
|
|
round-trip (~1.16s/row without a geom index on the remote table) — UNUSABLE for a synchronous
|
|
endpoint (`GET /api/v1/trade-in/location-coef`). We materialize the FDW once (single bulk
|
|
scan) into `osm_poi_ekb_local` with a real Point geom + GIST index, then
|
|
`app/services/location_coef.py` runs fast LOCAL ST_DWithin/ST_Distance queries per estimate.
|
|
|
|
Scheduler source='osm_poi_ekb_refresh' (daily — OSM POI data changes rarely). Pure internal
|
|
DB op — one FDW read + local TRUNCATE+INSERT, no HTTP/anti-bot.
|
|
|
|
psycopg v3: app SQL uses CAST(:x AS type), never :x::type.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import time
|
|
from dataclasses import dataclass, field
|
|
|
|
from sqlalchemy import text
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.services import scrape_runs as runs_mod
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def refresh_osm_poi_ekb_local(db: Session) -> int:
|
|
"""TRUNCATE osm_poi_ekb_local; bulk INSERT from gendesign_osm_poi_ekb FDW.
|
|
|
|
Single FDW scan (no per-row round-trips). Builds a Point(4326) geom from lon/lat.
|
|
Idempotent: TRUNCATE+INSERT in one transaction — the table is fully replaced atomically.
|
|
|
|
Returns the number of rows loaded.
|
|
"""
|
|
db.execute(text("TRUNCATE osm_poi_ekb_local"))
|
|
db.execute(
|
|
text(
|
|
"""
|
|
INSERT INTO osm_poi_ekb_local (category, name, lat, lon, geom)
|
|
SELECT
|
|
category, name, lat, lon,
|
|
ST_SetSRID(ST_MakePoint(lon, lat), 4326)
|
|
FROM gendesign_osm_poi_ekb
|
|
WHERE lat IS NOT NULL
|
|
AND lon IS NOT NULL
|
|
"""
|
|
)
|
|
)
|
|
count = db.execute(text("SELECT count(*) FROM osm_poi_ekb_local")).scalar() or 0
|
|
db.commit()
|
|
logger.info("refresh_osm_poi_ekb_local: loaded %d rows from FDW", count)
|
|
return int(count)
|
|
|
|
|
|
@dataclass
|
|
class OsmPoiRefreshResult:
|
|
poi_loaded: int = 0 # rows in osm_poi_ekb_local after refresh
|
|
duration_sec: float = field(default=0.0)
|
|
|
|
def to_counters(self) -> dict[str, int]:
|
|
return {
|
|
"poi_loaded": self.poi_loaded,
|
|
"duration_sec": int(self.duration_sec),
|
|
}
|
|
|
|
|
|
def run_osm_poi_ekb_refresh(db: Session, *, run_id: int, params: dict) -> OsmPoiRefreshResult:
|
|
"""Run-lifecycle wrapper for the OSM POI local-mirror refresh (scheduler entrypoint).
|
|
|
|
Launched by the in-app scheduler (source='osm_poi_ekb_refresh') via
|
|
trigger_osm_poi_ekb_refresh_run, or manually. No params consumed — single unconditional
|
|
bulk refresh (`params` accepted for scheduler-signature compatibility / future use).
|
|
|
|
Finalises scrape_runs (mark_done / mark_failed) with counters.
|
|
"""
|
|
counters: dict[str, int] = {"poi_loaded": 0}
|
|
start = time.monotonic()
|
|
|
|
try:
|
|
runs_mod.update_heartbeat(db, run_id, counters)
|
|
|
|
loaded = refresh_osm_poi_ekb_local(db)
|
|
result = OsmPoiRefreshResult(poi_loaded=loaded, duration_sec=time.monotonic() - start)
|
|
|
|
counters = result.to_counters()
|
|
runs_mod.mark_done(db, run_id, counters)
|
|
logger.info(
|
|
"run_osm_poi_ekb_refresh: run_id=%d DONE poi_loaded=%d duration=%.1fs",
|
|
run_id,
|
|
result.poi_loaded,
|
|
result.duration_sec,
|
|
)
|
|
return result
|
|
except Exception as exc:
|
|
logger.exception("run_osm_poi_ekb_refresh: run_id=%d FAILED", run_id)
|
|
try:
|
|
db.rollback()
|
|
except Exception:
|
|
pass
|
|
runs_mod.mark_failed(db, run_id, str(exc)[:1000], counters)
|
|
raise
|