From 69ab09e30411f6936ce78b28f907e3165e8cd588 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CNaeel=E2=80=9D?= Date: Sun, 28 Jun 2026 10:10:00 +0400 Subject: [PATCH] =?UTF-8?q?fix:=20=D0=BD=D0=BE=D1=80=D0=BC=D0=B0=D0=BB?= =?UTF-8?q?=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20=D1=87=D0=B8=D1=81=D0=B5?= =?UTF-8?q?=D0=BB=20(=D0=BF=D1=80=D0=BE=D0=B1=D0=B5=D0=BB=D1=8B/=D0=B7?= =?UTF-8?q?=D0=B0=D0=BF=D1=8F=D1=82=D1=8B=D0=B5)=20=D0=B8=20=D0=B4=D0=B0?= =?UTF-8?q?=D1=82=20(DD.MM.YYYY=E2=86=92YYYY-MM-DD)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/db/spec_events.py | 7 +++++-- deploy/services/metrics.py | 20 ++++++++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/deploy/db/spec_events.py b/deploy/db/spec_events.py index ee2f794..758d58f 100644 --- a/deploy/db/spec_events.py +++ b/deploy/db/spec_events.py @@ -152,11 +152,14 @@ def _log_unresolved(contract_id, supplement_id, seq, op, prompt_id, document_id, def _hash(name, date_start=None): - """Нормализованный хеш услуги. Включает date_start чтобы различать периоды.""" + """Нормализованный хеш услуги. Включает нормализованный date_start чтобы различать периоды.""" import hashlib key = name.strip().lower() if date_start: - key += "|" + str(date_start) + from services.metrics import normalize_date + nd = normalize_date(str(date_start)) + if nd: + key += "|" + nd return hashlib.sha256(key.encode()).hexdigest()[:16] diff --git a/deploy/services/metrics.py b/deploy/services/metrics.py index 9a37f22..4f389fa 100644 --- a/deploy/services/metrics.py +++ b/deploy/services/metrics.py @@ -63,10 +63,26 @@ class ClassifyMetrics: def _to_decimal(val) -> Decimal | None: - """Safe conversion to Decimal.""" + """Safe conversion to Decimal with number normalization.""" if val is None or val == "": return None try: - return Decimal(str(val)) + s = str(val).replace("\xa0", "").replace(" ", "").replace(",", ".") + return Decimal(s) except (InvalidOperation, ValueError): return None + + +def normalize_date(ds: str) -> str | None: + """Normalize date to YYYY-MM-DD. Handles DD.MM.YYYY, YYYY-MM-DD, etc.""" + if not ds: + return None + import re + # DD.MM.YYYY → YYYY-MM-DD + m = re.match(r"(\d{2})\.(\d{2})\.(\d{4})", ds) + if m: + return f"{m.group(3)}-{m.group(2)}-{m.group(1)}" + # YYYY-MM-DD — already canonical + if re.match(r"\d{4}-\d{2}-\d{2}", ds): + return ds + return ds # return as-is if unrecognized format