Skip to content

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

Add logging to iNaturalist date check (#1035)

rwidom370b916f948d
diff --git a/openverse_catalog/dags/providers/provider_api_scripts/inaturalist.py b/openverse_catalog/dags/providers/provider_api_scripts/inaturalist.py
index e94d282d9..5295303f4 100644
--- a/openverse_catalog/dags/providers/provider_api_scripts/inaturalist.py
+++ b/openverse_catalog/dags/providers/provider_api_scripts/inaturalist.py
@@ -189,6 +189,7 @@ class INaturalistDataIngester(ProviderDataIngester):
):
# if it was never run, assume the data is new
if last_success is None:
+ logger.info("No last success date, assuming iNaturalist data is new.")
return
s3 = S3Hook(aws_conn_id=aws_conn_id)
s3_client = s3.get_client_type()
@@ -198,11 +199,22 @@ class INaturalistDataIngester(ProviderDataIngester):
last_modified = s3_client.head_object(
Bucket="inaturalist-open-data", Key=key
)["LastModified"]
+ logger.info(
+ f"{key} was last modified on s3 on "
+ f"{last_modified.strftime('%Y-%m-%d %H:%M:%S')}."
+ )
# if any file has been updated, let's pull them all
if last_success < last_modified:
+ logger.info(
+ f"{key} was updated on s3 since the last dag run on "
+ f"{last_success.to_datetime_string()}."
+ )
return
# If no files have been updated, skip the DAG
- raise AirflowSkipException("Nothing new to ingest")
+ raise AirflowSkipException(
+ "Nothing new to ingest since last successful dag run on "
+ f"{last_success.to_datetime_string()}."
+ )
@staticmethod
def load_catalog_of_life_names(task: PythonOperator, remove_api_files: bool):
@@ -286,6 +298,8 @@ class INaturalistDataIngester(ProviderDataIngester):
check_for_file_updates = PythonOperator(
task_id="check_for_file_updates",
python_callable=INaturalistDataIngester.compare_update_dates,
+ # Pass in the start date of the prior successful dag run
+ # https://airflow.apache.org/docs/apache-airflow/stable/templates-ref.html
op_kwargs={
"last_success": "{{ prev_start_date_success }}",
"s3_keys": [
diff --git a/tests/dags/providers/provider_api_scripts/test_inaturalist.py b/tests/dags/providers/provider_api_scripts/test_inaturalist.py
index 45fdbd28d..a20a5a46a 100644
--- a/tests/dags/providers/provider_api_scripts/test_inaturalist.py
+++ b/tests/dags/providers/provider_api_scripts/test_inaturalist.py
@@ -2,7 +2,9 @@ from ast import literal_eval
from pathlib import Path
from unittest import mock
+import pendulum
import pytest
+from airflow.exceptions import AirflowSkipException
from airflow.models import TaskInstance
from common.constants import IMAGE
from common.loader.reporting import RecordMetrics
@@ -172,3 +174,59 @@ def test_get_batches(batch_length, max_id, expected):
with mock.patch.object(PostgresHook, "get_records", return_value=max_id):
actual = INAT.get_batches(batch_length, task)
assert actual == expected
+
+
+LAST_SUCCESS = pendulum.datetime(2023, 2, 15, tz="UTC")
+OLD = pendulum.datetime(2023, 1, 27, tz="UTC")
+NEW = pendulum.datetime(2023, 2, 27, tz="UTC")
+
+
+ "last_success, s3_dir, expected_msgs",
+ [
+ pytest.param(
+ None,
+ {"file1": OLD},
+ ["No last success date, assuming iNaturalist data is new."],
+ id="no_prior_run",
+ ),
+ pytest.param(
+ LAST_SUCCESS,
+ {"file1": OLD, "file2": OLD},
+ [
+ "file1 was last modified on s3 on 2023-01-27 00:00:00.",
+ "file2 was last modified on s3 on 2023-01-27 00:00:00.",
+ "Nothing new to ingest since last successful dag run on 2023-02-15 00:00:00.", # noqa
+ ],
+ id="old_files",
+ marks=pytest.mark.raises(exception=AirflowSkipException),
+ ),
+ pytest.param(
+ LAST_SUCCESS,
+ {"file1": NEW, "file2": NEW},
+ [
+ "file1 was last modified on s3 on 2023-02-27 00:00:00.",
+ "file1 was updated on s3 since the last dag run on 2023-02-15 00:00:00.", # noqa
+ ],
+ id="new_files",
+ ),
+ pytest.param(
+ LAST_SUCCESS,
+ {"file1": OLD, "file2": NEW, "file3": OLD, "file4": NEW},
+ [
+ "file1 was last modified on s3 on 2023-01-27 00:00:00.",
+ "file2 was last modified on s3 on 2023-02-27 00:00:00.",
+ "file2 was updated on s3 since the last dag run on 2023-02-15 00:00:00.", # noqa
+ ],
+ id="mixed_files",
+ ),
+ ],
+)
+def test_compare_update_dates(last_success, s3_dir, expected_msgs, caplog):
+ with mock.patch("providers.provider_api_scripts.inaturalist.S3Hook") as s3_hook:
+ s3_client = s3_hook.return_value.get_client_type.return_value
+ s3_client.head_object = lambda Bucket, Key: {"LastModified": s3_dir[Key]}
+ actual = INAT.compare_update_dates(last_success, s3_dir.keys())
+ assert actual is None
+ for msg in expected_msgs:
+ assert msg in caplog.text