Skip to content

Commit c036b00

Browse files
committed
chg: accumulate multiple params to list and logs
1 parent 7b7453e commit c036b00

2 files changed

Lines changed: 28 additions & 26 deletions

File tree

src/datasync/nva/nva_search_resources.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from collections import defaultdict
2+
13
import typer
24

35
from ..settings import (
@@ -61,8 +63,8 @@ def search_resources_api(
6163
"""
6264
Get resources from NVA API
6365
64-
This will first fetch the data from the NVA API based on the search parameters
65-
then write the data to the specified S3 location
66+
This will use the NVA API to fetch resources based on the provided parameters.
67+
The data will be written to the specified S3 location in parquet format.
6668
6769
Args:
6870
resource_name: Will be used for the output files on S3, avoid using '-'.
@@ -91,7 +93,7 @@ def search_resources_api(
9193
# Filter by contributor and publication year:
9294
uv run datasync nva search-resources-api \
9395
--resource-name "author-publications" \
94-
--filter contributor="https://api.nva.unit.no/cristin/person/1773250" \\
96+
--filter contributor="https://api.nva.unit.no/cristin/person/1773250" \
9597
--filter publication_year_since=2020
9698
"""
9799
log.debug("Starting NVA API search with parameters", filters=filters)
@@ -109,7 +111,7 @@ def search_resources_api(
109111
"in the output file names due to DLT naming conventions."
110112
)
111113

112-
search_params = {}
114+
search_params: defaultdict[str, list[str]] = defaultdict(list)
113115
for filter_str in filters:
114116
key, value = filter_str.split("=", 1)
115117
key = key.strip()
@@ -119,7 +121,8 @@ def search_resources_api(
119121
log.error("Valid filters", valid_filters=VALID_PARAMS_NVA_API)
120122
raise typer.Exit(code=1)
121123

122-
search_params[key] = value
124+
search_params[key].append(value)
125+
123126
log.debug("Parsed search parameters", search_params=search_params)
124127
if not search_params:
125128
log.error("Valid filters", valid_filters=VALID_PARAMS_NVA_API)
@@ -143,7 +146,7 @@ def search_resources_api(
143146
)
144147

145148
log.info(f"Fetching resources from {base_url}")
146-
load_info = pipeline.run(
149+
pipeline.run(
147150
nva_search_source(
148151
base_url=base_url,
149152
resource_name=resource_name,
@@ -153,8 +156,6 @@ def search_resources_api(
153156
loader_file_format="parquet",
154157
)
155158

156-
log.info("Pipeline run completed", load_info=load_info)
157-
158159
if apply_filter:
159160
con = setup_duckdb_s3_connection(
160161
endpoint_url=storage_endpoint_url,
@@ -174,7 +175,6 @@ def search_resources_api(
174175
write_timestamp(con, storage_bucket, storage_prefix)
175176
con.close()
176177

177-
log.info("NVA data sync completed")
178178
log.info(
179179
f"Data available at: {storage_endpoint_url}/{storage_bucket}/{storage_prefix}/"
180180
f"main/{resource_name}.parquet"

src/datasync/nva/utils.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
def get_nva_resources(
1515
client: RESTClient,
16-
search_params: dict[str, str],
16+
search_params: dict[str, list[str]],
1717
):
1818
"""
1919
Function to fetch resources from NVA search API.
@@ -38,7 +38,7 @@ def get_nva_resources(
3838
def nva_search_source(
3939
base_url: str,
4040
resource_name: str = "resources",
41-
search_params: dict[str, str] | None = None,
41+
search_params: dict[str, list[str]] | None = None,
4242
):
4343
"""
4444
DLT source for fetching resources from NVA search API.
@@ -49,19 +49,19 @@ def nva_search_source(
4949
search_params: Dictionary of search parameters (project, publisher, etc.)
5050
5151
Example:
52-
# For project-based search:
53-
nva_search_source(
54-
resource_name="renew_hydro_resources",
55-
search_params={"project": "https://api.nva.unit.no/cristin/project/2732649"}
56-
)
57-
58-
# For publisher-based search:
59-
nva_search_source(
60-
resource_name="atlantic_salmon_resources",
61-
search_params={"publisher": "Vitenskapelig råd for lakseforvaltning"}
62-
)
63-
64-
# For more parameters: https://swagger-ui.nva.unit.no/
52+
# For project-based search:
53+
nva_search_source(
54+
resource_name="renew_hydro_resources",
55+
search_params={"project": ["https://api.nva.unit.no/cristin/project/2732649"]}
56+
)
57+
58+
# For publisher-based search:
59+
nva_search_source(
60+
resource_name="atlantic_salmon_resources",
61+
search_params={"publisher": ["Vitenskapelig råd for lakseforvaltning"]}
62+
)
63+
64+
# For more parameters: https://swagger-ui.nva.unit.no/
6565
"""
6666
if search_params is None:
6767
search_params = {}
@@ -203,5 +203,7 @@ def apply_filter_transformation(
203203
compression="zstd",
204204
overwrite=True,
205205
)
206-
207-
log.info("Filter applied and data written back to S3")
206+
log.info(
207+
"Filter applied and data written back to S3",
208+
number_of_resources=len(filtered_resources),
209+
)

0 commit comments

Comments
 (0)