"""Public publication evidence from the council export and employee database.""" import calendar import hashlib import json import re from collections import Counter, defaultdict from datetime import date, datetime from functools import lru_cache from pathlib import Path from urllib.parse import quote, urlsplit from zoneinfo import ZoneInfo from bs4 import BeautifulSoup from sqlalchemy import select from sqlalchemy.orm import Session, selectinload from app.models import Employee from app.services.academic_degrees import academic_degrees from app.services.council_source import _doi, _journal_names, _name, _quartiles, load_reference from app.services.council_catalog import catalog, publication_catalog from app.services.council_assessment import ( BRANCHES, assess_candidate, degree_level, evidence, iso_date, publication_proofs, publication_type_allowed, sourced, ) EXPORT = Path(__file__).resolve().parents[1] / "data" / "council_articles.json" K1_JOURNALS = EXPORT.with_name("k1_journals.json") K1_SPECIALTIES = EXPORT.with_name("k1_specialties.json") K2_JOURNALS = EXPORT.with_name("k2_journals.json") DOCTOR = re.compile(r"^доктор\b.+\bнаук$", re.IGNORECASE) TYPES = {"ARTICLE": "Статья", "BOOK": "Книга", "CHAPTER": "Глава книги", "BOOK_CHAPTER": "Глава книги", "REPORT": "Доклад", "PATENT": "Патент", "PREPRINT": "Препринт", "CONFERENCE_PAPER": "Материал конференции"} class CouncilInputError(ValueError): """Invalid public assessment context, as opposed to damaged source data.""" def _safe_url(value: str | None) -> str | None: try: url = urlsplit(value or "") if url.scheme in {"http", "https"} and url.hostname and not url.username and not url.password: return value except ValueError: pass return None def period(today: date) -> tuple[date, date]: year = today.year - 5 return date(year, today.month, min(today.day, calendar.monthrange(year, today.month)[1])), today def _journal_key(value: str) -> str: return " ".join(re.findall(r"\w+", _name(value))) @lru_cache(maxsize=1) def _k1_journals() -> dict[str, str]: data = json.loads(K1_JOURNALS.read_text(encoding="utf-8")) journals = {} for journal in data["journals"]: for name in [journal["name"], *journal.get("aliases", [])]: key = _journal_key(name) if key in journals and journals[key] != journal["name"]: raise ValueError("Ambiguous K1 journal alias") journals[key] = journal["name"] return journals @lru_cache(maxsize=1) def _k1_specialties() -> dict: data = json.loads(K1_SPECIALTIES.read_text(encoding="utf-8")) if hashlib.sha256(K1_JOURNALS.read_bytes().replace(b"\r\n", b"\n")).hexdigest() != data["k1_source_sha256"]: raise ValueError("K1 specialty source checksum mismatch") return {**data, "by_journal": {j["name"]: j["specialty_codes"] for j in data["journals"]}, "journal_issns": {j["name"]: [entry["issn"] for entry in j.get("vak_entries", []) if entry.get("issn")] for j in data["journals"] if j.get("status") == "matched"}} @lru_cache(maxsize=1) def _k2_journals() -> dict: data = json.loads(K2_JOURNALS.read_text(encoding="utf-8")) if data["source_sha256"] != json.loads(K1_JOURNALS.read_text(encoding="utf-8"))["source_sha256"]: raise ValueError("K2 source differs from K1 source") result = {} for journal in data["journals"]: for name in [journal["name"], *journal.get("aliases", [])]: key = _journal_key(name) if key in result and result[key] != journal["name"]: raise ValueError("Ambiguous K2 journal alias") result[key] = journal["name"] return result def _description_journals(description: dict) -> list[str]: names = [] for field in ("short", "shortLeft"): value = description.get(field) values = value.values() if isinstance(value, dict) else [value] for text in values: if isinstance(text, str): text = BeautifulSoup(text, "html.parser").get_text(" ", strip=True) # HSE short descriptions start with the journal, followed by the year. names.append(re.split(r"\b(?:19|20)\d{2}\b", text, maxsplit=1)[0].strip(" .,:;")) return names @lru_cache(maxsize=1) def _articles_by_author() -> dict[str, list[dict]]: return load_reference(EXPORT) def _database_article(publication) -> dict: raw = publication.raw_data if isinstance(publication.raw_data, dict) else {} imported = raw.get("csv_enrichment") from_site = bool(publication.publication_id or publication.url or any(key != "csv_enrichment" for key in raw)) origins = ["БД miem-workers · сайт ВШЭ"] if from_site else [] if imported: origins.append("БД miem-workers · импорт CSV") kind = publication.publication_type description = publication.description if isinstance(publication.description, dict) else raw.get("description") journals = _journal_names(raw) if isinstance(imported, dict): journals.extend(_journal_names(imported)) if isinstance(description, dict): journals.extend(_description_journals(description)) return {"title": publication.title, "year": publication.year, "doi": _doi(publication.doi_url or ""), "types": [TYPES.get(kind.upper(), f"Другой тип: {kind}")] if kind else [], "quartiles": _quartiles(imported) if isinstance(imported, dict) else [], "journals": journals, "issns": [raw[key] for key in ("issn", "eissn") if isinstance(raw.get(key), str)], "source_hashes": [publication.source_hash], "origins": origins or ["БД miem-workers"], "rows": [], "in_csv": False, "in_db": True, "url": _safe_url(publication.url)} def _unique_articles(articles: list[dict], start: date, end: date) -> list[dict]: k1_journals = _k1_journals() specialties = _k1_specialties()["by_journal"] title_years = defaultdict(set) for article in articles: if article["year"] is not None: title_years[_name(article["title"])].add(article["year"]) def title_key(article): title = _name(article["title"]) years = title_years[title] year = article["year"] return title, next(iter(years)) if year is None and len(years) == 1 else year # A title/year fallback is safe only when it identifies at most one DOI. title_dois = defaultdict(set) for article in articles: if article["doi"]: title_dois[title_key(article)].add(article["doi"]) unique = {} for article in articles: fallback = title_key(article) identifiers = title_dois[fallback] identifier = article["doi"] or (next(iter(identifiers)) if len(identifiers) == 1 else "") key = identifier or fallback if key not in unique: unique[key] = {**article, "doi": identifier, **{field: list(article[field]) for field in ("types", "quartiles", "journals", "origins", "rows")}, "evidence": [], "issns": [], "source_hashes": []} merged = unique[key] merged["evidence"].append(article) for field in ("issns", "source_hashes"): merged[field] = list(dict.fromkeys([*merged[field], *article.get(field, [])])) for field in ("types", "quartiles", "journals", "origins", "rows"): merged[field] = list(dict.fromkeys([*merged[field], *article[field]])) merged["in_csv"] |= article["in_csv"] merged["in_db"] |= article["in_db"] merged["url"] = merged["url"] or article["url"] for article in unique.values(): evidence = article.pop("evidence") years = sorted({item["year"] for item in evidence if item["year"] is not None}) # Never pick a conflicting source year merely because that row came first. year = article["year"] = years[0] if len(years) == 1 else None article["source_years"] = years article["criteria"] = list(article["quartiles"]) matches = list(dict.fromkeys(k1_journals[key] for name in article["journals"] if (key := _journal_key(name)) in k1_journals)) article["criteria"].extend(f"К1 · {name} (справочник ВАК)" for name in matches) eligible_type = not article["types"] or article["types"] == ["Статья"] article["q1"] = article["q2"] = article["k1"] = False article["specialty_codes"] = [] for item in evidence: source_year = item["year"] if item["year"] is not None else year if not eligible_type or source_year is None or not start.year <= source_year <= end.year: continue article["q1"] |= any(q.endswith(" Q1") for q in item["quartiles"]) article["q2"] |= any(q.endswith(" Q2") for q in item["quartiles"]) item_journals = {k1_journals[key] for name in item["journals"] if (key := _journal_key(name)) in k1_journals} article["k1"] |= bool(item_journals) article["specialty_codes"] = sorted(set(article["specialty_codes"]).union( *(specialties.get(name, []) for name in item_journals))) article["qualifies"] = article["q1"] or article["q2"] or article["k1"] article["k2"] = any(start.year <= (item["year"] or year or 0) <= end.year and eligible_type and any(_journal_key(name) in _k2_journals() for name in item["journals"]) for item in evidence) article["rsci"] = False article["catalog_records"] = [] article["catalog_issues"] = [] if article["k2"]: article["criteria"].append("К2 · текущий справочник ВАК; историческая категория требует проверки") if len(years) > 1: article["criteria"].append("Годы источников расходятся: " + ", ".join(map(str, years)) + "; критерии учитываются по году каждой исходной записи") elif year is None: article["criteria"].append("Год не указан — период не подтверждён") elif start.year <= year <= end.year: article["criteria"].append("В пределах пяти лет по году; точную дату нужно проверить" if year in {start.year, end.year} else "В пределах пяти лет") else: article["criteria"].append("Вне пятилетнего периода") if not article["quartiles"] and not matches: article["criteria"].append("Q1/Q2 и К1 не подтверждены доступными данными") article["doi_url"] = f"https://doi.org/{quote(article['doi'], safe='/')}" if article["doi"] else None article["type_label"] = " / ".join(article["types"]) or "Тип не указан — статья не подтверждена" if article["types"] and article["types"] != ["Статья"]: article["criteria"].append("Тип материала не подтверждён как статья — не включён в счётчики Q1/Q2/К1") return sorted(unique.values(), key=lambda item: (not item["qualifies"], -(item["year"] or 0), item["title"].casefold())) def council_candidates(db: Session, today: date | None = None, q: str = "", offset: int = 0, sort: str = "total_count", direction: str = "desc", paginate: bool = True, specialty: str = "", petition_date: date | str | None = None, branch: str = "") -> dict: if isinstance(petition_date, str): try: petition_date = iso_date(petition_date) if petition_date else None except ValueError as exc: raise CouncilInputError("Некорректная дата ходатайства; ожидается ГГГГ-ММ-ДД") from exc if petition_date and petition_date.year <= 5: raise CouncilInputError("Дата ходатайства не позволяет построить пятилетний период") if branch and branch not in BRANCHES: raise CouncilInputError("Неизвестная отрасль науки") today = today or datetime.now(ZoneInfo("Europe/Moscow")).date() start, end = period(petition_date or today) specialties = _k1_specialties()["specialties"] if specialty and specialty not in {s["code"] for s in specialties}: raise CouncilInputError("Неизвестная специальность ВАК") specialty_name = next((s["name"] for s in specialties if s["code"] == specialty), "") employees = db.scalars(select(Employee).options(selectinload(Employee.publications))).all() name_counts = Counter(_name(employee.full_name or "") for employee in employees) by_author = _articles_by_author() verified = evidence() candidates = [] for employee in employees: author = _name(employee.full_name or "") ambiguous = bool(author and name_counts[author] > 1 and by_author.get(author)) csv_articles = by_author.get(author, []) if author and name_counts[author] == 1 else [] data = employee.current_data if isinstance(employee.current_data, dict) else {} try: degrees = academic_degrees(data) except (AttributeError, TypeError): degrees = [] employee_evidence = verified["employees"].get(employee.profile_key, {}) if not any(degree_level(degree) for degree in degrees) and not employee_evidence.get("degrees"): continue articles = _unique_articles([*csv_articles, *(_database_article(p) for p in employee.publications)], start, end) for article in articles: proofs = publication_proofs(employee_evidence, article) kinds = {proof["kind"] for proof in proofs if proof.get("kind") and sourced(proof.get("kind_evidence"))} kind = next(iter(kinds)) if len(kinds) == 1 else None preliminary_type_allowed = len(kinds) <= 1 and publication_type_allowed( article["types"], kind, verified.get("policy", {}), preliminary=True) if not preliminary_type_allowed: for flag in ("q1", "q2", "k1", "k2", "rsci"): article[flag] = False identifiers = [*article["issns"], *(value for proof in proofs for value in proof.get("issns", []))] for name in article["journals"]: canonical = _k1_journals().get(_journal_key(name)) identifiers.extend(_k1_specialties()["journal_issns"].get(canonical, [])) article["catalog_records"], article["catalog_issues"] = publication_catalog(identifiers, article["journals"], specialty) for record in article["catalog_records"]: for metric in record["metrics"]: if article["year"] == metric["year"] and start.year <= article["year"] <= end.year and metric["quartile"] in {"Q1", "Q2"} and not article["catalog_issues"] and preliminary_type_allowed: article[metric["quartile"].lower()] = True for proof in proofs: if not preliminary_type_allowed: continue for basis in proof.get("bases", []): if not isinstance(basis, dict) or basis.get("applicable_year") != article["year"] or article["year"] is None or not start.year <= article["year"] <= end.year: continue key = {"К1": "k1", "К2": "k2", "Q1": "q1", "Q2": "q2", "RSCI": "rsci"}.get(basis.get("basis")) if key: article[key] = True assessment = assess_candidate(degrees, articles, employee_evidence, petition_date, branch, specialty, start, end) specialty_count = sum(specialty in a["specialty_codes"] for a in articles) if specialty else 0 for article in articles: article["matches_specialty"] = bool(specialty and specialty in article["specialty_codes"]) if article["matches_specialty"]: article["criteria"].append(f"По специальности {specialty}: {specialty_name} (журнал К1, Перечень ВАК)") candidates.append({**assessment, "specialty_count": specialty_count,"name": employee.full_name or "ФИО не указано", "count": len(articles), **{f"{key}_count": sum(a[key] for a in articles) for key in ("q1", "q2", "k1", "k2", "rsci")}, "total_count": sum(a["qualifies"] for a in articles), "db_count": sum(a["in_db"] for a in articles), "articles": articles, "profile_url": _safe_url(employee.canonical_url), "ambiguous": ambiguous, "dismissed": employee.status == "dismissed"}) sort = sort if sort in {"full_name", "q1_count", "q2_count", "k1_count", "k2_count", "rsci_count", "confirmed_count", "total_count", "db_count", "count"} else "total_count" direction = direction if direction in {"asc", "desc"} else "desc" candidates.sort(key=lambda item: _name(item["name"]), reverse=sort == "full_name" and direction == "desc") if sort != "full_name": candidates.sort(key=lambda item: (item[sort], item["count"]), reverse=direction == "desc") total = len(candidates) query = _name(q) candidates = [item for item in candidates if query in _name(item["name"]) and (not specialty or item["specialty_count"] > 0)] matched = len(candidates) limit = 25 offset = min(max(0, offset), ((matched - 1) // limit) * limit) if matched else 0 return {"candidates": candidates[offset:offset + limit] if paginate else candidates, "start": start, "end": end, "total": total, "specialties": specialties, "specialty": specialty, "specialty_name": specialty_name, "petition_date": petition_date, "branch": branch, "branches": BRANCHES, "catalog_metadata": catalog()["metadata"], "catalog_issues": catalog()["issues"], "category_metadata": [{"file": path.name, **{key: data[key] for key in ("source", "source_sha256", "source_rows")}} for path in (K1_JOURNALS, K2_JOURNALS) for data in [json.loads(path.read_text(encoding="utf-8"))]], "matched": matched, "q": q, "offset": offset, "limit": limit, "sort": sort, "direction": direction}