Skip to content

Commit eddcd13

Browse files
authored
Merge pull request #842 from OpenKnowledgeMaps/dev
Local dev database insertion workaround
2 parents 5c53744 + 23d2123 commit eddcd13

10 files changed

Lines changed: 120 additions & 35 deletions

File tree

.docker.test.env

Lines changed: 0 additions & 26 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ server/workers/tests/testutils/
3333
local_dev/renv/*
3434
local_dev/dev.env
3535
local_dev/paper_preview
36+
.docker.test.env
3637

3738
# php files
3839
/server/classes/headstart/vendor

docker-compose.yml

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
services:
2-
32
db:
4-
image: 'postgres:12.2-alpine'
3+
image: "postgres:12.2-alpine"
54
hostname: "${POSTGRES_HOSTNAME}"
65
restart: unless-stopped
76
environment:
87
POSTGRES_USER: "${POSTGRES_USER}"
98
POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}"
10-
command: postgres -c config_file=/etc/postgresql.conf -c hba_file=/etc/pg_hba.conf
9+
command:
10+
postgres -c config_file=/etc/postgresql.conf -c hba_file=/etc/pg_hba.conf
1111
volumes:
1212
- db_data:/var/lib/postgresql/data
1313
- ./local_dev/pg_hba.conf:/etc/pg_hba.conf
@@ -16,18 +16,26 @@ services:
1616
- headstart
1717

1818
redis:
19-
image: 'redis:6.0-alpine'
19+
image: "redis:6.0-alpine"
2020
restart: unless-stopped
2121
hostname: "${REDIS_HOST}"
2222
environment:
2323
REDIS_HOST: "${REDIS_HOST}"
2424
REDIS_PORT: "${REDIS_PORT}"
25-
command: ["redis-server", "/etc/redis/redis.conf", "--bind", "${REDIS_HOST}", "--port", "${REDIS_PORT}"]
25+
command:
26+
[
27+
"redis-server",
28+
"/etc/redis/redis.conf",
29+
"--bind",
30+
"${REDIS_HOST}",
31+
"--port",
32+
"${REDIS_PORT}",
33+
]
2634
volumes:
27-
- 'redis:/var/lib/redis/data'
28-
- ./local_dev/redis.conf:/etc/redis/redis.conf
35+
- "redis:/var/lib/redis/data"
36+
- ./local_dev/redis.conf:/etc/redis/redis.conf
2937
ports:
30-
- "127.0.0.1:${REDIS_PORT}:${REDIS_PORT}"
38+
- "127.0.0.1:${REDIS_PORT}:${REDIS_PORT}"
3139
networks:
3240
- headstart
3341

@@ -80,6 +88,7 @@ services:
8088
- ./server/workers/persistence/src:/api
8189
depends_on:
8290
- redis
91+
- db
8392
networks:
8493
- headstart
8594

@@ -264,7 +273,6 @@ services:
264273
networks:
265274
- headstart
266275

267-
268276
volumes:
269277
redis:
270278
db_data:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"author":null,"documents":"[]"}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"endpoint": "http://127.0.0.1:8081/dev/persistence/createVisualization/dev",
3+
"visualizations": [
4+
{
5+
"vis_id": "81abae6d3af6e47761e2a761a47c2c11",
6+
"vis_title": "base",
7+
"vis_clean_query": "digital education",
8+
"vis_query": "digital education",
9+
"vis_params": "{\"from\":\"1665-01-01\",\"to\":\"2025-12-03\",\"document_types\":[\"121\"],\"sorting\":\"most-relevant\",\"min_descsize\":\"300\",\"lang_id\":[\"all-lang\"]}",
10+
"data_file": "81abae6d3af6e47761e2a761a47c2c11.json"
11+
}
12+
]
13+
}
14+
15+

local_dev/tools/db-insertion/OpenAIRE/9d4dc6b920d1e2cc08a741f7c56821db.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"endpoint": "http://127.0.0.1:8081/dev/persistence/createVisualization/dev",
3+
"visualizations": [
4+
{
5+
"vis_id": "9d4dc6b920d1e2cc08a741f7c56821db",
6+
"vis_title": "openaire",
7+
"vis_clean_query": "DP0878177",
8+
"vis_query": "DP0878177",
9+
"vis_params": "{\"project_id\":\"DP0878177\",\"funder\":\"ARC\",\"acronym\":\"\",\"title\":\"Understanding the impact of global environmental change on Australian forests and woodlands using rainforest boundaries and Callitris growth as bio-indicators\",\"start_date\":\"2008-01-01\",\"end_date\":\"2011-12-31\",\"special_clause\":\"false\",\"oa_mandate\":\"false\",\"organisations\":[],\"openaire_link\":\"http:\\/\\/purl.org\\/au-research\\/grants\\/arc\\/DP0878177\",\"obj_id\":\"arc_________::fe52f7d04f4139c2c80b4144c294f12d\",\"call_id\":\"\",\"funding_tree\":[null,null,\"Discovery Projects\"]}",
10+
"data_file": "9d4dc6b920d1e2cc08a741f7c56821db.json"
11+
}
12+
]
13+
}
14+
15+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# About that folder
2+
3+
This folder contains code that allows adding visualization information to the database.
4+
5+
> [!TIP]
6+
> This can be useful when, for example, due to errors on the part of other services (not ours), we are unable to create a visualization, but such visualization is necessary for testing or development. To avoid stopping work and blocking the task, visualisation can be added to the database using this code.
7+
8+
## How does it all work?
9+
10+
To work with this functionality, it is important to understand two main entities: `insert.py` and configuration files for each service.
11+
12+
The `insert.py` file is universal code that can write visualization information to the database for any service. To do this, you only need to specify the desired service in the `insert.py` file (or, in other words, connect the necessary configuration).
13+
14+
Services and their configurations are stored in folders such as `./BASE/` or `./OpenAIRE/`. These folders contain two files: `<visualization_id>.json` and `config.json`:
15+
- The `<visualization_id>.json` file stores visualization data that will be used to build the visualization on the client;
16+
- The `config.json` file stores the rest of the information that is important for creating a database record and will also be partially used on the client.
17+
18+
To connect the required service, simply make a change in line 7 in the `insert.py` file - this is the line where the path to a configuration (variable `CONFIG_PATH`) is created. You need to replace the service name with the required one.
19+
20+
> [!IMPORTANT]
21+
> The name of the service must match the name of the folder where its configurations are stored. Pay attention to the case of the characters!
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import json
2+
import requests
3+
4+
from pathlib import Path
5+
6+
BASE_DIR = Path(__file__).resolve().parent
7+
CONFIG_PATH = BASE_DIR / "OpenAIRE" / "config.json"
8+
9+
VISUALIZATION_DATA = []
10+
ENDPOINT_URL = "http://127.0.0.1:8081/dev/persistence/createVisualization/dev"
11+
12+
13+
def insert_from_config(config_path: Path) -> None:
14+
"""Insert visualizations described in the given config JSON file."""
15+
with open(config_path, "r") as f:
16+
config = json.load(f)
17+
18+
endpoint = config.get("endpoint", ENDPOINT_URL)
19+
visualizations = config.get("visualizations", VISUALIZATION_DATA)
20+
21+
for vis in visualizations:
22+
data_file = config_path.parent / vis["data_file"]
23+
with open(data_file, "r") as df:
24+
data = df.read()
25+
26+
payload = {
27+
"vis_id": vis["vis_id"],
28+
"vis_title": vis["vis_title"],
29+
"vis_clean_query": vis["vis_clean_query"],
30+
"vis_query": vis["vis_query"],
31+
"vis_params": vis["vis_params"],
32+
"data": data,
33+
}
34+
35+
res = requests.post(endpoint, json=payload)
36+
print(f"Inserted {vis['vis_id']}: {res.status_code}")
37+
print(res.text)
38+
39+
40+
def main() -> None:
41+
insert_from_config(CONFIG_PATH)
42+
43+
44+
if __name__ == "__main__":
45+
main()
46+
47+

server/workers/tests/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ listen_addresses = 'localhost,headstart_db_1,headstart-db-1'
6969

7070
Run tests
7171

72+
Create a `.docker.test.env` file first, then run the following command:
73+
7274
```
7375
docker compose -f docker-compose-integration-tests.yml --env-file .docker.test.env run integration_tests
7476
```

0 commit comments

Comments
 (0)