fix(sf-07): use median_12m from mv_quarter_price_per_m2 for district price

ekb_districts.median_price_per_m2 was populated from a 24-month window.
LEFT JOIN mv_quarter_price_per_m2 on the cad quarter and COALESCE(median_12m,
fallback) so the API returns the 12-month median where available.
Frontend OverviewTab adds a "(12 мес)" hint next to the value.

Closes #271 item 7
This commit is contained in:
lekss361 2026-05-17 14:28:32 +03:00
parent 28ca7bf6a8
commit 4d39ae5b5d
2 changed files with 8 additions and 3 deletions

View file

@ -1188,16 +1188,20 @@ def analyze_parcel(
geom_wkt: str = geom_row["wkt"] # type: ignore[index]
# 2) District context — ближайший район ЕКБ
# median_price_per_m2: предпочитаем median_12m из mv_quarter_price_per_m2 (12 мес),
# fallback на ekb_districts.median_price_per_m2 (24 мес).
district_row = (
db.execute(
text("""
SELECT district_name,
median_price_per_m2,
SELECT d.district_name,
COALESCE(mq.median_12m, d.median_price_per_m2) AS median_price_per_m2,
ST_Distance(
d.geom::geography,
ST_Centroid(ST_GeomFromText(:wkt, 4326))::geography
) AS dist_to_center
FROM ekb_districts d
LEFT JOIN mv_quarter_price_per_m2 mq
ON mq.quarter_cad_number = REGEXP_REPLACE(:cad_num, ':[^:]+$', '')
WHERE ST_DWithin(
d.geom::geography,
ST_Centroid(ST_GeomFromText(:wkt, 4326))::geography,
@ -1206,7 +1210,7 @@ def analyze_parcel(
ORDER BY dist_to_center ASC
LIMIT 1
"""),
{"wkt": geom_wkt},
{"wkt": geom_wkt, "cad_num": cad_num},
)
.mappings()
.first()

View file

@ -77,6 +77,7 @@ export function OverviewTab({ data, onIsochronesResult }: Props) {
<strong style={{ color: "#374151" }}>
{(data.district.median_price_per_m2 / 1000).toFixed(0)} тыс /м²
</strong>
<span className="text-xs text-slate-500 ml-1">(12 мес)</span>
<span style={{ marginLeft: 12 }}>
{(data.district.dist_to_center / 1000).toFixed(1)} км до центра
</span>