Show distinct GESN norm and source document totals

This commit is contained in:
anor
2026-12-01 17:00:48 +10:00
parent e6985aad11
commit 94e3dde629
5 changed files with 57 additions and 8 deletions
+41 -2
View File
@@ -13,13 +13,17 @@ import ext_state
_CACHE_SECONDS = 300
_cache_until = 0.0
_cache_value: dict | None = None
_norm_sig: tuple | None = None
_norm_value: int | None = None
def clear_cache() -> None:
"""Сброс снимка для тестов и оператора после ручного обновления корпуса."""
global _cache_until, _cache_value
global _cache_until, _cache_value, _norm_sig, _norm_value
_cache_until = 0.0
_cache_value = None
_norm_sig = None
_norm_value = None
def _read_meta(path: Path) -> dict | None:
@@ -112,6 +116,33 @@ def _literature_count() -> int:
and path.suffix.lower() in {".pdf", ".doc", ".docx", ".htm", ".html"})
def _norm_count() -> int | None:
"""Число рабочих норм ГЭСН; повторно парсим JSON только при смене файлов."""
global _norm_sig, _norm_value
project = os.environ.get("SMETA_PROJECT_DIR")
if not project:
return None
files = sorted((Path(project) / "data" / "gesn_extra").glob("gesn_*.json"))
if not files:
return None
try:
sig = tuple((str(path), path.stat().st_mtime_ns, path.stat().st_size)
for path in files)
if sig == _norm_sig:
return _norm_value
total = 0
for path in files:
data = json.loads(path.read_text(encoding="utf-8"))
if not isinstance(data, dict):
return None
total += sum(isinstance(item, dict) and "error" not in item
for item in data.values())
except (OSError, ValueError):
return None
_norm_sig, _norm_value = sig, total
return total
def snapshot() -> dict:
"""Снимок на пять минут: не читать сотни файлов на каждый просмотр главной."""
global _cache_until, _cache_value
@@ -119,6 +150,7 @@ def snapshot() -> dict:
if _cache_value is None or now >= _cache_until:
external = _collect()
_cache_value = {"count": external["count"] + _literature_count(),
"norm_count": _norm_count(),
"recent": external["recent"]}
_cache_until = now + _CACHE_SECONDS
return _cache_value
@@ -127,6 +159,9 @@ def snapshot() -> dict:
def render() -> str:
data = snapshot()
count = data["count"]
norm_count = data["norm_count"]
norm_text = (f"{norm_count:,}".replace(",", "\u00a0")
if norm_count is not None else "—")
if data["recent"]:
items = "".join(
'<li class="public-doc-item">'
@@ -141,8 +176,12 @@ def render() -> str:
feed = '<p class="public-doc-empty">Документы пока не загружены.</p>'
return (
'<div class="public-doc-stat">'
'<div class="public-doc-primary">'
f'<strong id="public-norm-count">{norm_text}</strong>'
'<span>норм ГЭСН в поисковой базе</span></div>'
'<div class="public-doc-secondary">'
f'<strong id="public-doc-count">{count}</strong>'
'<span>нормативных документов<br>в базе сервиса</span>'
'<span>нормативных файлов-источников</span></div>'
'</div><div class="public-doc-feed">'
'<h3>Последние загрузки из внешних источников</h3>'
f'{feed}</div>'