Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Make Finnish DAG dated (#879)

Staci Cooper2040dc2bf135
diff --git a/DAGs.md b/DAGs.md
index f7887c2e5..60b4b3e99 100644
--- a/DAGs.md
+++ b/DAGs.md
@@ -67,7 +67,7 @@ The following are DAGs grouped by their primary tag:
| `brooklyn_museum_workflow` | `@monthly` | `False` | image |
| `cleveland_museum_workflow` | `@monthly` | `False` | image |
| [`europeana_workflow`](#europeana_workflow) | `@daily` | `True` | image |
-| `finnish_museums_workflow` | `@monthly` | `False` | image |
+| [`finnish_museums_workflow`](#finnish_museums_workflow) | `@daily` | `True` | image |
| [`flickr_workflow`](#flickr_workflow) | `@daily` | `True` | image |
| [`freesound_workflow`](#freesound_workflow) | `@monthly` | `False` | audio |
| [`inaturalist_workflow`](#inaturalist_workflow) | `@monthly` | `False` | image |
@@ -106,6 +106,7 @@ The following is documentation associated with each DAG (where available):
1. [`check_silenced_dags`](#check_silenced_dags)
1. [`europeana_reingestion_workflow`](#europeana_reingestion_workflow)
1. [`europeana_workflow`](#europeana_workflow)
+ 1. [`finnish_museums_workflow`](#finnish_museums_workflow)
1. [`flickr_reingestion_workflow`](#flickr_reingestion_workflow)
1. [`flickr_workflow`](#flickr_workflow)
1. [`freesound_workflow`](#freesound_workflow)
@@ -237,6 +238,25 @@ Output: TSV file containing the images and the
Notes: https://www.europeana.eu/api/v2/search.json
+## `finnish_museums_workflow`
+
+
+Content Provider: Finnish Museums
+
+ETL Process: Use the API to identify all CC licensed images.
+
+Output: TSV file containing the images and the
+ respective meta-data.
+
+Notes: https://api.finna.fi/swagger-ui/
+ https://www.finna.fi/Content/help-syntax?lng=en-gb
+ The Finnish Museums provider script is a dated DAG that
+ ingests all records that were last updated in the previous
+ day. Because of this, it is not necessary to run a separate
+ reingestion DAG, as updated data will be processed during
+ regular ingestion.
+
+
## `flickr_reingestion_workflow`
diff --git a/openverse_catalog/dags/providers/provider_api_scripts/finnish_museums.py b/openverse_catalog/dags/providers/provider_api_scripts/finnish_museums.py
index e40992c3b..84759bb90 100644
--- a/openverse_catalog/dags/providers/provider_api_scripts/finnish_museums.py
+++ b/openverse_catalog/dags/providers/provider_api_scripts/finnish_museums.py
@@ -1,4 +1,21 @@
+"""
+Content Provider: Finnish Museums
+
+ETL Process: Use the API to identify all CC licensed images.
+
+Output: TSV file containing the images and the
+ respective meta-data.
+
+Notes: https://api.finna.fi/swagger-ui/
+ https://www.finna.fi/Content/help-syntax?lng=en-gb
+ The Finnish Museums provider script is a dated DAG that
+ ingests all records that were last updated in the previous
+ day. Because of this, it is not necessary to run a separate
+ reingestion DAG, as updated data will be processed during
+ regular ingestion.
+"""
import logging
+from datetime import datetime, timedelta, timezone
from itertools import chain
from common.licenses import get_license_info
@@ -26,16 +43,71 @@ class FinnishMuseumsDataIngester(ProviderDataIngester):
delay = 5
format_type = "0/Image/"
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ # A flag that is turned on each time we start ingestion for a new set of
+ # query params. This is useful for logging information on only the first
+ # batch of each set.
+ self.new_iteration = True
+
+ # Build the list of timestamp pairs once, so they can be reused for each
+ # building queried.
+ self.timestamp_pairs = self._get_timestamp_query_params_list(self.date)
+
+ @staticmethod
+ def _get_timestamp_query_params_list(date):
+ """
+ The Finnish Museums API behaves unexpectedly when querying large datasets,
+ resulting in large numbers of duplicates and eventual DAG timeouts
+ (see https://github.com/WordPress/openverse-catalog/pull/879 for more
+ details). To avoid this, we build a list of timestamp pairs that divide
+ the ingestion date into equal portions of the 24-hour period, and run
+ ingestion separately for each time slice.
+ """
+ # Get the logical date for the DagRun
+ utc_date = datetime.strptime(date, "%Y-%m-%d").replace(tzinfo=timezone.utc)
+
+ seconds_in_a_day = 86400
+ number_of_divisions = 48 # Half-hour increments
+ portion = int(seconds_in_a_day / number_of_divisions)
+
+ def _format_timestamp(d):
+ return d.isoformat().replace("+00:00", "Z")
+
+ # Generate the start/end timestamps for each half-hour 'slice' of the day
+ pair_list = [
+ (
+ _format_timestamp(utc_date + timedelta(seconds=i * portion)),
+ _format_timestamp(utc_date + timedelta(seconds=(i + 1) * portion)),
+ )
+ for i in range(number_of_divisions)
+ ]
+ return pair_list
+
def ingest_records(self, **kwargs):
for building in BUILDINGS:
logger.info(f"Obtaining images of building {building}")
- super().ingest_records(building=building)
+
+ for start_ts, end_ts in self.timestamp_pairs:
+ self.new_iteration = True
+
+ logger.info(f"Ingesting data for start: {start_ts}, end: {end_ts}")
+ super().ingest_records(
+ building=building, start_ts=start_ts, end_ts=end_ts
+ )
def get_next_query_params(self, prev_query_params, **kwargs):
if not prev_query_params:
building = kwargs.get("building")
+ start_ts = kwargs.get("start_ts")
+ end_ts = kwargs.get("end_ts")
+
return {
- "filter[]": [f'format:"{self.format_type}"', f'building:"{building}"'],
+ "filter[]": [
+ f'format:"{self.format_type}"',
+ f'building:"{building}"',
+ f'last_indexed:"[{start_ts} TO {end_ts}]"',
+ ],
"limit": self.batch_limit,
"page": 1,
}
@@ -53,6 +125,10 @@ class FinnishMuseumsDataIngester(ProviderDataIngester):
):
return None
+ if self.new_iteration:
+ logger.info(f"Detected {response_json['resultCount']} total records.")
+ self.new_iteration = False
+
return response_json["records"]
def get_record_data(self, data):
@@ -112,7 +188,6 @@ class FinnishMuseumsDataIngester(ProviderDataIngester):
def main():
- logger.info("Begin: Finnish museum data ingestion")
ingester = FinnishMuseumsDataIngester()
ingester.ingest_records()
diff --git a/openverse_catalog/dags/providers/provider_workflows.py b/openverse_catalog/dags/providers/provider_workflows.py
index cb4c60457..3b95221e9 100644
--- a/openverse_catalog/dags/providers/provider_workflows.py
+++ b/openverse_catalog/dags/providers/provider_workflows.py
@@ -124,9 +124,10 @@ PROVIDER_WORKFLOWS = [
),
ProviderWorkflow(
ingester_class=FinnishMuseumsDataIngester,
- start_date=datetime(2020, 9, 1),
- pull_timeout=timedelta(days=5),
- load_timeout=timedelta(days=5),
+ start_date=datetime(2015, 11, 1),
+ load_timeout=timedelta(hours=5),
+ schedule_string="@daily",
+ dated=True,
),
ProviderWorkflow(
ingester_class=FlickrDataIngester,
diff --git a/tests/dags/providers/provider_api_scripts/test_finnish_museums.py b/tests/dags/providers/provider_api_scripts/test_finnish_museums.py
index 29074c450..56acdf5be 100644
--- a/tests/dags/providers/provider_api_scripts/test_finnish_museums.py
+++ b/tests/dags/providers/provider_api_scripts/test_finnish_museums.py
@@ -17,7 +17,8 @@ logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s: %(message)s", level=logging.DEBUG
)
-fm = FinnishMuseumsDataIngester()
+FROZEN_DATE = "2020-04-01"
+fm = FinnishMuseumsDataIngester(date=FROZEN_DATE)
image_store = ImageStore(provider=prov.FINNISH_DEFAULT_PROVIDER)
fm.media_stores = {"image": image_store}
@@ -28,10 +29,75 @@ def _get_resource_json(json_name):
return resource_json
+def test_get_timestamp_query_params_list():
+ actual_list = fm._get_timestamp_query_params_list(FROZEN_DATE)
+ # Expected timestamp list from splitting the FROZEN_DATE into 48 segments
+ expected_list = [
+ ("2020-04-01T00:00:00Z", "2020-04-01T00:30:00Z"),
+ ("2020-04-01T00:30:00Z", "2020-04-01T01:00:00Z"),
+ ("2020-04-01T01:00:00Z", "2020-04-01T01:30:00Z"),
+ ("2020-04-01T01:30:00Z", "2020-04-01T02:00:00Z"),
+ ("2020-04-01T02:00:00Z", "2020-04-01T02:30:00Z"),
+ ("2020-04-01T02:30:00Z", "2020-04-01T03:00:00Z"),
+ ("2020-04-01T03:00:00Z", "2020-04-01T03:30:00Z"),
+ ("2020-04-01T03:30:00Z", "2020-04-01T04:00:00Z"),
+ ("2020-04-01T04:00:00Z", "2020-04-01T04:30:00Z"),
+ ("2020-04-01T04:30:00Z", "2020-04-01T05:00:00Z"),
+ ("2020-04-01T05:00:00Z", "2020-04-01T05:30:00Z"),
+ ("2020-04-01T05:30:00Z", "2020-04-01T06:00:00Z"),
+ ("2020-04-01T06:00:00Z", "2020-04-01T06:30:00Z"),
+ ("2020-04-01T06:30:00Z", "2020-04-01T07:00:00Z"),
+ ("2020-04-01T07:00:00Z", "2020-04-01T07:30:00Z"),
+ ("2020-04-01T07:30:00Z", "2020-04-01T08:00:00Z"),
+ ("2020-04-01T08:00:00Z", "2020-04-01T08:30:00Z"),
+ ("2020-04-01T08:30:00Z", "2020-04-01T09:00:00Z"),
+ ("2020-04-01T09:00:00Z", "2020-04-01T09:30:00Z"),
+ ("2020-04-01T09:30:00Z", "2020-04-01T10:00:00Z"),
+ ("2020-04-01T10:00:00Z", "2020-04-01T10:30:00Z"),
+ ("2020-04-01T10:30:00Z", "2020-04-01T11:00:00Z"),
+ ("2020-04-01T11:00:00Z", "2020-04-01T11:30:00Z"),
+ ("2020-04-01T11:30:00Z", "2020-04-01T12:00:00Z"),
+ ("2020-04-01T12:00:00Z", "2020-04-01T12:30:00Z"),
+ ("2020-04-01T12:30:00Z", "2020-04-01T13:00:00Z"),
+ ("2020-04-01T13:00:00Z", "2020-04-01T13:30:00Z"),
+ ("2020-04-01T13:30:00Z", "2020-04-01T14:00:00Z"),
+ ("2020-04-01T14:00:00Z", "2020-04-01T14:30:00Z"),
+ ("2020-04-01T14:30:00Z", "2020-04-01T15:00:00Z"),
+ ("2020-04-01T15:00:00Z", "2020-04-01T15:30:00Z"),
+ ("2020-04-01T15:30:00Z", "2020-04-01T16:00:00Z"),
+ ("2020-04-01T16:00:00Z", "2020-04-01T16:30:00Z"),
+ ("2020-04-01T16:30:00Z", "2020-04-01T17:00:00Z"),
+ ("2020-04-01T17:00:00Z", "2020-04-01T17:30:00Z"),
+ ("2020-04-01T17:30:00Z", "2020-04-01T18:00:00Z"),
+ ("2020-04-01T18:00:00Z", "2020-04-01T18:30:00Z"),
+ ("2020-04-01T18:30:00Z", "2020-04-01T19:00:00Z"),
+ ("2020-04-01T19:00:00Z", "2020-04-01T19:30:00Z"),
+ ("2020-04-01T19:30:00Z", "2020-04-01T20:00:00Z"),
+ ("2020-04-01T20:00:00Z", "2020-04-01T20:30:00Z"),
+ ("2020-04-01T20:30:00Z", "2020-04-01T21:00:00Z"),
+ ("2020-04-01T21:00:00Z", "2020-04-01T21:30:00Z"),
+ ("2020-04-01T21:30:00Z", "2020-04-01T22:00:00Z"),
+ ("2020-04-01T22:00:00Z", "2020-04-01T22:30:00Z"),
+ ("2020-04-01T22:30:00Z", "2020-04-01T23:00:00Z"),
+ ("2020-04-01T23:00:00Z", "2020-04-01T23:30:00Z"),
+ ("2020-04-01T23:30:00Z", "2020-04-02T00:00:00Z"),
+ ]
+ assert actual_list == expected_list
+
+
def test_build_query_param_default():
- actual_param_made = fm.get_next_query_params(None, building="0/Museovirasto/")
+ actual_param_made = fm.get_next_query_params(
+ None,
+ building="0/Museovirasto/",
+ start_ts="2020-04-01T00:30:00Z",
+ end_ts="2020-04-01T01:00:00Z",
+ )
expected_param = {
- "filter[]": ['format:"0/Image/"', 'building:"0/Museovirasto/"'],
+ "filter[]": [
+ 'format:"0/Image/"',
+ 'building:"0/Museovirasto/"',
+ 'last_indexed:"[2020-04-01T00:30:00Z TO 2020-04-01T01:00:00Z]"',
+ ],
"limit": 100,
"page": 1,
}