Skip to content

Commit 37f9862

Browse files
committed
chg: add sync script for publications per researcher
1 parent bcb5f8e commit 37f9862

5 files changed

Lines changed: 106 additions & 17 deletions

File tree

src/datasync/nva/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
# this allows to attach the scripts to the app
33
import typer
44

5-
from . import nva, nva_filter_resources, nva_search_resources
5+
from . import (
6+
nva,
7+
nva_filter_resources,
8+
nva_get_publications_per_researcher,
9+
nva_search_resources,
10+
)
611

712
app = typer.Typer(help="Commands to handle NVA tasks")
813

@@ -15,3 +20,7 @@
1520
app.command(help="Fetch and filter NVA data with flexible search parameters")(
1621
nva_search_resources.search_resources_api
1722
)
23+
24+
app.command(help="Fetch publications for researchers at NINA")(
25+
nva_get_publications_per_researcher.get_pubs_per_researcher
26+
)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
@app.command()
27+
def nva_pubs_per_researcher(
28+
employees_parquet: str = NINA_EMPLOYEES_PARQUET,
29+
storage_s3_path: str = NVA_S3_ENDPOINT_URL,
30+
storage_access_key: str = NVA_S3_ACCESS_KEY,
31+
storage_secret_key: str = NVA_S3_SECRET_KEY,
32+
storage_bucket: str = NVA_S3_BUCKET,
33+
storage_prefix: str = NVA_S3_PREFIX,
34+
storage_region: str = NVA_S3_REGION,
35+
):
36+
log.debug("Fetching ALL publications from researchers at NINA")
37+
38+
if not employees_parquet:
39+
log.error(
40+
"Path to employees parquet file is not set."
41+
"Set the NINA_EMPLOYEES_PARQUET environment variable."
42+
)
43+
raise typer.Exit(code=1)
44+
45+
con = setup_duckdb_s3_connection(
46+
endpoint_url=storage_s3_path,
47+
access_key=storage_access_key,
48+
secret_key=storage_secret_key,
49+
region=storage_region,
50+
)
51+
52+
employees = con.read_parquet(employees_parquet).to_df()
53+
# get all nva_id from employees
54+
nva_ids = employees["nva_id"].tolist()
55+
56+
for nva_id in nva_ids:
57+
log.debug("Fetching publications for researcher", nva_id=nva_id)
58+
search_resources_api(
59+
resource_name=f"publications_{nva_id}",
60+
filters=[f"contributor=https://api.nva.unit.no/cristin/person/{nva_id}"],
61+
apply_filter=False,
62+
storage_endpoint_url=storage_s3_path,
63+
storage_access_key=storage_access_key,
64+
storage_secret_key=storage_secret_key,
65+
storage_bucket=storage_bucket,
66+
storage_prefix=f"{storage_prefix}/{nva_id}/",
67+
storage_region=storage_region,
68+
)
69+
70+
log.info("Fetched all publications for researchers", total_researchers=len(nva_ids))

src/datasync/nva/nva_search_resources.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def search_resources_api(
126126
log.error("Valid filters", valid_filters=VALID_PARAMS_NVA_API)
127127
raise typer.Exit(code=1)
128128

129-
log.info(f"Fetching NVA data with search parameters: {search_params}")
129+
log.debug(f"Fetching NVA data with search parameters: {search_params}")
130130

131131
credentials = create_s3_credentials(
132132
endpoint_url=storage_endpoint_url,
@@ -143,7 +143,7 @@ def search_resources_api(
143143
region=storage_region,
144144
)
145145

146-
log.info(f"Fetching resources from {base_url}")
146+
log.debug(f"Fetching resources from {base_url}")
147147
load_info = pipeline.run(
148148
nva_search_source(
149149
base_url=base_url,
@@ -154,16 +154,15 @@ def search_resources_api(
154154
loader_file_format="parquet",
155155
)
156156

157-
log.info("Pipeline run completed", load_info=load_info)
157+
log.debug("Pipeline run completed", load_info=load_info)
158158

159+
con = setup_duckdb_s3_connection(
160+
endpoint_url=storage_endpoint_url,
161+
access_key=storage_access_key,
162+
secret_key=storage_secret_key,
163+
region=storage_region,
164+
)
159165
if apply_filter:
160-
con = setup_duckdb_s3_connection(
161-
endpoint_url=storage_endpoint_url,
162-
access_key=storage_access_key,
163-
secret_key=storage_secret_key,
164-
region=storage_region,
165-
)
166-
167166
apply_filter_transformation(
168167
con=con,
169168
bucket=storage_bucket,
@@ -175,7 +174,7 @@ def search_resources_api(
175174
write_timestamp(con, storage_bucket, storage_prefix)
176175
con.close()
177176

178-
log.info("NVA data sync completed")
177+
log.debug("NVA data sync completed")
179178
log.info(
180179
f"Data available at: {storage_endpoint_url}/{storage_bucket}/{storage_prefix}/"
181180
f"main/{resource_name}.parquet"

src/datasync/nva/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
NVA_S3_PREFIX = env("NVA_S3_PREFIX", default="")
1414
NVA_S3_REGION = env("NVA_S3_REGION", default="us-east-1")
1515

16+
# Employees parquet file path
17+
NINA_EMPLOYEES_PARQUET = env("NINA_EMPLOYEES_PARQUET", default="")
18+
1619
# Valid search parameters accepted by the NVA API search endpoint
1720
# Based on: https://swagger-ui.nva.unit.no/#/NVA%20Public%20Search%20API/searchResources
1821
VALID_PARAMS_NVA_API = {

uv.lock

Lines changed: 13 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)