447 lines
30 KiB
Python
447 lines
30 KiB
Python
"""Public publication evidence from the council export and employee database."""
|
||
|
||
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 sqlalchemy import select, func, case, cast
|
||
from sqlalchemy.dialects.postgresql import JSONPATH
|
||
from sqlalchemy.orm import Session
|
||
|
||
from app.models import Employee, EmployeePublication, CouncilCorrections, json_type
|
||
from app.services.academic_degrees import academic_degrees
|
||
from app.services.council_source import _doi, _journal_names, _name, load_reference, _description_journals
|
||
from app.services.council_catalog import catalog, publication_catalog
|
||
from app.services.publication_quartiles import qualifying_entries, identity_issues, hse_entries, merge as merge_quartiles
|
||
from app.services.council_assessment import (
|
||
BRANCHES, assess_candidate, degree_level, evidence, 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]:
|
||
return date(today.year - 4, 1, 1), date(today.year, 12, 31)
|
||
|
||
|
||
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
|
||
|
||
|
||
@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 = not imported or bool(publication.publication_id or publication.url or getattr(publication, "raw_has_metadata", any(key != "csv_enrichment" for key in raw)))
|
||
origins = ["ЛК сотрудника ВШЭ"] if from_site else []
|
||
if imported:
|
||
origins.append("Данные ВШЭ")
|
||
kind = publication.publication_type
|
||
description = publication.description if isinstance(publication.description, dict) else raw.get("description")
|
||
journals = _journal_names(raw)
|
||
if isinstance(description, dict):
|
||
journals.extend(_description_journals(description))
|
||
doi = _doi(publication.doi_url or "")
|
||
identifiers = [raw[key] for key in ("issn", "eissn", "ISSN", "EISSN") if isinstance(raw.get(key), str)]
|
||
current_journals = list(journals)
|
||
imported_quartiles = merge_quartiles(None, hse_entries(raw, publication.year))
|
||
issues = identity_issues(publication.quartiles, doi, current_journals, identifiers)
|
||
imported_issues = identity_issues(imported_quartiles, doi, current_journals, identifiers)
|
||
imported_doi = _doi(str(imported.get("DOI") or "")) if isinstance(imported, dict) else ""
|
||
if isinstance(imported, dict) and not imported_issues and (not imported_doi or imported_doi == doi):
|
||
journals.extend(_journal_names(imported))
|
||
issues = list(dict.fromkeys([*issues, *imported_issues]))
|
||
quartile_entries = [*qualifying_entries(publication.quartiles, publication.year, doi, current_journals, identifiers),
|
||
*qualifying_entries(imported_quartiles, publication.year, doi, current_journals, identifiers)]
|
||
return {"title": publication.title, "year": publication.year, "doi": _doi(publication.doi_url or ""),
|
||
"types": [TYPES.get(kind.upper(), f"Другой тип: {kind}")] if kind else [],
|
||
"quartiles": list(dict.fromkeys(f"{item['system']} {item['quartile']}" for item in quartile_entries)),
|
||
"quartile_entries": quartile_entries,
|
||
"quartile_issues": issues,
|
||
"journals": journals, "issns": [raw[key] for key in ("issn", "eissn", "ISSN", "EISSN") if isinstance(raw.get(key), str)],
|
||
"source_hashes": [publication.source_hash],
|
||
"origins": origins, "rows": [], "in_csv": bool(imported), "in_db": from_site,
|
||
"url": _safe_url(publication.url)}
|
||
|
||
|
||
def _unique_articles(articles: list[dict], start: date, end: date, corrections=None, employee_id=None) -> 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": [], "quartile_issues": []}
|
||
merged = unique[key]
|
||
merged["evidence"].append(article)
|
||
merged["quartile_issues"] = list(dict.fromkeys([*merged["quartile_issues"], *article.get("quartile_issues", [])]))
|
||
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 identity, article in unique.items():
|
||
evidence = article.pop("evidence")
|
||
article["edit_key"] = hashlib.sha256(json.dumps(
|
||
identity,
|
||
ensure_ascii=False).encode()).hexdigest()
|
||
correction = (corrections or {}).get("articles", {}).get(f"{employee_id}:{article['edit_key']}")
|
||
article["manually_corrected"] = bool(correction)
|
||
article["journal_corrected"] = bool(correction and article["journals"] != [correction["journal"]])
|
||
if correction:
|
||
journal_changed = article["journal_corrected"]
|
||
article["title"] = correction["title"]
|
||
article["journals"] = [correction["journal"]]
|
||
if journal_changed:
|
||
article["issns"] = []
|
||
article["quartiles"] = []
|
||
article["quartile_issues"] = []
|
||
evidence = [{**item, "journals": article["journals"],
|
||
"quartiles": [] if journal_changed else item["quartiles"]} for item in 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["automatic_quartile_bases"] = []
|
||
if len(article["journals"]) == 1 and not article["quartile_issues"] and not article["journal_corrected"]:
|
||
for item in evidence:
|
||
if item["year"] != year:
|
||
continue
|
||
details = item.get("quartile_entries")
|
||
if details is None:
|
||
# Prepared CSV evidence has no category or metric metadata.
|
||
details = [{"quartile": label[-2:], "system": label[:-3],
|
||
"source": " / ".join(item["origins"]), "source_url": item.get("url")}
|
||
for label in item["quartiles"] if label.endswith((" Q1", " Q2"))]
|
||
article["automatic_quartile_bases"].extend(
|
||
{**entry, "basis": entry["quartile"], "applicable_year": year} for entry in details)
|
||
article["criteria"] = [*article["quartiles"], *article["quartile_issues"]]
|
||
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(f"В пределах пятилетнего периода {start.year}–{end.year}")
|
||
else:
|
||
article["criteria"].append(f"Вне пятилетнего периода {start.year}–{end.year}")
|
||
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 = "",
|
||
branch: str = "технические", employee_id: int | None = None,
|
||
previous_specialty: str | None = None) -> dict:
|
||
if branch not in BRANCHES:
|
||
raise CouncilInputError("Неизвестная отрасль науки")
|
||
today = today or datetime.now(ZoneInfo("Europe/Moscow")).date()
|
||
start, end = period(today)
|
||
specialties = _k1_specialties()["specialties"]
|
||
if specialty and specialty not in {s["code"] for s in specialties}:
|
||
raise CouncilInputError("Неизвестная специальность ВАК")
|
||
branches = list(dict.fromkeys(name for s in specialties if not specialty or s["code"] == specialty
|
||
for name in s["branches"]))
|
||
if branch and branch not in branches:
|
||
previous_branches = {name for s in specialties if previous_specialty == "" or s["code"] == previous_specialty
|
||
for name in s["branches"]}
|
||
if previous_specialty is not None and previous_specialty != specialty and branch in previous_branches:
|
||
branch = branches[0]
|
||
else:
|
||
raise CouncilInputError("Отрасль оценки не относится к выбранным специальностям ВАК")
|
||
specialty_name = next((s["name"] for s in specialties if s["code"] == specialty), "")
|
||
degree_sections = Employee.current_data["sections"]
|
||
if db.get_bind().dialect.name == "postgresql":
|
||
degree_sections = func.jsonb_path_query_array(Employee.current_data,
|
||
cast('$.sections[*] ? (@.title like_regex "образован|степен|academic degree|education" flag "i")', JSONPATH), type_=json_type)
|
||
employees = db.execute(select(Employee.id, Employee.profile_key, Employee.full_name, Employee.status,
|
||
Employee.canonical_url, degree_sections.label("degree_sections"))).all()
|
||
name_counts = Counter(_name(employee.full_name or "") for employee in employees)
|
||
by_author = _articles_by_author()
|
||
verified = evidence()
|
||
corrections_record = db.get(CouncilCorrections, 1)
|
||
corrections = corrections_record.data if corrections_record else {}
|
||
revision = corrections_record.revision if corrections_record else 0
|
||
eligible = []
|
||
degrees_by_employee = {}
|
||
for employee in employees:
|
||
try:
|
||
degrees = academic_degrees({"sections": employee.degree_sections})
|
||
except (AttributeError, TypeError):
|
||
degrees = []
|
||
if any(degree_level(degree) for degree in degrees) or verified["employees"].get(employee.profile_key, {}).get("degrees"):
|
||
eligible.append(employee)
|
||
degrees_by_employee[employee.id] = degrees
|
||
total = len(eligible)
|
||
query = _name(q)
|
||
eligible = [employee for employee in eligible if query in _name(employee.full_name or "ФИО не указано")
|
||
and (employee_id is None or employee.id == employee_id)]
|
||
publication_fields = [EmployeePublication.employee_id, EmployeePublication.title, EmployeePublication.year,
|
||
EmployeePublication.publication_type, EmployeePublication.publication_id, EmployeePublication.url,
|
||
EmployeePublication.doi_url, EmployeePublication.source_hash, EmployeePublication.quartiles]
|
||
raw_data, description = EmployeePublication.raw_data, EmployeePublication.description
|
||
if db.get_bind().dialect.name == "postgresql":
|
||
# Only council metadata crosses the DB boundary; abstracts/authors stay in PostgreSQL.
|
||
description = case((func.jsonb_typeof(description) == "object", func.jsonb_build_object(
|
||
"short", description["short"], "shortLeft", description["shortLeft"], type_=json_type)),
|
||
else_=description)
|
||
raw = EmployeePublication.raw_data
|
||
raw_has_metadata = case((func.jsonb_typeof(raw) == "object",
|
||
raw != func.jsonb_build_object("csv_enrichment", raw["csv_enrichment"])), else_=False)
|
||
raw_keys = ("csv_enrichment", "journal", "Журнал", "Название журнала", "issn", "eissn", "ISSN", "EISSN")
|
||
raw_data = case((func.jsonb_typeof(raw) == "object", func.jsonb_build_object(
|
||
*[value for key in raw_keys for value in (key, raw[key])],
|
||
"description", case((func.jsonb_typeof(EmployeePublication.description) == "object", None),
|
||
else_=raw["description"]), type_=json_type)), else_=None)
|
||
publication_fields.append(raw_has_metadata.label("raw_has_metadata"))
|
||
publications_by_employee = defaultdict(list)
|
||
if eligible:
|
||
publications = db.execute(select(*publication_fields, raw_data.label("raw_data"), description.label("description"))
|
||
.where(EmployeePublication.employee_id.in_([e.id for e in eligible]))).all()
|
||
for publication in publications:
|
||
publications_by_employee[publication.employee_id].append(publication)
|
||
candidates = []
|
||
for employee in eligible:
|
||
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 []
|
||
degrees = degrees_by_employee[employee.id]
|
||
employee_evidence = verified["employees"].get(employee.profile_key, {})
|
||
articles = _unique_articles([*csv_articles, *(_database_article(p) for p in publications_by_employee[employee.id])], start, end, corrections, employee.id)
|
||
for article in articles:
|
||
article["manual_review"] = article["journal_corrected"] or (len(article["journals"]) == 1 and
|
||
f"{_journal_key(article['journals'][0])}:{article['year']}" in corrections.get("journals", {}))
|
||
proofs = [] if article["manual_review"] else 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 = [] if article["journal_corrected"] else [*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
|
||
if not article["catalog_issues"] and not article["journal_corrected"]:
|
||
article["automatic_quartile_bases"].extend({"basis": metric["quartile"], "system": metric["system"],
|
||
"metric": metric["metric"], "category": metric["category"], "metric_year": metric["year"],
|
||
"applicable_year": article["year"], "source": metric["source"], "source_url": metric["source_url"]}
|
||
for record in article["catalog_records"] for metric in record["metrics"]
|
||
if article["year"] == metric["year"] and metric["quartile"] in {"Q1", "Q2"})
|
||
journal_key = _journal_key(article["journals"][0]) if len(article["journals"]) == 1 else ""
|
||
manual_quartile = corrections.get("journals", {}).get(f"{journal_key}:{article['year']}")
|
||
if manual_quartile is not None:
|
||
article["quartiles"] = [f"Ручная корректировка {manual_quartile}"] if manual_quartile else []
|
||
article["q1"] = bool(manual_quartile == "Q1" and preliminary_type_allowed and article["year"] is not None and start.year <= article["year"] <= end.year)
|
||
article["q2"] = bool(manual_quartile == "Q2" and preliminary_type_allowed and article["year"] is not None and start.year <= article["year"] <= end.year)
|
||
article["criteria"] = [c for c in article["criteria"] if not c.endswith((" Q1", " Q2", " Q3", " Q4")) and c != "Q1/Q2 и К1 не подтверждены доступными данными"]
|
||
article["criteria"].append(f"Квартиль журнала за {article['year']}: {manual_quartile or 'не указан'} · ручная корректировка, требует подтверждения")
|
||
if article["manual_review"]:
|
||
article["automatic_quartile_bases"] = []
|
||
manual_specialties = corrections.get("specialties", {}).get(journal_key) if journal_key else None
|
||
article["manual_specialties"] = manual_specialties is not None
|
||
default_specialties = sorted(set().union(
|
||
*(_k1_specialties()["by_journal"].get(_k1_journals().get(_journal_key(name)), []) for name in article["journals"]),
|
||
*(record["specialty_codes"] for record in article["catalog_records"])))
|
||
article["edit_specialty_codes"] = manual_specialties if manual_specialties is not None else default_specialties
|
||
if manual_specialties is not None:
|
||
eligible = preliminary_type_allowed and article["year"] is not None and start.year <= article["year"] <= end.year
|
||
article["specialty_codes"] = manual_specialties if eligible else []
|
||
for record in article["catalog_records"]:
|
||
record["thematic"] = (["Связь журнала со специальностью: ручная корректировка"] if specialty in manual_specialties
|
||
else ["Связь журнала с выбранной специальностью исключена вручную"] if specialty else [])
|
||
article["qualifies"] = article["q1"] or article["q2"] or article["k1"]
|
||
labels = {q[-2:] for q in article["quartiles"] if q.endswith((" Q1", " Q2", " Q3", " Q4"))}
|
||
if manual_quartile is None:
|
||
labels.update(metric["quartile"] for record in article["catalog_records"] for metric in record["metrics"]
|
||
if metric["year"] == article["year"] and metric["quartile"] in {"Q1", "Q2", "Q3", "Q4"})
|
||
labels.update(basis["basis"] for proof in proofs for basis in proof.get("bases", [])
|
||
if isinstance(basis, dict) and basis.get("applicable_year") == article["year"] and basis.get("basis") in {"Q1", "Q2", "Q3", "Q4"})
|
||
article["edit_quartile"] = manual_quartile if manual_quartile is not None else next(iter(labels)) if len(labels) == 1 else ""
|
||
article["edit_journal"] = article["journals"][0] if len(article["journals"]) == 1 else ""
|
||
changed = [a for a in articles if a["manual_review"]]
|
||
adjusted_evidence = {**employee_evidence, "publications": [p for p in employee_evidence.get("publications", [])
|
||
if not any((a["doi"] and p.get("doi") == a["doi"]) or p.get("source_hash") in a["source_hashes"] for a in changed)]}
|
||
assessment = assess_candidate(degrees, articles, adjusted_evidence, branch, specialty, start, end)
|
||
for article in changed:
|
||
message = "Журнал или квартиль исправлен вручную. Для зачёта требуется источник, подтверждающий эти сведения за год статьи"
|
||
article["confirmation_reasons"].append(message)
|
||
article["warnings"]["quartile"].append(message)
|
||
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} ({'ручная связь журнала' if article['manual_specialties'] else 'журнал К1, Перечень ВАК'})")
|
||
for article in articles:
|
||
if specialty:
|
||
article["warnings"]["specialty"].extend(text for record in article["catalog_records"]
|
||
for text in record["thematic"] if not text.startswith(("Паспорт ", "Аналитическая связь журнала:")))
|
||
article["messages"] = list(dict.fromkeys([*article["criteria"], *article["confirmation_reasons"]]))
|
||
candidates.append({**assessment, "specialty_count": specialty_count,"name": employee.full_name or "ФИО не указано", "employee_id": employee.id,
|
||
"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")
|
||
candidates = [item for item in candidates if 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
|
||
criteria_groups = {}
|
||
for name in branches:
|
||
minimum, monographs = BRANCHES[name]
|
||
criteria_groups.setdefault((minimum, monographs), []).append(name)
|
||
return {"criteria_groups": [{"names": ", ".join(names), "minimum": minimum, "monographs": monographs}
|
||
for (minimum, monographs), names in criteria_groups.items()], "journal_names": sorted({name for c in candidates for a in c["articles"] for name in a["journals"]}), "revision": revision, "candidates": candidates[offset:offset + limit] if paginate else candidates,
|
||
"start": start, "end": end, "total": total,
|
||
"specialties": specialties, "specialty": specialty, "specialty_name": specialty_name,
|
||
"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}
|