This repo contains a docker-compose.yml file which configures the containers and their interaction.
To run the containers:
- users and passwords (adjust env variables as needed and set new passwords):
Optionally, add the following env variables for postgres and/or OpenSearch (not needed for local dev):
cp env.template .env
POSTGRES_ADDRESS_DOCKER(default "postgres", docker network internal) andPOSTGRES_PORT_DOCKER(default 5432)POSTGRES_ADDRESS_HOST(default "127.0.0.1", host) andPOSTGRES_PORT_HOST(default 5432)OPENSEARCH_ADDRESS_DOCKER(default "opensearch", docker network internal) andOPENSEARCH_PORT_DOCKER(default 9200)OPENSEARCH_ADDRESS_HOST(default "127.0.0.1", host) andOPENSEARCH_PORT_HOST(default 9200)FASTAPI_ADDRESS(default "127.0.0.1") andFASTAPI_PORT(default 8080)
- API keys for search API server:
cp keys.env.template keys.env
- Dev config for docker containers:
cp docker-compose.override.yml.template docker-compose.override.yml
-
docker compose up -d
- create postgreSQL table structure, see below.
- create OpenSearch index, see below.
- run transformation process, see below.
- when using pgAdmin, register a new server with
Host name"postgres" (container name in docker network) with port "5432". - provide credentials as defined in
.env.
-
cd scripts - Install uv and run
uv sync --frozen
In production, the DB is populated by running the crawler. In development, it may be more convenient to load pre-harvested static data:
-
create a folder per repository, e.g.,
scripts/postgres_data/data/dans_arch -
create an XML file containing records such as
<Records xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <record xmlns="http://www.openarchives.org/OAI/2.0/"> ... </record> ... </Records>
-
fetch additional metadata using
scripts/postgres_data/dataverse.py(Dataverse) orscripts/postgres_data/get_meta.py(HAL, Zenodo) Combine additional metadata files in one virtual structure usingDataverse:
import json, glob lookup = {} for f in glob.glob('*.json'): with open(f) as fh: obj = json.load(fh) key = obj["data"]["datasetPersistentId"] lookup[key] = obj # or just the fields you need # Optionally save it with open('lookup.json', 'w') as out: json.dump(lookup, out)
HAL:
import json, glob lookup = {} for f in glob.glob('*.json'): with open(f) as fh: obj = json.load(fh) if len(obj["response"]["docs"]) == 1: key = obj["response"]["docs"][0]["halId_s"] lookup[key] = obj # or just the fields you need else: print(f) print(obj) # Optionally save it with open('lookup.json', 'w') as out: json.dump(lookup, out)
-
check the settings in
scripts/postgres_data/import_data.py:HARVEST_ENDPOINTS = [ ('DANS', 'https://archaeology.datastations.nl/oai', Path('data/dans_arch/dans_arch.xml'), Path('doi_dataverse/lookup.json'), None)
where
data/dans_arch/dans_arch.xmlcontains the OAI-PMH records anddoi_dataverse/lookup.jsonthe additional metadata.
-
cd scripts/postgres_data -
create table structure and repo config as defined in
scripts/postgres_data/create_sql/$dbnameuv run create_db.py --db $dbname [--reset]This will create and init the specified DB if it does not exist yet. If it already exists and should be dropped and reinitialized, additionally provide the flag --reset.
-
the script defines the initial state of the database and should be stamped as the migration baseline.
All further changes to the database should be done as Alembic migrations.
To stamp the initial state as the baseline and then upgrade to the latest version:# Alembic migrations are executed at project root cd ../.. alembic stamp 0001_baseline alembic upgrade head # Return to scripts cd scripts/postgres_data
-
load XML data from
scripts/postgres_data/data(populates tableharvest_events):uv run import_data.py
See Prepare Data For Local Import for further details about local data preparation.
-
transform data from
scripts/postgres_data/datato a local dir (to test transformation, alternative to using the Celery process):uv run transform.py -i harvests_{repo_suffix} -o {repo_suffix}_json -s JSON_schema_file [-n] [-v]If the -n flag is provided, the JSON data will be normalized (the raw JSON may look differently based on the input XML, see these specs). If the -v flag is set, the JSON will be validated against the JSON schema file
utils/schema.json
-
cd scripts/opensearch_data -
create
test_dataciteindex (deletes existingtest_dataciteindex):uv run create_index.py
-
for sample OpenSearch queries, see open_search_queries
-
to test queries requiring vector embeddings, run
uv run query_index.py
The transformer container provides an API to start the transformation and indexing process.
A transformation requires a harvest_run_id.
When running the script import_data.py (scripts/postgres_data/data),
for each endpoint a harves run is created, the single OAI-PMH records are registered as harvest events,
and the harvest run is then closed. Note that a transformation can only be performed for a closed harvest run.
- check if transformer container is up and running:
http://127.0.0.1:8080/health
Airflow orchestrates the harvesting/transformation workflow (it replaces the custom scheduler step by step).
It runs with the CeleryExecutor: RabbitMQ (broker) is the Celery broker, and a dedicated Postgres
container (airflow-postgres) holds both Airflow's metadata and the Celery result backend.
Services: airflow-api-server (UI/API), airflow-scheduler, airflow-dag-processor, airflow-worker
(one-shot airflow-init migrates the DB and creates the admin user).
Setup (after cp env.template .env, see the # Airflow block there):
- set
AIRFLOW_FERNET_KEY,AIRFLOW_JWT_SECRET,AIRFLOW_API_SECRET_KEYand change the default passwords - on Linux set
AIRFLOW_UIDto the output ofid -u -
docker compose build airflow-init # builds the shared Airflow image once docker compose up -d - UI: http://127.0.0.1:8081 (port from
docker-compose.override.yml), login asAIRFLOW_ADMIN_USER - DAG files live in
airflow/dags. Unpause and triggerhello_worldto smoke-test the setup (scheduler -> RabbitMQ -> worker -> warehouse API).
Plans and runs the transformation of the harvest events of one closed harvest run (replaces GET /index).
Trigger it from the UI ("Trigger DAG w/ config") with:
harvest_run_id(required),index_name(defaultINDEX_NAME),reuse_embeddings,batch_size(defaultCELERY_BATCH_SIZE)
plan_batches splits the run into batches; one mapped run_batch task per batch runs on airflow-worker-transform
(queue transform, one task at a time) in the transform venv of the Airflow image (/opt/airflow/transform-venv,
built from the repo's uv.lock). It loads the batch and runs transform.transform_core.transform_batch_impl,
the same logic as the legacy Celery task. summarize prints the totals. The log of each batch shows how long
loading the embedding model and the transformation took.
The embedding model cache ./cache/fastembed is mounted into airflow-worker-transform, which runs as AIRFLOW_UID
(not root). If the model is not cached yet, make the directory writable first: mkdir -p cache/fastembed && chmod -R a+rwX cache/fastembed.
Replaces the custom scheduler: harvests the endpoints that are due and indexes the harvested runs.
plan_endpointsasks the API (GET /harvest_run) which endpoints are due (independent ones first, endpoints that depend on another endpoint, e.g. Zenodo after HAL, last)harvest_independent,harvest_dependent: one mapped task per endpoint, one at a time. The crawler runs as a Python module (harvester.run_harvest) inside the Airflow worker, its logs are part of the task log. A failed endpoint fails only its own task, the others continue.plan_indexingcollects the runs closed since the DAG run started,index_runtriggerstransform_indexfor each of them (one after the other).
Params: harvest_urls (harvest only these endpoints), all_runs (index all closed runs), index_name,
reuse_embeddings, batch_size. Schedule: set HARVEST_SCHEDULE (cron) in .env, otherwise the DAG only runs
when triggered manually. The crawler version is set with HARVESTER_REF (branch/tag/commit, built into the image).
Fills a NEW OpenSearch index from the records table (DAG id warehouse_opensearch_sync_index). Use it when
OpenSearch is out of sync with the database, e.g. after records were deleted manually in the DB.
Unlike reindex_provider nothing is harvested, transformed or embedded: the stored datacite_json and embeddings
of every record are indexed, together with the repository code and harvest url of its endpoint.
Old records (from before the doi column was removed) are normalized like new ones: url is the records.url column,
which is also the document _id, and the legacy fields id, doi and _additional_metadata are not indexed.
The records table is only read.
Trigger it from the UI ("Trigger DAG w/ config") with:
index_name(required, must NOT exist yet: if it exists the DAG run fails),batch_size(defaultCELERY_BATCH_SIZE),embedding_dims(defaultEMBEDDING_DIMS)
create_index creates the index with config/opensearch_mapping.json, sync_records reads the table with keyset
pagination (batches of batch_size, ordered by records.id) and bulk-indexes it. There is no resume: if the sync
fails, delete the partial index and trigger the DAG again.
Optionally add the following env variables (not needed for local dev):
WAREHOUSE_API_URL(default "http://transform:80")INDEX_NAME(default "test_datacite")
To format all files properly, run:
uv run ruff formatuv run ruff check --select I --fix
Before running the e2e tests locally, set the env vars POSTGRES_DB and FILE_DB
to testdatasetdb and testfiledb, respectively, since the e2e tests and the API
must use the same DBs.
Note that the e2e tests reset testdatasetdb and testfiledb on each run. Because
the test DB names are hardcoded in the e2e tests, your production DBs will not be
overwritten.
To run the e2e tests:
uv run pytest -s e2eKeep to this commit message style. For semantic versioning, see these release-please. Set up pre-commit hooks to check your messages before commiting them to the repo:
uv sync --frozen --all-extras --devuv run pre-commit install --hook-type commit-msg
See .pre-commit-config.yaml for further details.