gendesign/tradein-mvp/scripts/probe-db-region-leak.py
lekss361 d2a0257639
All checks were successful
Deploy Trade-In / changes (push) Successful in 6s
Deploy Trade-In / build-backend (push) Has been skipped
Deploy Trade-In / build-frontend (push) Has been skipped
Deploy Trade-In / deploy (push) Successful in 32s
feat(tradein-scripts): scrapers + probes + Phase C improvements (#551)
2026-05-24 19:58:38 +00:00

75 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Проверка: есть ли в DB Avito листинги из других городов (region leak)."""
from __future__ import annotations
import os
import psycopg
dsn = os.environ["DATABASE_URL"].replace("postgresql+psycopg://", "postgresql://")
conn = psycopg.connect(dsn)
cur = conn.cursor()
cur.execute("SELECT count(*) FROM listings WHERE source='avito'")
total = cur.fetchone()[0]
print(f"avito total: {total}")
# Все с lat/lon — проверим bounding box ЕКБ (56.7..57.0, 60.4..60.8)
cur.execute(
"""
SELECT
count(*) FILTER (WHERE lat BETWEEN 56.5 AND 57.0 AND lon BETWEEN 60.4 AND 61.0) as in_ekb,
count(*) FILTER (WHERE lat IS NOT NULL AND (lat < 56.5 OR lat > 57.0 OR lon < 60.4 OR lon > 61.0)) as outside_ekb,
count(*) FILTER (WHERE lat IS NULL) as no_coords
FROM listings WHERE source='avito'
"""
)
in_ekb, outside, no_coords = cur.fetchone()
print(f" in EKB bbox: {in_ekb}")
print(f" outside EKB bbox: {outside} ← out-of-region leak!")
print(f" no coords yet: {no_coords}")
# Sample outside
cur.execute(
"""
SELECT source_id, lat, lon, address, price_rub, rooms
FROM listings
WHERE source='avito' AND lat IS NOT NULL
AND (lat < 56.5 OR lat > 57.0 OR lon < 60.4 OR lon > 61.0)
ORDER BY scraped_at DESC NULLS LAST
LIMIT 10
"""
)
print("\nsample non-EKB listings:")
for r in cur.fetchall():
sid, lat, lon, addr, price, rooms = r
print(f" {sid} ({lat:.3f},{lon:.3f}) rooms={rooms} {price:,}₽ — {(addr or '')[:80]!r}")
# Address-based leak (без lat/lon) — поиск явных маркеров других городов
cur.execute(
"""
SELECT count(*) FROM listings
WHERE source='avito'
AND lat IS NULL
AND address IS NOT NULL
AND (address ILIKE '%москв%' OR address ILIKE '%омск%' OR address ILIKE '%чебок%'
OR address ILIKE '%петерб%' OR address ILIKE '%казан%' OR address ILIKE '%новосиб%')
"""
)
print(f"\naddress-based leak (no coords, other-city keyword): {cur.fetchone()[0]}")
# По комнатам — где leak вероятнее всего
cur.execute(
"""
SELECT rooms,
count(*) AS total,
count(*) FILTER (WHERE lat IS NOT NULL AND (lat < 56.5 OR lat > 57.0 OR lon < 60.4 OR lon > 61.0)) AS leak
FROM listings WHERE source='avito'
GROUP BY rooms
ORDER BY rooms NULLS LAST
"""
)
print("\nleak by rooms:")
print(f" {'rooms':<8} {'total':>8} {'leak':>8}")
for r in cur.fetchall():
rooms, total, leak = r
pct = (leak * 100.0 / total) if total else 0
print(f" {str(rooms):<8} {total:>8} {leak:>8} ({pct:.0f}%)")