Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Retire image expiration dag (#1093)
Ashwanthram K Ld50d44b660ea
diff --git a/DAGs.md b/DAGs.mdindex 43b203acd..0f8afaa43 100644--- a/DAGs.md+++ b/DAGs.md@@ -39,7 +39,6 @@ The following are DAGs grouped by their primary tag:| DAG ID | Schedule Interval || --------------------------------------------------------------------------------- | ----------------- |-| `image_expiration_workflow` | `None` || [`recreate_audio_popularity_calculation`](#recreate_audio_popularity_calculation) | `None` || [`recreate_image_popularity_calculation`](#recreate_image_popularity_calculation) | `None` || [`report_pending_reported_media`](#report_pending_reported_media) | `@weekly` |diff --git a/openverse_catalog/dags/common/loader/sql.py b/openverse_catalog/dags/common/loader/sql.pyindex 1773ca844..386f27f8b 100644--- a/openverse_catalog/dags/common/loader/sql.py+++ b/openverse_catalog/dags/common/loader/sql.py@@ -370,57 +370,3 @@ def _delete_malformed_row_in_file(tsv_file_name, line_number):for index, line in enumerate(lines):if index + 1 != line_number:write_obj.write(line)---def expire_old_images(- postgres_conn_id,- provider,- image_table=TABLE_NAMES[IMAGE],- task: AbstractOperator = None,-):- postgres = PostgresHook(- postgres_conn_id=postgres_conn_id,- default_statement_timeout=PostgresHook.get_execution_timeout(task),- )-- if provider not in OLDEST_PER_PROVIDER:- raise Exception(- f"Provider value {provider} not defined in the "- f"OLDEST_PER_PROVIDER dictionary"- )-- """- Select all records that are outdated- """- select_query = dedent(- f"""- SELECT {col.FOREIGN_ID.db_name}- FROM {image_table}- WHERE- {col.PROVIDER.db_name} = '{provider}'- AND- {col.UPDATED_ON.db_name} < {NOW} - INTERVAL '{OLDEST_PER_PROVIDER[provider]}';- """- )-- selected_records = postgres.get_records(select_query)-- """- Set the 'removed_from_source' value of each selected row to True to- indicate that those images are outdated- """- for row in selected_records:- foreign_id = row[0]-- postgres.run(- dedent(- f"""- UPDATE {image_table}- SET {col.REMOVED.db_name} = 't'- WHERE- {image_table}.{col.PROVIDER.db_name} = '{provider}'- AND- MD5({image_table}.{col.FOREIGN_ID.db_name}) = MD5('{foreign_id}');- """- )- )diff --git a/openverse_catalog/dags/retired/common/loader/sql.py b/openverse_catalog/dags/retired/common/loader/sql.pynew file mode 100644index 000000000..a89fe5f68--- /dev/null+++ b/openverse_catalog/dags/retired/common/loader/sql.py@@ -0,0 +1,52 @@+def expire_old_images(+ postgres_conn_id,+ provider,+ image_table=TABLE_NAMES[IMAGE],+ task: AbstractOperator = None,+):+ postgres = PostgresHook(+ postgres_conn_id=postgres_conn_id,+ default_statement_timeout=PostgresHook.get_execution_timeout(task),+ )++ if provider not in OLDEST_PER_PROVIDER:+ raise Exception(+ f"Provider value {provider} not defined in the "+ f"OLDEST_PER_PROVIDER dictionary"+ )++ """+ Select all records that are outdated+ """+ select_query = dedent(+ f"""+ SELECT {col.FOREIGN_ID.db_name}+ FROM {image_table}+ WHERE+ {col.PROVIDER.db_name} = '{provider}'+ AND+ {col.UPDATED_ON.db_name} < {NOW} - INTERVAL '{OLDEST_PER_PROVIDER[provider]}';+ """+ )++ selected_records = postgres.get_records(select_query)++ """+ Set the 'removed_from_source' value of each selected row to True to+ indicate that those images are outdated+ """+ for row in selected_records:+ foreign_id = row[0]++ postgres.run(+ dedent(+ f"""+ UPDATE {image_table}+ SET {col.REMOVED.db_name} = 't'+ WHERE+ {image_table}.{col.PROVIDER.db_name} = '{provider}'+ AND+ MD5({image_table}.{col.FOREIGN_ID.db_name}) = MD5('{foreign_id}');+ """+ )+ )diff --git a/openverse_catalog/dags/database/image_expiration_workflow.py b/openverse_catalog/dags/retired/database/image_expiration_workflow.pysimilarity index 100%rename from openverse_catalog/dags/database/image_expiration_workflow.pyrename to openverse_catalog/dags/retired/database/image_expiration_workflow.pydiff --git a/tests/dags/common/loader/test_sql.py b/tests/dags/common/loader/test_sql.pyindex 74c690655..54fc6753c 100644--- a/tests/dags/common/loader/test_sql.py+++ b/tests/dags/common/loader/test_sql.py@@ -1432,85 +1432,6 @@ def test_drop_load_table_drops_table(postgres_with_load_table, load_table, identassert not check_result-def test_image_expiration(- postgres_with_load_and_image_table,- load_table,- image_table,- identifier,- mock_pg_hook_task,-):- postgres_conn_id = POSTGRES_CONN_ID-- FID_A = "a"- FID_B = "b"- IMG_URL_A = "https://images.com/a/img.jpg"- IMG_URL_B = "https://images.com/b/img.jpg"- PROVIDER_A = "smithsonian"- PROVIDER_B = "flickr"- LICENSE = "by-nc-nd"-- query_values = [- create_query_values(- {- col.FOREIGN_ID.db_name: FID_A,- col.DIRECT_URL.db_name: IMG_URL_A,- col.LICENSE.db_name: LICENSE,- col.PROVIDER.db_name: PROVIDER_A,- col.SOURCE.db_name: PROVIDER_A,- }- ),- create_query_values(- {- col.FOREIGN_ID.db_name: FID_B,- col.DIRECT_URL.db_name: IMG_URL_B,- col.LICENSE.db_name: LICENSE,- col.PROVIDER.db_name: PROVIDER_B,- col.SOURCE.db_name: PROVIDER_B,- }- ),- ]- insert_data_query = f"""INSERT INTO {load_table} VALUES- ({query_values[0]}),- ({query_values[1]}- );"""-- postgres_with_load_and_image_table.cursor.execute(insert_data_query)- postgres_with_load_and_image_table.connection.commit()- sql.upsert_records_to_db_table(- postgres_conn_id, identifier, db_table=image_table, task=mock_pg_hook_task- )- postgres_with_load_and_image_table.connection.commit()- postgres_with_load_and_image_table.cursor.execute(f"DELETE FROM {load_table};")- postgres_with_load_and_image_table.connection.commit()-- postgres_with_load_and_image_table.cursor.execute(- f"UPDATE {image_table} SET updated_on = NOW() - INTERVAL '1 year' "- f"WHERE provider = 'flickr';"- )-- postgres_with_load_and_image_table.connection.commit()-- sql.expire_old_images(- postgres_conn_id, PROVIDER_A, image_table=image_table, task=mock_pg_hook_task- )-- sql.expire_old_images(- postgres_conn_id, PROVIDER_B, image_table=image_table, task=mock_pg_hook_task- )-- postgres_with_load_and_image_table.connection.commit()-- postgres_with_load_and_image_table.cursor.execute(f"SELECT * FROM {image_table};")- actual_rows = postgres_with_load_and_image_table.cursor.fetchall()- assert len(actual_rows) == 2-- for actual_row in actual_rows:- if actual_row[fid_idx] == "a":- assert not actual_row[removed_idx]- else:- assert actual_row[fid_idx] == "b" and actual_row[removed_idx]--@pytest.mark.parametrize("value, expected",[