gendesign/tradein-mvp/backend/app/services
Light1YT 613f576858 feat(tradein): hook matching service в save_listings — fill listing_sources
Wire app.services.matching в base.save_listings: после каждого INSERT,
resolve canonical house через match_or_create_house и register
(ext_source, ext_id) в listing_sources через upsert_listing_source.

Закрывает gap который оставлял listing_sources пустым (0 rows в проде)
при том что listings имеет 18k+ rows. Каждый scraped lot теперь создаёт/
обновляет listing_sources row и линкует к canonical houses — enabling
per-house price history и de-duplication через avito/cian/yandex/n1.

Implementation:
- save_listings RETURNING id добавлен (был только `(xmax = 0) AS inserted`)
- Новый helper _link_listing_to_house():
  * match_or_create_house() для resolve canonical houses.id из address/lat/lon
    (Tier 0–3 как у estimator.py)
  * Использует lot.house_source/house_ext_id если scraper extract'нул
    (Avito Houses Catalog, Cian newbuilding); fallback на (lot.source, ext_id)
  * upsert_listing_source с method='source_link', confidence=1.0
- Fault-tolerant: try/except + db.begin_nested() SAVEPOINT
  — match failure не abortит INSERT batch
- Idempotent: ON CONFLICT (ext_source, ext_id) UPDATE
- House-match failure отдельно handled — listing_sources всё равно upsert'ит
  (без house_id) чтобы row для later backfill existed
- 4 UA strings помечены noqa: E501 (pre-existing, нельзя split)

Tests: 14 pass в test_base_save_listings.py:
- 6 existing updated (_get_execute_params reads call_args_list[0], _mock_db
  returns id from RETURNING, autouse fixture патчит matching service)
- 8 new: RETURNING id, hook firing once, listing context passed, ON CONFLICT
  path, failure non-abort, dedup_hash fallback, house-match failure path,
  empty lots skip
- Wider 89 tests passing

Out of scope: backfill existing 18k listings (PR J) — для new scrapes
этот hook предотвратит regression.

Open Q для PR J: matching/listings.py Tier 2-3 references listings.house_id
но schema только house_id_fk. Backfill PR должен resolve canonical column.
2026-05-27 15:02:10 +05:00
..
exporters feat(tradein): этаж/этажность optional + best test presets по deal count (#558) 2026-05-24 21:44:44 +00:00
matching fix(tradein-matching): pg_advisory_xact_lock to prevent duplicate house INSERTs (#501) 2026-05-24 10:52:10 +00:00
scrapers feat(tradein): hook matching service в save_listings — fill listing_sources 2026-05-27 15:02:10 +05:00
__init__.py feat: add tradein-mvp subproject (Trade-In Estimator под /trade-in) 2026-05-21 00:25:39 +03:00
brand.py feat: add tradein-mvp subproject (Trade-In Estimator под /trade-in) 2026-05-21 00:25:39 +03:00
cache.py feat(tradein): Phase 3.2 — /api/v1/search endpoint + Redis cache (#479) 2026-05-23 14:44:22 +00:00
cian_session.py fix(tradein): CIAN_REQUIRED_COOKIES — add real auth cookies (DMIR_AUTH + Cian session) (#480) 2026-05-23 14:52:24 +00:00
estimator.py feat(tradein): этаж/этажность optional + best test presets по deal count (#558) 2026-05-24 21:44:44 +00:00
geocoder.py feat(tradein): MapPicker snap marker — Phase 5 of #582 (#594) 2026-05-27 09:30:17 +00:00
house_metadata.py feat(tradein): postgres_fdw live read of gendesign.cad_buildings (replaces snapshot) (#493) 2026-05-24 08:57:30 +00:00
image_sanitizer.py fix(tradein-photos): sanitize uploaded images via Pillow re-encode (#511) 2026-05-24 11:32:04 +00:00
scheduler.py feat(tradein): in-app scheduler — UI-managed schedule (replaces SSH crontab) (#482) 2026-05-23 14:54:25 +00:00
scrape_pipeline.py fix(tradein/avito): anti-bot hardening — sleep + abort + shared session (#487) 2026-05-23 20:08:40 +00:00
scrape_runs.py fix(tradein/avito): anti-bot hardening — sleep + abort + shared session (#487) 2026-05-23 20:08:40 +00:00
scraper_settings.py feat(tradein): global scraper delay setting (applies across all scrapers) (#485) 2026-05-23 15:43:28 +00:00
search_query.py feat(tradein): Phase 3.2 — /api/v1/search endpoint + Redis cache (#479) 2026-05-23 14:44:22 +00:00