Skip to content

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

Add DAG to trim and deduplicate tags (#4429)

sarayourfriend4f0e43a26fcb
diff --git a/catalog/dags/maintenance/trim_and_deduplicate_tags.py b/catalog/dags/maintenance/trim_and_deduplicate_tags.py
new file mode 100644
index 000000000..5eee080c5
--- /dev/null
+++ b/catalog/dags/maintenance/trim_and_deduplicate_tags.py
@@ -0,0 +1,88 @@
+"""
+See the issue for context and motivation: https://github.com/WordPress/openverse/issues/4199
+
+This DAG triggers a run of the batched update DAG. It generates a new list of tags by
+trimming all existing tags and re-inserting only the distinct tags of the resulting list of tags.
+"""
+
+from datetime import datetime, timedelta
+from textwrap import dedent
+
+from airflow.decorators import dag
+from airflow.operators.trigger_dagrun import TriggerDagRunOperator
+
+from common.constants import DAG_DEFAULT_ARGS, MEDIA_TYPES
+from database.batched_update.constants import DAG_ID as BATCHED_UPDATE_DAG_ID
+
+
+DAG_ID = "trim_and_deduplicate_tags"
+
+
+@dag(
+ dag_id=DAG_ID,
+ schedule=None,
+ start_date=datetime(2024, 6, 3),
+ tags=["database"],
+ doc_md=__doc__,
+ max_active_runs=1,
+ default_args=DAG_DEFAULT_ARGS,
+)
+def trim_and_deduplicate_tags():
+ TriggerDagRunOperator.partial(
+ task_id=DAG_ID,
+ trigger_dag_id=BATCHED_UPDATE_DAG_ID,
+ wait_for_completion=True,
+ execution_timeout=timedelta(hours=5),
+ max_active_tis_per_dag=2,
+ map_index_template="""{{ task.conf['table_name'] }}""",
+ retries=0,
+ ).expand(
+ conf=[
+ {
+ "query_id": f"{DAG_ID}_{media_type}",
+ "table_name": media_type,
+ "select_query": (
+ "WHERE "
+ + (
+ ""
+ if media_type == "audio"
+ # We've identified these image providers in particular definitely have tags that need trimming, so we can narrow the query starting there at least
+ else (
+ "provider IN ('flickr', 'geographorguk', 'thingiverse', 'animaldiversity', 'clevelandmuseum', 'digitaltmuseum') AND "
+ )
+ )
+ + (
+ f"""EXISTS (SELECT 1 FROM jsonb_array_elements({media_type}.tags) AS t WHERE t->>'name' ILIKE ' %' OR t->>'name' ILIKE '% ')"""
+ )
+ ),
+ "update_query": (
+ "SET updated_on = now(), "
+ + dedent(
+ f"""
+ tags = (
+ SELECT
+ jsonb_agg(
+ jsonb_set(
+ deduped.tag,
+ '{{name}}',
+ to_jsonb(deduped.trimmed_name)
+ )
+ )
+ FROM (
+ SELECT DISTINCT ON (tag->>'name', tag->'provider')
+ trim(tag->>'name') trimmed_name,
+ tag
+ FROM jsonb_array_elements({media_type}.tags || '[]'::jsonb) tag
+ ) deduped
+ )
+ """
+ )
+ ),
+ "dry_run": False,
+ }
+ for media_type in MEDIA_TYPES
+ ]
+ )
+
+
+trim_and_deduplicate_tags()
diff --git a/documentation/catalog/reference/DAGs.md b/documentation/catalog/reference/DAGs.md
index f6b934ead..46a21ce67 100644
--- a/documentation/catalog/reference/DAGs.md
+++ b/documentation/catalog/reference/DAGs.md
@@ -54,6 +54,7 @@ The following are DAGs grouped by their primary tag:
| [`recreate_image_popularity_calculation`](#recreate_media_type_popularity_calculation) | `None` |
| [`report_pending_reported_media`](#report_pending_reported_media) | `@weekly` |
| [`staging_database_restore`](#staging_database_restore) | `@monthly` |
+| [`trim_and_deduplicate_tags`](#trim_and_deduplicate_tags) | `None` |
### Elasticsearch
@@ -174,6 +175,7 @@ The following is documentation associated with each DAG (where available):
1. [`smk_workflow`](#smk_workflow)
1. [`staging_database_restore`](#staging_database_restore)
1. [`stocksnap_workflow`](#stocksnap_workflow)
+1. [`trim_and_deduplicate_tags`](#trim_and_deduplicate_tags)
1. [`wikimedia_commons_workflow`](#wikimedia_commons_workflow)
1. [`wikimedia_reingestion_workflow`](#wikimedia_commons_workflow)
1. [`wordpress_workflow`](#wordpress_workflow)
@@ -1068,6 +1070,17 @@ authorization required. API is undocumented.
----
+### `trim_and_deduplicate_tags`
+
+See the issue for context and motivation:
+https://github.com/WordPress/openverse/issues/4199
+
+This DAG triggers a run of the batched update DAG. It generates a new list of
+tags by trimming all existing tags and re-inserting only the distinct tags of
+the resulting list of tags.
+
+----
+
### `wikimedia_commons_workflow`
**Content Provider:** Wikimedia Commons