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