Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Improve minor aspects of the code (#296)
Dhruv Bhanushaliad2f74a111af
diff --git a/ingestion_server/README.md b/ingestion_server/README.mdindex df9489a3f..ac4dace5f 100644--- a/ingestion_server/README.md+++ b/ingestion_server/README.md@@ -4,7 +4,7 @@Ingestion Server is a small private API for copying data from an upstream source and loading it into the Openverse API. This is a two-step process:-1. The data is copied from the upstream CC Catalog database and into the downstream API database.+1. The data is copied from the upstream catalog database and into the downstream API database.2. Data from the downstream API database gets indexed in Elasticsearch.Performance is dependent on the size of the target Elasticsearch cluster, database throughput, and bandwidth available to the ingestion server. The primary bottleneck is indexing to Elasticsearch.diff --git a/ingestion_server/ingestion_server/api.py b/ingestion_server/ingestion_server/api.pyindex 7ba331218..f0fbe8de1 100644--- a/ingestion_server/ingestion_server/api.py+++ b/ingestion_server/ingestion_server/api.py@@ -1,3 +1,8 @@+"""+A small RPC API server for scheduling ingestion of upstream data and+Elasticsearch indexing tasks.+"""+import jsonimport loggingimport sys@@ -14,12 +19,6 @@ from ingestion_server.state import clear_state, worker_finishedfrom ingestion_server.tasks import Task, TaskTracker, TaskTypes-"""-A small RPC API server for scheduling ingestion of upstream data and-Elasticsearch indexing tasks.-"""--MODEL = "model"ACTION = "action"CALLBACK_URL = "callback_url"diff --git a/ingestion_server/ingestion_server/authority.py b/ingestion_server/ingestion_server/authority.pyindex c82f80750..7a8ca686c 100644--- a/ingestion_server/ingestion_server/authority.py+++ b/ingestion_server/ingestion_server/authority.py@@ -1,6 +1,3 @@-from enum import Enum, auto--"""Authority is a ranking from 0 to 100 (with 0 being least authoritative)indicating the pedigree of an image. Some examples of things that could impact@@ -22,6 +19,8 @@ Also note that this is just one factor in rankings, and the magnitude of theboost can be adjusted at search-time."""+from enum import Enum, auto+class AuthorityTypes(Enum):CURATED = auto()diff --git a/ingestion_server/ingestion_server/categorize.py b/ingestion_server/ingestion_server/categorize.pyindex ae2880de4..387612497 100644--- a/ingestion_server/ingestion_server/categorize.py+++ b/ingestion_server/ingestion_server/categorize.py@@ -1,6 +1,3 @@-from enum import Enum, auto--"""https://github.com/creativecommons/cccatalog-api/issues/340@@ -8,6 +5,8 @@ Attempt to figure out the image type (illustration, vector, photograph, ordigitized artwork) based on its source and file extension."""+from enum import Enum, auto+class Category(Enum):PHOTOGRAPH = auto()diff --git a/ingestion_server/ingestion_server/cleanup.py b/ingestion_server/ingestion_server/cleanup.pyindex f42550e58..751a67219 100644--- a/ingestion_server/ingestion_server/cleanup.py+++ b/ingestion_server/ingestion_server/cleanup.py@@ -1,3 +1,8 @@+"""+Functions for processing data when it is imported into the catalog. This+includes cleaning up malformed URLs and filtering out undesirable tags.+"""+import logging as logimport multiprocessingimport time@@ -11,11 +16,6 @@ from psycopg2.extras import DictCursor, Jsonfrom ingestion_server.indexer import DB_BUFFER_SIZE, database_connect-"""-Functions for processing data when it is imported into the CC Catalog. This-includes cleaning up malformed URLs and filtering out undesirable tags.-"""-# Number of records to buffer in memory at onceCLEANUP_BUFFER_SIZE = DB_BUFFER_SIZEdiff --git a/ingestion_server/ingestion_server/distributed_reindex_scheduler.py b/ingestion_server/ingestion_server/distributed_reindex_scheduler.pyindex 433f69cdd..0c2a9d3c4 100644--- a/ingestion_server/ingestion_server/distributed_reindex_scheduler.py+++ b/ingestion_server/ingestion_server/distributed_reindex_scheduler.py@@ -7,6 +7,7 @@ request.Once the reindexing job is finished, each worker will notify Ingestion Server,which should then shut down the instances."""+import logging as logimport mathimport socketdiff --git a/ingestion_server/ingestion_server/elasticsearch_models.py b/ingestion_server/ingestion_server/elasticsearch_models.pyindex 1be9cf51a..eb4fdea0e 100644--- a/ingestion_server/ingestion_server/elasticsearch_models.py+++ b/ingestion_server/ingestion_server/elasticsearch_models.py@@ -1,11 +1,3 @@-from enum import Enum, auto--from elasticsearch_dsl import Document, Field, Integer--from ingestion_server.authority import get_authority_boost-from ingestion_server.categorize import get_categories--"""Provides an ORM-like experience for accessing data in Elasticsearch.@@ -13,6 +5,13 @@ Note the actual schema for Elasticsearch is defined in es_mapping.py; anylow-level changes to the index must be represented there as well."""+from enum import Enum, auto++from elasticsearch_dsl import Document, Field, Integer++from ingestion_server.authority import get_authority_boost+from ingestion_server.categorize import get_categories+class RankFeature(Field):name = "rank_feature"diff --git a/ingestion_server/ingestion_server/indexer.py b/ingestion_server/ingestion_server/indexer.pyindex 0d6f06d46..fb898ea1f 100644--- a/ingestion_server/ingestion_server/indexer.py+++ b/ingestion_server/ingestion_server/indexer.py@@ -1,3 +1,20 @@+"""+A utility for indexing data to Elasticsearch. For each table to+sync, find its largest ID in database. Find the corresponding largest ID in+Elasticsearch. If the database ID is greater than the largest corresponding+ID in Elasticsearch, copy the missing records over to Elasticsearch.++Each table is database corresponds to an identically named index in+Elasticsearch. For instance, if database has a table that we would like to+replicate called 'image', the indexer will create an Elasticsearch called+'image' and populate the index with documents. See elasticsearch_models.py to+change the format of Elasticsearch documents.++This can either be run as a module, CLI, or in daemon mode. In daemon mode,+it will actively monitor Postgres for updates and index them automatically. This+is useful for local development environments.+"""+import argparseimport datetimeimport logging as log@@ -22,23 +39,6 @@ from ingestion_server.qa import create_search_qa_indexfrom ingestion_server.queries import get_existence_queries-"""-A utility for indexing data to Elasticsearch. For each table to-sync, find its largest ID in database. Find the corresponding largest ID in-Elasticsearch. If the database ID is greater than the largest corresponding-ID in Elasticsearch, copy the missing records over to Elasticsearch.--Each table is database corresponds to an identically named index in-Elasticsearch. For instance, if database has a table that we would like to-replicate called 'image', the indexer will create an Elasticsearch called-'image' and populate the index with documents. See elasticsearch_models.py to-change the format of Elasticsearch documents.--This can either be run as a module, CLI, or in daemon mode. In daemon mode,-it will actively monitor Postgres for updates and index them automatically. This-is useful for local development environments.-"""-# For AWS IAM access to ElasticsearchAWS_ACCESS_KEY_ID = config("AWS_ACCESS_KEY_ID", default="")AWS_SECRET_ACCESS_KEY = config("AWS_SECRET_ACCESS_KEY", default="")diff --git a/ingestion_server/ingestion_server/indexer_worker.py b/ingestion_server/ingestion_server/indexer_worker.pyindex 967aff4f7..027a46146 100644--- a/ingestion_server/ingestion_server/indexer_worker.py+++ b/ingestion_server/ingestion_server/indexer_worker.py@@ -5,6 +5,7 @@ database.Accept an HTTP request specifying a range of image IDs to reindex. After thedata has been indexed, notify Ingestion Server and stop the instance."""+import logging as logimport sysfrom multiprocessing import Process, Valuediff --git a/ingestion_server/ingestion_server/ingest.py b/ingestion_server/ingestion_server/ingest.pyindex 69b6c1574..a332236e5 100644--- a/ingestion_server/ingestion_server/ingest.py+++ b/ingestion_server/ingestion_server/ingest.py@@ -1,20 +1,3 @@-import datetime-import logging as log--import psycopg2-from decouple import config-from psycopg2.extras import DictCursor-from psycopg2.sql import SQL, Identifier, Literal--from ingestion_server.cleanup import clean_image_data-from ingestion_server.indexer import database_connect-from ingestion_server.queries import (- get_copy_data_query,- get_fdw_query,- get_go_live_query,-)--"""Pull the latest copy of a table from the upstream database (aka CC Catalog/theintermediary database).@@ -33,6 +16,23 @@ table to replace the old data. This strategy is far faster than updating thedata in place."""+import datetime+import logging as log++import psycopg2+from decouple import config+from psycopg2.extras import DictCursor+from psycopg2.sql import SQL, Identifier, Literal++from ingestion_server.cleanup import clean_image_data+from ingestion_server.indexer import database_connect+from ingestion_server.queries import (+ get_copy_data_query,+ get_fdw_query,+ get_go_live_query,+)++UPSTREAM_DB_HOST = config("UPSTREAM_DB_HOST", default="localhost")UPSTREAM_DB_PORT = config("UPSTREAM_DB_PORT", default=5433, cast=int)UPSTREAM_DB_USER = config("UPSTREAM_DB_USER", default="deploy")@@ -236,7 +236,7 @@ def _remap_constraint(name, con_table, fk_statement, table):def reload_upstream(table, progress=None, finish_time=None):"""- Import updates from the upstream CC Catalog database into the API. The+ Import updates from the upstream catalog database into the API. Theprocess involves the following steps.1. Get the list of overlapping columns: ``_get_shared_cols``diff --git a/ingestion_server/ingestion_server/state.py b/ingestion_server/ingestion_server/state.pyindex 8d7b25697..2059d78c7 100644--- a/ingestion_server/ingestion_server/state.py+++ b/ingestion_server/ingestion_server/state.py@@ -1,12 +1,3 @@-import datetime-import enum-import logging as log-import shelve--from decouple import config-from filelock import FileLock--"""Indexing is distributed across multiple independent hosts. We don't want to"go live" in production with the newly indexed data until all of the indexing@@ -17,6 +8,14 @@ State is persisted to the disk using shelve. Concurrent writes aren't allowed,so all operations need to acquire a lock."""+import datetime+import enum+import logging as log+import shelve++from decouple import config+from filelock import FileLock+lock_path = config("LOCK_PATH", default="lock")shelf_path = config("SHELF_PATH", default="db")diff --git a/ingestion_server/ingestion_server/tasks.py b/ingestion_server/ingestion_server/tasks.pyindex 5d6349c2a..9ee97a827 100644--- a/ingestion_server/ingestion_server/tasks.py+++ b/ingestion_server/ingestion_server/tasks.py@@ -1,3 +1,7 @@+"""+Simple in-memory tracking of executed tasks.+"""+import datetime as dtimport loggingfrom enum import Enum@@ -9,9 +13,6 @@ from ingestion_server.indexer import TableIndexer, elasticsearch_connectfrom ingestion_server.ingest import reload_upstream-""" Simple in-memory tracking of executed tasks. """--class TaskTypes(Enum):# Completely reindex all data for a given model.REINDEX = 0