All checks were successful
Deploy Trade-In / changes (push) Successful in 11s
Deploy Trade-In / build-browser (push) Has been skipped
Deploy Trade-In / test (push) Successful in 1m36s
Deploy Trade-In / build-frontend (push) Successful in 4m43s
Deploy Trade-In / build-backend (push) Successful in 4m44s
Deploy Trade-In / deploy (push) Successful in 2m15s
74 lines
2.8 KiB
Python
74 lines
2.8 KiB
Python
"""CLI: загрузка ГАР-знаменателя «квартир на дом» + матч к houses (мигр. 143).
|
||
|
||
Запуск (каталог — УЖЕ распакованный ГАР региона, напр. папка `66/` из gar_xml.zip):
|
||
|
||
python -m app.tasks.gar_flats_load --dir /data/gar/66 --region 66 --version 2026-06-01
|
||
|
||
Делает два шага в одной транзакционной сессии:
|
||
1. load_gar_region — стриминговый парс XML → UPSERT gar_house_flats (коммит).
|
||
2. match_houses_to_gar — UPDATE houses.gar_flat_count по нормализованному адресу (коммит).
|
||
|
||
Многогигабайтный ДАМП качается/распаковывается отдельно (ops-шаг, см.
|
||
docs/gar-flats-runbook.md) — этот лоадер потребляет уже распакованные XML локально.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import argparse
|
||
import logging
|
||
from datetime import date
|
||
|
||
from app.core.db import SessionLocal
|
||
from app.services.gar_flats_loader import load_gar_region, match_houses_to_gar
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
|
||
def run_gar_flats_load(dir_path: str, region_code: str, gar_version: str) -> dict[str, int]:
|
||
"""Парс+UPSERT (load_gar_region) затем матч (match_houses_to_gar). Возвращает счётчики."""
|
||
db = SessionLocal()
|
||
try:
|
||
load = load_gar_region(dir_path, region_code, gar_version, db=db)
|
||
matched = match_houses_to_gar(db, region_code=region_code)
|
||
db.commit()
|
||
finally:
|
||
db.close()
|
||
|
||
counts = load.to_counts()
|
||
counts["houses_matched"] = matched
|
||
logger.info(
|
||
"gar_flats_load DONE: region=%s version=%s houses=%d apartments=%d upserted=%d "
|
||
"houses_matched=%d duration=%ds",
|
||
region_code,
|
||
gar_version,
|
||
counts["houses"],
|
||
counts["apartments"],
|
||
counts["upserted"],
|
||
matched,
|
||
counts["duration_sec"],
|
||
)
|
||
return counts
|
||
|
||
|
||
def main() -> None:
|
||
logging.basicConfig(
|
||
level=logging.INFO,
|
||
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
|
||
)
|
||
parser = argparse.ArgumentParser(
|
||
description="ГАР loader: квартир на дом → gar_house_flats + match к houses"
|
||
)
|
||
parser.add_argument("--dir", required=True, help="каталог с распакованными ГАР XML региона")
|
||
parser.add_argument("--region", default="66", help="код региона (по умолчанию 66)")
|
||
parser.add_argument(
|
||
"--version",
|
||
default=date.today().isoformat(),
|
||
help="версия ГАР-дампа YYYY-MM-DD (по умолчанию сегодня)",
|
||
)
|
||
args = parser.parse_args()
|
||
|
||
run_gar_flats_load(args.dir, args.region, args.version)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|