"""Public publication evidence from the council export and employee database.""" import calendar import csv import re import unicodedata 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 sqlalchemy import select from sqlalchemy.orm import Session, selectinload from app.models import Employee from app.services.academic_degrees import academic_degrees EXPORT = Path(__file__).resolve().parents[1] / "data" / "council_articles.csv" DOCTOR = re.compile(r"^доктор\b.+\bнаук$", re.IGNORECASE) DOI = re.compile(r"10\.\d{4,9}/\S+", re.IGNORECASE) TYPES = {"ARTICLE": "Статья", "BOOK": "Книга", "CHAPTER": "Глава книги", "BOOK_CHAPTER": "Глава книги", "REPORT": "Доклад", "PATENT": "Патент", "PREPRINT": "Препринт", "CONFERENCE_PAPER": "Материал конференции"} def _name(value: str) -> str: return " ".join(unicodedata.normalize("NFKC", value).casefold().replace("ё", "е").split()) def _doi(value: str) -> str: value = re.sub(r"^(?:https?://(?:dx\.)?doi\.org/|doi:\s*)", "", value.strip(), flags=re.I).lower() return value if DOI.fullmatch(value) else "" 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 _quartiles(row: dict) -> list[str]: return [f"{label} {str(row.get(field) or '').strip().upper()}" for label, field in (("Scopus", "Квартиль scopus"), ("Web of Science", "Квартиль wos")) if str(row.get(field) or '').strip().upper() in {"Q1", "Q2"}] @lru_cache(maxsize=1) def _articles_by_author() -> dict[str, list[dict]]: articles = defaultdict(list) with EXPORT.open(encoding="utf-8-sig", newline="") as source: reader = csv.DictReader(source, delimiter=";") required = {"Название публикации", "Год", "DOI", "Авторы НИУ ВШЭ", "Квартиль scopus", "Квартиль wos"} if not required <= set(reader.fieldnames or []): raise ValueError("Missing council CSV headers") for row_number, row in enumerate(reader, 2): if None in row or any(value is None for value in row.values()): raise ValueError("Malformed council CSV record") title = row["Название публикации"].strip() if not title: continue year = int(row["Год"]) if row["Год"].strip().isdigit() else None article = {"title": title, "year": year, "doi": _doi(row["DOI"]), "types": [], "quartiles": _quartiles(row), "origins": ["Файл CSV"], "rows": [row_number], "in_csv": True, "in_db": False, "url": None} for author in set(_name(value) for value in row["Авторы НИУ ВШЭ"].split(";")) - {""}: articles[author].append(article) return articles 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 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 [], "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]: 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", "origins", "rows")}, "csv_qualifies": False} merged = unique[key] for field in ("types", "quartiles", "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"] merged["year"] = merged["year"] or article["year"] in_period = article["year"] is not None and start.year <= article["year"] <= end.year merged["csv_qualifies"] |= bool(article["in_csv"] and article["quartiles"] and in_period) for article in unique.values(): year = article["year"] article["criteria"] = list(article["quartiles"]) if 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"]: article["criteria"].append("Q1/Q2, К1 и RSCI не подтверждены доступными данными") 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["csv_qualifies"] = False article["criteria"].append("Тип материала не подтверждён как статья — не включён в счётчик CSV") return sorted(unique.values(), key=lambda item: (not item["csv_qualifies"], -(item["year"] or 0), item["title"].casefold())) def council_candidates(db: Session, today: date | None = None, q: str = "", offset: int = 0, sort: str = "csv_count", direction: str = "desc") -> dict: today = today or datetime.now(ZoneInfo("Europe/Moscow")).date() start, end = period(today) 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() 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 [] articles = _unique_articles([*csv_articles, *(_database_article(p) for p in employee.publications)], start, end) if not articles: continue data = employee.current_data if isinstance(employee.current_data, dict) else {} try: degrees = academic_degrees(data) except (AttributeError, TypeError): degrees = [] if not any(DOCTOR.fullmatch(degree) for degree in degrees): continue candidates.append({"name": employee.full_name or "ФИО не указано", "degree": "; ".join(degrees) or "Учёная степень не указана", "count": len(articles), "csv_count": sum(a["csv_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", "csv_count", "db_count", "count"} else "csv_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"])] matched = len(candidates) limit = 25 offset = min(max(0, offset), ((matched - 1) // limit) * limit) if matched else 0 return {"candidates": candidates[offset:offset + limit], "start": start, "end": end, "total": total, "matched": matched, "q": q, "offset": offset, "limit": limit, "sort": sort, "direction": direction}