Skip to content

Commit ae33b71

Browse files
authored
Merge pull request #924 from mapswipe/dev
Backend Workers Refactoring
2 parents e65431f + adfc2d8 commit ae33b71

File tree

152 files changed

+10897
-1351
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

152 files changed

+10897
-1351
lines changed

.github/workflows/actions.yml

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ jobs:
6363
env:
6464
FIREBASE_API_KEY: ${{ secrets.FIREBASE_API_KEY }}
6565
FIREBASE_DB: ${{ secrets.FIREBASE_DB }}
66+
FIREBASE_STORAGE_BUCKET: ${{ secrets.FIREBASE_STORAGE_BUCKET }}
6667
POSTGRES_PASSWORD: postgres
6768
POSTGRES_USER: postgres
6869
POSTGRES_DB: postgres

Makefile

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ update_firebase_functions_and_db_rules:
2424
docker compose run --rm firebase_deploy
2525

2626
deploy_latest_workers_version:
27-
git pull; docker compose build postgres django django-schedule-task manager_dashboard community_dashboard nginx api mapswipe_workers_creation mapswipe_workers_stats mapswipe_workers_firebase_to_postgres firebase_deploy; docker compose up -d postgres django django-schedule-task manager_dashboard community_dashboard nginx api mapswipe_workers_creation mapswipe_workers_stats mapswipe_workers_firebase_to_postgres firebase_deploy
28-
27+
git pull; docker compose build postgres django django-schedule-task manager_dashboard community_dashboard nginx api mapswipe_workers_creation mapswipe_workers_stats mapswipe_workers_firebase_to_postgres firebase_deploy; docker compose up -d postgres django django-schedule-task manager_dashboard community_dashboard nginx api mapswipe_workers_creation mapswipe_workers_stats mapswipe_workers_firebase_to_postgres firebase_deploy; docker compose restart nginx
2928
update_tms_api_key:
3029
@echo "first update the .env file with the new api key and then run this script with tms_name=your_tms_provider_name to update API key"; docker compose run --rm mapswipe_workers_creation python python_scripts/change_tms_api_key_for_projects.py $(tms_name)
30+
31+
list_all_project_managers:
32+
docker compose run --rm mapswipe_workers_creation python python_scripts/list_all_project_managers.py

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ Please refer to the documentation for more information: https://mapswipe-workers
2121
- MapSwipe OSM-Wiki: https://wiki.openstreetmap.org/wiki/MapSwipe
2222

2323

24+
## Development Setup
25+
26+
Please see here: https://mapswipe-workers.readthedocs.io/en/master/dev_setup.html
27+
2428
## Contributing Guidelines
2529

2630
### Feature Branch

docker-compose.development.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ services:
1313
manager_dashboard:
1414
container_name: manager_dashboard
1515
build:
16-
context: manager_dashboard/
16+
context: manager-dashboard/
1717
restart: unless-stopped
1818
ports:
1919
- '5000:80'
2020
expose:
2121
- "80"
2222
volumes:
23-
- ./manager_dashboard/manager_dashboard:/usr/share/nginx/html/manager_dashboard
23+
- ./manager-dashboard:/usr/share/nginx/html/manager_dashboard
2424
networks:
2525
- manager_dashboard
2626

@@ -39,6 +39,6 @@ services:
3939
- ./postgres-data:/var/lib/postgresql/mapswipe
4040
restart: unless-stopped
4141
ports:
42-
- "5431:5432"
42+
- "${POSTGRES_PORT}:5432"
4343
networks:
44-
- postgres
44+
- postgres

docker-compose.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ x-mapswipe-workers: &base_mapswipe_workers
5858
FIREBASE_DB: '${FIREBASE_DB}'
5959
FIREBASE_API_KEY: '${FIREBASE_API_KEY}'
6060
FIREBASE_TOKEN: '${FIREBASE_TOKEN}'
61+
FIREBASE_STORAGE_BUCKET: '${FIREBASE_STORAGE_BUCKET}'
6162
GOOGLE_APPLICATION_CREDENTIALS: 'serviceAccountKey.json'
6263
POSTGRES_PASSWORD: '${POSTGRES_PASSWORD}'
6364
POSTGRES_USER: '${POSTGRES_USER}'

docs/source/configuration.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Most of the configuration is stored in environment variables.
44
At the root of the GitHub repository (in the same directory as `docker-compose.yml`) an example file (`example.env`) with all possible configuration variables exists. To get started copy this file to `.env` (no name is required) and fill in missing variables. The Docker Compose file will access those variables when needed.
55

