-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Targeted ingestion provider workflows
- Loading branch information
1 parent
f944960
commit 59d2e4c
Showing
5 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
catalog/dags/providers/provider_api_scripts/targeted_records_provider_data_ingester.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from providers.provider_api_scripts.provider_data_ingester import ProviderDataIngester | ||
|
||
|
||
class TargetedRecordsProviderDataIngesterMixin: | ||
""" | ||
A mixin used with ProviderDataIngesters to create subclasses for ingesting targeted records. | ||
Records are identified to the `ingest_records` entrypoint by their foreign identifiers. | ||
This class leverages existing functionality in ProviderDataIngester implementations | ||
like the batched processing methods. It mimics batched processing by wrapping the | ||
responses for each retrieved record in a list. In other words, each individual record | ||
is treated as its own "batch", which would have otherwise corresponded to a single | ||
page of the regular ingestion workflow for the provider. As such, if the provider returns | ||
the same data format for individual records as for those records in search, it is not necessary | ||
to override the data processing methods for individual records that will already be present | ||
in ProviderDataIngester implementations. | ||
At request time, URLs and query parameters unique to each foreign identifier should | ||
be created by implementing overrides to `get_endpoint` and `get_query_params`, each | ||
of which take the foreign identifier as an argument. For more complex scenarios, | ||
you may override `get_record` instead for. | ||
""" | ||
|
||
def __init__(self: ProviderDataIngester, conf: dict, *args, **kwargs): | ||
self.foreign_identifiers = conf["foreign_identifiers"] | ||
super().__init__(conf, *args, **kwargs) | ||
|
||
def ingest_records(self: ProviderDataIngester) -> None: | ||
logger.info(f"Begin ingestion of single records for {self.__class__.__name__}") | ||
|
||
for foreign_identifier in self.foreign_identifiers: | ||
single_record = self.get_record(foreign_identifier) | ||
self.record_count += self.process_batch([single_record]) | ||
logger.info(f"{self.record_count} records ingested so far.") | ||
|
||
if self.limit and self.record_count >= self.limit: | ||
logger.info(f"Ingestion limit of {self.limit} has been reached.") | ||
break | ||
|
||
self._commit_records() | ||
|
||
def get_query_params(self: ProviderDataIngester, foreign_identifier: str) -> dict: | ||
"""Build the query params to request the data for the given foreign identifier""" | ||
return self.additional_query_params | ||
|
||
def get_endpoint(self: ProviderDataIngester, foreign_identifier: str) -> str: | ||
"""Build the endpoint to request the data for the given foreign identifier""" | ||
return self.endpoint | ||
|
||
def get_record(self: ProviderDataIngester, foreign_identifier: str) -> dict | None: | ||
""" | ||
Retrieve an individual record's data from the provider. | ||
Analogous to `ProviderDataIngester::get_batch`. | ||
""" | ||
logger.info("Getting single record %s", foreign_identifier) | ||
endpoint = self.get_endpoint(foreign_identifier) | ||
query_params = self.get_query_params(foreign_identifier) | ||
return self.get_response_json(query_params, endpoint) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
catalog/dags/providers/provider_targeted_ingestion_dag_factory.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
""" | ||
# Provider Targeted Ingestion Workflow DAG Factory | ||
This file iterates over the configurations defined in PROVIDER_TARGETED_INGESTION_WORKFLOWS | ||
and generates a targeted ingestion workflow DAG for each. | ||
""" | ||
|
||
from providers.provider_dag_factory import create_targeted_ingestion_dag | ||
from providers.provider_targeted_ingestion_workflows import ( | ||
PROVIDER_TARGETED_INGESTION_WORKFLOWS, | ||
) | ||
|
||
|
||
for provider_workflow in PROVIDER_TARGETED_INGESTION_WORKFLOWS: | ||
globals()[provider_workflow.dag_id] = create_targeted_ingestion_dag( | ||
provider_workflow | ||
) |
42 changes: 42 additions & 0 deletions
42
catalog/dags/providers/provider_targeted_ingestion_workflows.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from dataclasses import dataclass | ||
|
||
from providers.provider_api_scripts.flickr import FlickrDataIngester | ||
from providers.provider_workflows import ProviderWorkflow | ||
|
||
|
||
@dataclass | ||
class ProviderTargetedIngestionWorkflow(ProviderWorkflow): | ||
""" | ||
Provider targeted ingestion workflow configurations. | ||
Extends the ProviderWorkflow with configuration options used to set up | ||
a targeted ingestion workflow DAG. | ||
""" | ||
|
||
max_foreign_identifiers: int = 0 | ||
""" | ||
The maximum foreign identifiers to allow for a single run of the DAG. | ||
This should limit the DAG run to less than the setting for ``pull_timeout``. | ||
""" | ||
|
||
schedule_string: None = None | ||
""" | ||
Targeted reingestion cannot happen on a schedule and must be triggered | ||
by another process. | ||
""" | ||
|
||
def __post_init__(self): | ||
if not self.dag_id: | ||
_, provider_name = self._get_module_info() | ||
self.dag_id = f"{provider_name}_targeted_ingestion_workflow" | ||
|
||
super().__post_init__() | ||
|
||
|
||
PROVIDER_TARGETED_INGESTION_WORKFLOWS = [ | ||
ProviderTargetedIngestionWorkflow( | ||
ingester_class=FlickrDataIngester, | ||
max_foreign_identifiers=2000, | ||
), | ||
] |