Skip to content

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

Improved data refresh status reporting (#744)

Madison Swain-Bowdend4dbf4d0617a
diff --git a/openverse_catalog/dags/data_refresh/dag_factory.py b/openverse_catalog/dags/data_refresh/dag_factory.py
index ec1d0710a..91c9a71c0 100644
--- a/openverse_catalog/dags/data_refresh/dag_factory.py
+++ b/openverse_catalog/dags/data_refresh/dag_factory.py
@@ -48,7 +48,6 @@ from common.constants import (
from common.operators.postgres_result import PostgresResultOperator
from data_refresh.data_refresh_task_factory import create_data_refresh_task_group
from data_refresh.data_refresh_types import DATA_REFRESH_CONFIGS, DataRefresh
-from data_refresh.record_reporting import report_record_difference
from data_refresh.refresh_popularity_metrics_task_factory import (
GROUP_ID as REFRESH_POPULARITY_METRICS_GROUP_ID,
)
@@ -60,6 +59,7 @@ from data_refresh.refresh_view_data_task_factory import (
UPDATE_DB_VIEW_TASK_ID,
create_refresh_view_data_task,
)
+from data_refresh.reporting import report_record_difference, report_status
logger = logging.getLogger(__name__)
@@ -135,6 +135,22 @@ def _month_check(dag_id: str, session: SASession = None) -> str:
)
+def _month_check_with_reporting(dag_id: str, media_type: str) -> str:
+ """
+ Wrapper for the monthly check function to report which step is starting
+ and which step is next to slack.
+ """
+ next_task_id = _month_check(dag_id)
+ next_step = {
+ REFRESH_POPULARITY_METRICS_TASK_ID: "update popularity metrics",
+ REFRESH_MATERIALIZED_VIEW_TASK_ID: "refresh matview",
+ }.get(next_task_id, "unable to determine next step")
+ message = f":horse_racing: Starting data refresh | _Next: {next_step}_"
+ report_status(media_type, message, dag_id)
+
+ return next_task_id
+
+
def create_data_refresh_dag(data_refresh: DataRefresh, external_dag_ids: Sequence[str]):
"""
This factory method instantiates a DAG that will run the popularity calculation and
@@ -182,9 +198,10 @@ def create_data_refresh_dag(data_refresh: DataRefresh, external_dag_ids: Sequenc
# Check if this is the first DagRun of the month for this DAG.
month_check = BranchPythonOperator(
task_id="month_check",
- python_callable=_month_check,
+ python_callable=_month_check_with_reporting,
op_kwargs={
"dag_id": data_refresh.dag_id,
+ "media_type": data_refresh.media_type,
},
)
diff --git a/openverse_catalog/dags/data_refresh/refresh_popularity_metrics_task_factory.py b/openverse_catalog/dags/data_refresh/refresh_popularity_metrics_task_factory.py
index 9554345bd..35844a04c 100644
--- a/openverse_catalog/dags/data_refresh/refresh_popularity_metrics_task_factory.py
+++ b/openverse_catalog/dags/data_refresh/refresh_popularity_metrics_task_factory.py
@@ -12,6 +12,7 @@ from airflow.operators.python import PythonOperator
from airflow.utils.task_group import TaskGroup
from common.constants import POSTGRES_CONN_ID
from common.popularity import sql
+from data_refresh import reporting
from data_refresh.data_refresh_types import DataRefresh
@@ -24,7 +25,8 @@ def create_refresh_popularity_metrics_task_group(data_refresh: DataRefresh):
"""
This factory method instantiates a TaskGroup that will update the popularity
DB tables for the given media type, including percentiles and popularity
- metrics.
+ metrics. It also creates a reporting tasks which will report the status of the
+ various steps once they complete.
Required Arguments:
@@ -45,6 +47,17 @@ def create_refresh_popularity_metrics_task_group(data_refresh: DataRefresh):
),
)
+ update_metrics_status = PythonOperator(
+ task_id=f"report_{UPDATE_MEDIA_POPULARITY_METRICS_TASK_ID}_status",
+ python_callable=reporting.report_status,
+ op_kwargs={
+ "media_type": media_type,
+ "dag_id": data_refresh.dag_id,
+ "message": "Popularity metrics update complete | "
+ "_Next: popularity constants view update_",
+ },
+ )
+
update_constants = PythonOperator(
task_id=UPDATE_MEDIA_POPULARITY_CONSTANTS_TASK_ID,
python_callable=sql.update_media_popularity_constants,
@@ -56,6 +69,18 @@ def create_refresh_popularity_metrics_task_group(data_refresh: DataRefresh):
),
)
- update_metrics >> update_constants
+ update_constants_status = PythonOperator(
+ task_id=f"report_{UPDATE_MEDIA_POPULARITY_CONSTANTS_TASK_ID}_status",
+ python_callable=reporting.report_status,
+ op_kwargs={
+ "media_type": media_type,
+ "dag_id": data_refresh.dag_id,
+ "message": "Popularity constants view update complete | "
+ "_Next: refresh matview_",
+ },
+ )
+
+ update_metrics >> [update_constants, update_metrics_status]
+ update_constants >> update_constants_status
return refresh_all_popularity_data
diff --git a/openverse_catalog/dags/data_refresh/refresh_view_data_task_factory.py b/openverse_catalog/dags/data_refresh/refresh_view_data_task_factory.py
index a6ed03e54..e0b582f1e 100644
--- a/openverse_catalog/dags/data_refresh/refresh_view_data_task_factory.py
+++ b/openverse_catalog/dags/data_refresh/refresh_view_data_task_factory.py
@@ -14,6 +14,7 @@ from airflow.operators.python import PythonOperator
from airflow.utils.trigger_rule import TriggerRule
from common.constants import POSTGRES_CONN_ID
from common.popularity import sql
+from data_refresh import reporting
from data_refresh.data_refresh_types import DataRefresh
@@ -25,7 +26,8 @@ def create_refresh_view_data_task(data_refresh: DataRefresh):
The task refreshes the materialized view for the given media type. The view collates
popularity data for each record. Refreshing has the effect of adding popularity data
for records that were ingested since the last time the view was refreshed, and
- updating popularity data for existing records.
+ updating popularity data for existing records. It also creates a reporting task
+ which will report the status of the matview refresh once it is complete.
Required Arguments:
@@ -40,4 +42,17 @@ def create_refresh_view_data_task(data_refresh: DataRefresh):
doc_md=create_refresh_view_data_task.__doc__,
)
+ refresh_status = PythonOperator(
+ task_id=f"report_{UPDATE_DB_VIEW_TASK_ID}_status",
+ python_callable=reporting.report_status,
+ op_kwargs={
+ "media_type": data_refresh.media_type,
+ "dag_id": data_refresh.dag_id,
+ "message": "Matview refresh complete | "
+ "_Next: ingestion server data refresh_",
+ },
+ )
+
+ refresh_matview >> refresh_status
+
return refresh_matview
diff --git a/openverse_catalog/dags/data_refresh/record_reporting.py b/openverse_catalog/dags/data_refresh/reporting.py
similarity index 72%
rename from openverse_catalog/dags/data_refresh/record_reporting.py
rename to openverse_catalog/dags/data_refresh/reporting.py
index 992a68ede..be3086d79 100644
--- a/openverse_catalog/dags/data_refresh/record_reporting.py
+++ b/openverse_catalog/dags/data_refresh/reporting.py
@@ -6,6 +6,17 @@ from common import slack
log = logging.getLogger(__name__)
+def report_status(media_type: str, message: str, dag_id: str):
+ message = f"`{media_type}`: {message}"
+ slack.send_message(
+ text=message,
+ dag_id=dag_id,
+ username="Data Refresh Notification",
+ icon_emoji="arrows_counterclockwise",
+ )
+ return message
+
+
def report_record_difference(before: str, after: str, media_type: str, dag_id: str):
before = int(before)
after = int(after)
diff --git a/tests/dags/data_refresh/test_dag_factory.py b/tests/dags/data_refresh/test_dag_factory.py
index eba1e2f42..5ba34fe0f 100644
--- a/tests/dags/data_refresh/test_dag_factory.py
+++ b/tests/dags/data_refresh/test_dag_factory.py
@@ -1,3 +1,5 @@
+from unittest import mock
+
import pytest
from airflow.models import DagRun
from airflow.models.dag import DAG
@@ -117,3 +119,26 @@ def test_month_check_ignores_failed_dagruns():
# successful run was last month, so we should refresh metrics.
next_task_id = dag_factory._month_check(TEST_DAG.dag_id)
assert next_task_id == REFRESH_POPULARITY_METRICS_TASK_ID
+
+
+ "task_id, expected_message",
+ [
+ (REFRESH_POPULARITY_METRICS_TASK_ID, "update popularity metrics"),
+ (REFRESH_MATERIALIZED_VIEW_TASK_ID, "refresh matview"),
+ ("Some other task ID", "unable to determine next step"),
+ ],
+)
+def test_month_check_with_reporting(task_id, expected_message):
+ with (
+ mock.patch("data_refresh.dag_factory._month_check") as mock_monthly_check,
+ mock.patch("data_refresh.dag_factory.report_status") as mock_report_status,
+ ):
+ mock_monthly_check.return_value = task_id
+ actual_task_id = dag_factory._month_check_with_reporting(
+ dag_id="fake_dag", media_type="image"
+ )
+ message = mock_report_status.call_args.args[1]
+ # Task ID returned from wrapper should be the same
+ assert actual_task_id == task_id
+ assert expected_message in message
diff --git a/tests/dags/data_refresh/test_record_reporting.py b/tests/dags/data_refresh/test_reporting.py
similarity index 74%
rename from tests/dags/data_refresh/test_record_reporting.py
rename to tests/dags/data_refresh/test_reporting.py
index aeba4e38f..d5c1cfeec 100644
--- a/tests/dags/data_refresh/test_record_reporting.py
+++ b/tests/dags/data_refresh/test_reporting.py
@@ -1,5 +1,5 @@
import pytest
-from data_refresh.record_reporting import report_record_difference
+from data_refresh.reporting import report_record_difference, report_status
@pytest.mark.parametrize(
@@ -19,3 +19,8 @@ def test_record_reporting(before, after, expected_in_message):
)
for expected in expected_in_message:
assert expected in actual
+
+
+def test_report_status():
+ actual = report_status("image", "This is my message", "sample_dag_id")
+ assert actual == "`image`: This is my message"