|
| 1 | +# get nva id's https://s3-ext-1.nina.no/dms/nina/employees.parquet |
| 2 | +# https://api.nva.unit.no/search/resources?contributor=https://api.nva.unit.no/cristin/person/408357 |
| 3 | +# use search_api function in nva_search_resources.py |
| 4 | +import typer |
| 5 | + |
| 6 | +from ..settings import ( |
| 7 | + log, |
| 8 | +) |
| 9 | +from .nva_search_resources import search_resources_api |
| 10 | +from .settings import ( |
| 11 | + NINA_EMPLOYEES_PARQUET, |
| 12 | + NVA_S3_ACCESS_KEY, |
| 13 | + NVA_S3_BUCKET, |
| 14 | + NVA_S3_ENDPOINT_URL, |
| 15 | + NVA_S3_PREFIX, |
| 16 | + NVA_S3_REGION, |
| 17 | + NVA_S3_SECRET_KEY, |
| 18 | +) |
| 19 | +from .utils import ( |
| 20 | + setup_duckdb_s3_connection, |
| 21 | +) |
| 22 | + |
| 23 | +app = typer.Typer(help="Fetching publications from researchers at NINA") |
| 24 | + |
| 25 | + |
| 26 | +def _fetch_publications_for_researcher( |
| 27 | + nva_id: str, |
| 28 | + storage_s3_path: str, |
| 29 | + storage_access_key: str, |
| 30 | + storage_secret_key: str, |
| 31 | + storage_bucket: str, |
| 32 | + storage_prefix: str, |
| 33 | + storage_region: str, |
| 34 | +) -> str: |
| 35 | + log.debug("Fetching publications for researcher", nva_id=nva_id) |
| 36 | + search_resources_api( |
| 37 | + resource_name=f"publications_{nva_id}", |
| 38 | + filters=[f"contributor=https://api.nva.unit.no/cristin/person/{nva_id}"], |
| 39 | + apply_filter=False, |
| 40 | + storage_endpoint_url=storage_s3_path, |
| 41 | + storage_access_key=storage_access_key, |
| 42 | + storage_secret_key=storage_secret_key, |
| 43 | + storage_bucket=storage_bucket, |
| 44 | + storage_prefix=f"{storage_prefix}/{nva_id}/", |
| 45 | + storage_region=storage_region, |
| 46 | + ) |
| 47 | + return nva_id |
| 48 | + |
| 49 | + |
| 50 | +@app.command() |
| 51 | +def get_pubs_per_researcher( |
| 52 | + employees_parquet: str = NINA_EMPLOYEES_PARQUET, |
| 53 | + storage_s3_path: str = NVA_S3_ENDPOINT_URL, |
| 54 | + storage_access_key: str = NVA_S3_ACCESS_KEY, |
| 55 | + storage_secret_key: str = NVA_S3_SECRET_KEY, |
| 56 | + storage_bucket: str = NVA_S3_BUCKET, |
| 57 | + storage_prefix: str = NVA_S3_PREFIX, |
| 58 | + storage_region: str = NVA_S3_REGION, |
| 59 | +): |
| 60 | + log.debug("Fetching ALL publications from researchers at NINA") |
| 61 | + |
| 62 | + if not employees_parquet: |
| 63 | + log.error( |
| 64 | + "Path to employees parquet file is not set." |
| 65 | + "Set the NINA_EMPLOYEES_PARQUET environment variable." |
| 66 | + ) |
| 67 | + raise typer.Exit(code=1) |
| 68 | + |
| 69 | + con = setup_duckdb_s3_connection( |
| 70 | + endpoint_url=storage_s3_path, |
| 71 | + access_key=storage_access_key, |
| 72 | + secret_key=storage_secret_key, |
| 73 | + region=storage_region, |
| 74 | + ) |
| 75 | + |
| 76 | + employees = con.read_parquet(employees_parquet).to_df() |
| 77 | + |
| 78 | + # get all employees with missing nva_id and log their names for debugging |
| 79 | + missing_nva_id = employees["nva_id"].isna().sum() |
| 80 | + if missing_nva_id > 0: |
| 81 | + missing_names = ( |
| 82 | + employees[employees["nva_id"].isna()]["firstname"] |
| 83 | + + " " |
| 84 | + + employees[employees["nva_id"].isna()]["lastname"] |
| 85 | + ).tolist() |
| 86 | + log.warning( |
| 87 | + "Employees missing NVA IDs", |
| 88 | + missing_count=missing_nva_id, |
| 89 | + total_employees=len(employees), |
| 90 | + missing_names=missing_names, |
| 91 | + ) |
| 92 | + con.close() |
| 93 | + nva_ids = [str(nva_id) for nva_id in employees["nva_id"].dropna().unique()] |
| 94 | + |
| 95 | + failed = [] |
| 96 | + for nva_id in nva_ids: |
| 97 | + try: |
| 98 | + _fetch_publications_for_researcher( |
| 99 | + nva_id, |
| 100 | + storage_s3_path, |
| 101 | + storage_access_key, |
| 102 | + storage_secret_key, |
| 103 | + storage_bucket, |
| 104 | + storage_prefix, |
| 105 | + storage_region, |
| 106 | + ) |
| 107 | + except Exception as e: |
| 108 | + log.error( |
| 109 | + "Failed to fetch publications for researcher", |
| 110 | + nva_id=nva_id, |
| 111 | + error=str(e), |
| 112 | + ) |
| 113 | + failed.append(nva_id) |
| 114 | + |
| 115 | + if failed: |
| 116 | + log.warning( |
| 117 | + "Some researchers failed to fetch", failed=failed, count=len(failed) |
| 118 | + ) |
| 119 | + |
| 120 | + log.info( |
| 121 | + "Fetched all publications for researchers", |
| 122 | + total_researchers=len(nva_ids), |
| 123 | + failed=len(failed), |
| 124 | + succeeded=len(nva_ids) - len(failed), |
| 125 | + ) |
0 commit comments