6-
> Note: If you want those variables to be accessible as Environment Variables in your current shell (Eg. Inside a Python virtual environment for development.) your need to parse the file and export the variables: `export $(cat .env | xargs)`
6+
> Note: If you want those variables to be accessible as Environment Variables in your current shell (E.g. Inside a Python virtual environment for development.) your need to parse the file and export the variables: `export $(cat .env | xargs)`
77
88
In following chapters configuration values and keys are discussed for each part of the MapSwipe Back-end.
99

@@ -17,6 +17,7 @@ Required environment variables are:
1717
- FIREBASE_API_KEY
1818
- FIREBASE_DB
1919
- FIREBASE_TOKEN
20+
- FIREBASE_STORAGE_BUCKET
2021
- GOOGLE_APPLICATION_CREDENTIALS
2122

2223
### Postgres DB

docs/source/dev_setup.md

+19-18
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,6 @@ The path to the Service Account Key is defined in the `GOOGLE_APPLICATION_CREDEN
4646
You could also set up your own Firebase instance. However, this is not recommended.
4747
If you still want to do it, get your Service Account Key from Firebase from [Google Cloud Service Accounts](https://console.cloud.google.com/iam-admin/serviceaccounts).
4848

49-
#### Directories
50-
51-
MapSwipe Workers needs access to a data directory for logs and data for the API:
52-
53-
To create this directories run:
54-
```
55-
mkdir --parents ~/.local/share/mapswipe_workers
56-
```
57-
58-
> Note: XDG Base Directory Specification is respected
59-
6049

6150
### Database
6251

@@ -74,21 +63,33 @@ Or set up Postgres using the `initdb.sql` file in the `postgres/` folder.
7463

7564
### Mapswipe-Workers Python Package
7665

77-
1. Export environment variables to current shell.
78-
2. Create a Python virtual environment with `system-site-packages` option enabled to get access to GDAL/OGR Python packages
79-
3. Activate the virtual environment.
80-
5. Install MapSwipe Workers using pip.
81-
6. Run it.
66+
#### Installation
67+
68+
The Python virtual environment needs to be created with `system-site-packages` option enabled to get access to GDAL/OGR Python packages.
69+
Also the MapSwipe Workers Python Package needs access to a data directory for logs and data for the API.
8270

8371
```bash
84-
export $(cat .env | xargs)
8572
cd mapswipe_workers
8673
python -m venv --system-site-packages venv
8774
source venv/bin/activate
88-
pip install --editable mapswipe_workers/
75+
pip install --editable .
76+
pre-commit install
77+
mkdir --parents ~/.local/share/mapswipe_workers
78+
```
79+
80+
#### Usage
81+
82+
```bash
83+
export $(cat .env | xargs) # See section on Configuration above
8984
mapswipe_workers --help
9085
```
9186

87+
#### Tests
88+
89+
```bash
90+
python -m unittest
91+
```
92+
9293
> Yeah! If you reached this point, you are ready to get into coding. Below you find some more information on Logging, Firebase Functions and Database Backup. However, you don't need this to get started for now.
9394
9495
# Further Information

example.env

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ FIREBASE_API_KEY=
66
FIREBASE_TOKEN=
77
GOOGLE_APPLICATION_CREDENTIALS="$HOME/.config/mapswipe_workers/serviceAccountKey.json"
88
FIREBASE_AUTH_SITE=
9+
FIREBASE_STORAGE_BUCKET=
910

1011
# postgres configuration
1112
POSTGRES_USER=mapswipe_workers

firebase/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ RUN npm install firebase-functions@latest firebase-admin@latest --save
33
RUN npm install -g [email protected]
44
COPY . /firebase
55
RUN cd firebase/functions && npm install
6-
WORKDIR /firebase/
6+
WORKDIR /firebase/

firebase/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Create a docker image from the latest data.
77
* `docker-compose build firebase_image`
88

99
Alternatively you could do
10-
* `sudo docker build -t . firebase_image`
10+
* `sudo docker build -t firebase_image .`
1111

1212
Then run the container interactively and open a bash shell.
1313
* `sudo docker run -it firebase_image /bin/bash`

mapswipe_workers/.pre-commit-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ repos:
44
rev: 22.3.0
55
hooks:
66
- id: black
7-
- repo: https://gitlab.com/pycqa/flake8
7+
- repo: https://github.com/PyCQA/flake8/
88
rev: 3.8.3
99
hooks:
1010
- id: flake8
1111
- repo: https://github.com/PyCQA/isort
12-
rev: 5.5.2
12+
rev: 5.12.0
1313
hooks:
1414
- id: isort

mapswipe_workers/mapswipe_workers/auth.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,25 @@ def get_tileserver_url(tileserver: str) -> str:
2929

3030

3131
def firebaseDB() -> db:
32+
"""Initialize the app.
33+
34+
Credentials will be retrieved from of following environment variable:
35+
`GOOGLE_APPLICATION_CREDENTIALS` which contains the path to service account
36+
key.
37+
"""
38+
3239
try:
3340
# Is an App instance already initialized?
3441
firebase_admin.get_app()
3542
# Return the imported Firebase Realtime Database module
3643
return db
3744
except ValueError:
38-
databaseURL = f"https://{FIREBASE_DB}.firebaseio.com"
39-
# Initialize the app.
40-
# Credentials will be retrieved from of following environment variable:
41-
# GOOGLE_APPLICATION_CREDENTIALS (Path to service account key in json format)
42-
firebase_admin.initialize_app(options={"databaseURL": databaseURL})
43-
# Return the imported Firebase Realtime Database module
45+
if "https://" in FIREBASE_DB:
46+
firebase_admin.initialize_app(options={"databaseURL": FIREBASE_DB})
47+
else:
48+
databaseURL = f"https://{FIREBASE_DB}.firebaseio.com"
49+
firebase_admin.initialize_app(options={"databaseURL": databaseURL})
50+
4451
return db
4552

4653

mapswipe_workers/mapswipe_workers/config.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
FIREBASE_API_KEY = os.environ["FIREBASE_API_KEY"]
44
FIREBASE_DB = os.getenv("FIREBASE_DB", default="mapswipe")
5+
FIREBASE_STORAGE_BUCKET = os.getenv("FIREBASE_STORAGE_BUCKET")
56

67
POSTGRES_DB = os.getenv("POSTGRES_DB", default="mapswipe")
78
POSTGRES_HOST = os.getenv("POSTGRES_HOST", "postgres")

mapswipe_workers/mapswipe_workers/definitions.py

+24-23
Original file line numberDiff line numberDiff line change
@@ -132,44 +132,45 @@ class ProjectType(Enum):
132132
FOOTPRINT = 2
133133
CHANGE_DETECTION = 3
134134
COMPLETENESS = 4
135+
MEDIA_CLASSIFICATION = 5
136+
DIGITIZATION = 6
135137

136138
@property
137139
def constructor(self):
138140
# Imports are first made once this method get called to avoid circular imports.
139-
from mapswipe_workers.project_types.arbitrary_geometry.project import (
140-
Project as ag_project,
141-
)
142-
from mapswipe_workers.project_types.tile_map_service_grid.project import (
143-
Project as tmsg_project,
141+
from mapswipe_workers.project_types import (
142+
ChangeDetectionProject,
143+
ClassificationProject,
144+
CompletenessProject,
145+
DigitizationProject,
146+
FootprintProject,
147+
MediaClassificationProject,
144148
)
145149

146150
project_type_classes = {
147-
1: tmsg_project,
148-
2: ag_project,
149-
3: tmsg_project,
150-
4: tmsg_project,
151+
1: ClassificationProject,
152+
2: FootprintProject,
153+
3: ChangeDetectionProject,
154+
4: CompletenessProject,
155+
5: MediaClassificationProject,
156+
6: DigitizationProject,
151157
}
152158
return project_type_classes[self.value]
153159

154160
@property
155161
def tutorial(self):
156162
# Imports are first made once this method get called to avoid circular imports.
157-
from mapswipe_workers.project_types.arbitrary_geometry.tutorial import (
158-
Tutorial as ArbitraryGeometryTutorial,
159-
)
160-
from mapswipe_workers.project_types.tile_map_service_grid.tutorial import (
161-
Tutorial as tmsg_tutorial,
163+
from mapswipe_workers.project_types import (
164+
ChangeDetectionTutorial,
165+
ClassificationTutorial,
166+
CompletenessTutorial,
167+
FootprintTutorial,
162168
)
163169

164-
# TODO: implement for arbitrary geometries
165-
# from mapswipe_workers.project_types.arbitrary_geometries.arbitrary_geometries_tutorial import ( # noqa E501
166-
# ArbitraryGeometriesTutorial,
167-
# )
168-
169170
project_type_classes = {
170-
1: tmsg_tutorial,
171-
2: ArbitraryGeometryTutorial,
172-
3: tmsg_tutorial,
173-
4: tmsg_tutorial,
171+
1: ClassificationTutorial,
172+
2: FootprintTutorial,
173+
3: ChangeDetectionTutorial,
174+
4: CompletenessTutorial,
174175
}
175176
return project_type_classes[self.value]

0 commit comments

Comments
 (0)