Skip to content

WIP: refactor(project-types)!: introduce base class and make groups and lists attribute of project type classes #920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 170 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
170 commits
Select commit Hold shift + click to select a range
b4cc68b
rename docker dev context manager dashboard
maze2point0 May 15, 2023
c717f18
docker dev context / name
maze2point0 May 15, 2023
b8f231a
make sure to initialize firebase app from newer url format
Hagellach37 May 15, 2023
f251ead
Merge branch 'refactor-python-workers' of github.com:mapswipe/python-…
Hagellach37 May 15, 2023
e480b67
add headline for setup instructions
masain May 15, 2023
ced859c
add instuctions for setup backend
masain May 15, 2023
b6d1bd7
add link to docs for setup instructions
masain May 15, 2023
18064ba
started splitting into sub project types
ElJocho May 16, 2023
de02ed8
Merge pull request #781 from ElJocho/refactor-python-workers
Hagellach37 May 16, 2023
830c931
update flake8 repo pre-commit hook
maze2point0 May 16, 2023
055ea3a
add unit test for tile classification project init
maze2point0 May 16, 2023
12cf189
splitted geometries from projects and validation as well as tests
maze2point0 May 16, 2023
1ce432e
add postgres port from env
maze2point0 May 16, 2023
6531a76
use dotenv to read from .env
maze2point0 May 16, 2023
aefd04e
fea(re)
ElJocho May 16, 2023
3b6b145
Merge branch 'refactor-python-workers' into refactor-python-workers
ElJocho May 16, 2023
c1d8eb4
Merge pull request #1 from mapswipe/refactor-python-workers
ElJocho May 16, 2023
bc655a2
add test for group and task creation for tile classification project …
maze2point0 May 16, 2023
c98f3dc
Merge pull request #2 from mapswipe/refactor-python-workers
ElJocho May 16, 2023
2a4dc82
add group/task creation to tile classification project and integratio…
maze2point0 May 17, 2023
bb9eb48
rename test function
maze2point0 May 22, 2023
d33d7da
ad class for tile classification tutorial and integration test
maze2point0 May 22, 2023
6e66909
added save to firebase as overloaded
ElJocho May 22, 2023
7856578
Merge branch 'refactor-python-workers' into refactor-python-workers
ElJocho May 22, 2023
14cf2fb
Merge pull request #789 from ElJocho/refactor-python-workers
Hagellach37 May 22, 2023
d9a573b
Revert "Refactor python workers"
Hagellach37 May 22, 2023
237d1b1
Merge pull request #790 from mapswipe/revert-789-refactor-python-workers
Hagellach37 May 22, 2023
7d00f70
Revert "Revert "Refactor python workers""
Hagellach37 May 22, 2023
31c8bce
Update mapswipe_workers/mapswipe_workers/project_types/base/project.py
Hagellach37 May 22, 2023
99fefeb
Merge pull request #791 from mapswipe/revert-790-revert-789-refactor-…
Hagellach37 May 22, 2023
8d8ca6d
remove dotenv dependency
Hagellach37 May 22, 2023
3dc4d98
comment out abstract method decorator and add todo
Hagellach37 May 22, 2023
2926e0e
better description why tasks are not saved in firebase
Hagellach37 May 22, 2023
f7fc45e
remove unused function and test
Hagellach37 May 22, 2023
8960337
remove unused file
Hagellach37 May 22, 2023
97966a6
rename test file and remove deletion of unassigned variable
Hagellach37 May 22, 2023
a218736
make black and flake8 happy
Hagellach37 May 22, 2023
1db6ba8
add init files for tests
Hagellach37 May 22, 2023
decf850
lock firebase tools version to 11.30.0
Hagellach37 May 22, 2023
f2039df
Update file path in tests
Hagellach37 May 22, 2023
14682a8
fix import test get results real project
itisacloud May 22, 2023
81a0b03
Merge remote-tracking branch 'origin/refactor-python-workers' into re…
itisacloud May 22, 2023
050f60e
test: change abs path to rel path
matthiasschaub May 22, 2023
884b193
fix import set_up and tear_down in tests
itisacloud May 22, 2023
6464576
Merge remote-tracking branch 'origin/refactor-python-workers' into re…
itisacloud May 22, 2023
aed4ad1
fix import BaseTestCase for tests
itisacloud May 22, 2023
c9ee3cf
Merge pull request #792 from mapswipe/refactor-python-workers
Hagellach37 May 22, 2023
6c116a8
docs: enhance docs on python setup and tests
matthiasschaub May 22, 2023
6b95645
docs: add information on how to install pre-commit
matthiasschaub May 22, 2023
0797b66
Merge pull request #793 from mapswipe/dev-docs
Hagellach37 May 22, 2023
c1d6ced
test push
ElJocho May 22, 2023
170c990
fix: add missing __init__.py
matthiasschaub May 22, 2023
a20dd86
add tests for additional project base attributes
ofr1tz May 23, 2023
6b91a9f
add tests for project creation with default attributes
ofr1tz May 23, 2023
3e5d05f
add default value explanation
ofr1tz May 23, 2023
a34ca7c
Merge pull request #803 from mapswipe/test-additional-base-attributes
Hagellach37 May 23, 2023
9bb03a0
refac[mapswipe-workers] #784
ElJocho May 23, 2023
0ab6662
merge
ElJocho May 23, 2023
9bd664a
fix typo in sql query
Hagellach37 May 23, 2023
02c28e4
refact[mapswipe_workers] black and test fixes
ElJocho May 23, 2023
e663f36
isort
ElJocho May 23, 2023
0e5379e
removed useless prints
ElJocho May 23, 2023
c49a766
fix bug in agg_results_by_task_id
Hagellach37 May 23, 2023
58bc859
wip test media projects
ofr1tz May 23, 2023
aad4319
test wip
masain May 23, 2023
97d91d0
Merge pull request #805 from mapswipe/test-fixtures
ofr1tz May 23, 2023
db7b0c8
feat[mapswipe_workers] imported media type project creation
ElJocho May 24, 2023
18e03cc
cleanup
ElJocho May 24, 2023
836f582
fix test
ElJocho May 24, 2023
bf576a0
refactor(project-types)!: introduce base class
masain May 23, 2023
001c5d6
refactor(project-types): completeness
matthiasschaub May 25, 2023
9e49090
docs: make comments clearer
matthiasschaub May 25, 2023
d421066
changed to typing annotation
ElJocho May 25, 2023
9a54f55
Refact[mapswipe-workers] #787 additionally refactored group and task …
ElJocho May 30, 2023
eba5442
merge media classification
ElJocho May 30, 2023
7a67acf
refact[mapswipe-workers] added and resolved tests for change-detectio…
ElJocho May 30, 2023
40d4004
refact[mapswipe_workers] refactored media classification to follow ne…
ElJocho May 30, 2023
d23e6b1
adjust imports
ElJocho May 30, 2023
e7bab2a
refac[mapswipe_workers] refactored arbitrary geometry to fit new stru…
ElJocho May 31, 2023
51660cc
refact[mapswipe-workers] adjusted ag tutorial to new structure
ElJocho Jun 5, 2023
2f2bef9
tried to clarify the sql
ElJocho Jun 5, 2023
11c7957
intendent
ElJocho Jun 5, 2023
29a471b
test[mapswipe-workers] fixed media type test
ElJocho Jun 5, 2023
c23df4a
test[mapswipe-workers] fixed the test correctly this time
ElJocho Jun 5, 2023
22e1ee8
feat[mapswipe_workers] added digitalisation project creation
ElJocho Jun 6, 2023
ef8b8f6
wip tutorial
ElJocho Jun 6, 2023
ea2b866
deploy bugs
ElJocho Jun 6, 2023
186d490
deployment bug
ElJocho Jun 6, 2023
80f9b17
deployment bug digitzation
ElJocho Jun 6, 2023
75511ff
deployment bug digitzation path
ElJocho Jun 6, 2023
092d981
added completness tutorial test
ElJocho Jun 6, 2023
1dc9501
fixed test
ElJocho Jun 7, 2023
5063517
merge
ElJocho Jun 7, 2023
c4beda7
remove relative imports
ElJocho Jun 7, 2023
3df671c
merge
ElJocho Jun 7, 2023
c428330
Update docker-compose.yaml
ElJocho Jun 7, 2023
1e7e705
new github env var
ElJocho Jun 7, 2023
25a5a1c
Merge branch 'add_custom_answers' of https://github.com/mapswipe/pyth…
ElJocho Jun 7, 2023
9aa2a6d
added env for github workflow
ElJocho Jun 7, 2023
36c2de6
added env for github workflow
ElJocho Jun 7, 2023
c755707
changed names for clarity
ElJocho Jun 7, 2023
17838cf
added optional attribute tileserver.maxZoom
ElJocho Jun 7, 2023
3233465
also add in project py
ElJocho Jun 7, 2023
247c725
Merge pull request #809 from mapswipe/add_digitalisation_project_type
ElJocho Jun 7, 2023
6505eed
test[overall_stats] added test and fixed syntax bugs
ElJocho Jun 7, 2023
cd79654
merge
ElJocho Jun 12, 2023
f3491db
Update mapswipe_workers/mapswipe_workers/project_types/arbitrary_geom…
ElJocho Jun 12, 2023
1a05bb3
change to crowdmapping default
ElJocho Jun 12, 2023
4f76601
Merge branch 'add_custom_answers' of https://github.com/mapswipe/pyth…
ElJocho Jun 12, 2023
742051a
typo
ElJocho Jun 12, 2023
060adcf
feat[mapswipe_workers] draft for data transfer from firebase to postg…
ElJocho Jun 12, 2023
2e05133
Merge pull request #794 from mapswipe/add_custom_answers
ElJocho Jun 13, 2023
66efb2c
feat(digitalization project type) added firebase to postgres for digi…
ElJocho Jun 13, 2023
915b0f1
fix comments
ElJocho Jun 13, 2023
b18c0ec
sync
ElJocho Jun 13, 2023
09284c2
also try to delete from geometry table
ElJocho Jun 13, 2023
13bd291
path
ElJocho Jun 13, 2023
af101e2
path
ElJocho Jun 13, 2023
928b2b9
fixed missing parameter and clarified test names
ElJocho Jun 14, 2023
c9b50be
typo
ElJocho Jun 19, 2023
70ed835
typo and deprecated warning removal
ElJocho Jun 19, 2023
f53165e
feat[digitization] added result to stats workflwo
ElJocho Jun 19, 2023
d29ce9a
type annotations
ElJocho Jun 20, 2023
85a33f0
weird docstring
ElJocho Jun 20, 2023
1f0e88c
removed unused parameter
ElJocho Jun 20, 2023
c2ff417
new function docstrings everywhere
ElJocho Jun 20, 2023
6432c92
type annotations
ElJocho Jun 20, 2023
a9abbbb
move dirs back into function
ElJocho Jun 20, 2023
07fcead
removed circular import
ElJocho Jun 20, 2023
ed0d923
moved base project in oprject_Type root folder
ElJocho Jul 10, 2023
458b9da
Merge branch 'dev' of https://github.com/mapswipe/python-mapswipe-wor…
ElJocho Jul 10, 2023
f4e41e5
Merge pull request #848 from mapswipe/digitalisation_result_handling
ElJocho Jul 10, 2023
2218c47
Merge branch 'dev' of https://github.com/mapswipe/python-mapswipe-wor…
ElJocho Jul 17, 2023
f6458dd
merge
ElJocho Jul 18, 2023
9ce4de7
fix test
ElJocho Jul 18, 2023
2b77126
Update README.md
ElJocho Jul 18, 2023
c6221d5
Update docker-compose.yaml
ElJocho Jul 18, 2023
c967320
Update docs/source/dev_setup.md
ElJocho Jul 18, 2023
544cecc
Update mapswipe_workers/mapswipe_workers/auth.py
ElJocho Jul 18, 2023
a5a315c
style(auth) formatting with black
ElJocho Jul 24, 2023
f07f0a3
Merge branch 'dev' of https://github.com/mapswipe/python-mapswipe-wor…
ElJocho Aug 14, 2023
66225c6
Merge branch 'dev' into project_type_refactoring
ElJocho Aug 14, 2023
4676924
small bug fixes
ElJocho Aug 16, 2023
db5a92d
merge
ElJocho Jan 8, 2024
5dd9937
delete project draft after project creation
matthiasschaub Jan 9, 2024
f123af4
docs: add missing config env (fb storage bucket)
matthiasschaub Jan 9, 2024
ae20084
docs: fix error in docker command
matthiasschaub Jan 9, 2024
a1ff002
test: fix wrong firebase ref in tear down
matthiasschaub Jan 10, 2024
57e9e69
tests: fix relative imports
ElJocho Jan 8, 2024
917b770
fix: remove default val for project.customOptions
matthiasschaub Jan 10, 2024
cb08e68
tests: rename tileServerA to tileServer in fixutre
matthiasschaub Jan 10, 2024
fae5c1b
tests: change app number from 99 to 3
matthiasschaub Jan 10, 2024
8bec12e
test: change test to reflect togglecorps default values
ElJocho Jan 10, 2024
933663d
fix: project_area was not correctly summing up all geometry areas, th…
ElJocho Jan 15, 2024
8708f18
refac: split and renamed the validate_geometry function for tile base…
ElJocho Jan 15, 2024
75de897
refac: rename variable to reflect its usage
ElJocho Jan 15, 2024
a4f3ae0
refac: black and flake8
ElJocho Jan 15, 2024
16689d3
chore: isort
ElJocho Jan 15, 2024
3a393b5
test: add additional userId to teardown list to fix bugs on rerunning…
ElJocho Jan 15, 2024
1ea2291
feat: moved deletion of results into project_types to account for dif…
ElJocho Jan 15, 2024
d909b40
fix: remove unneeded overrides
ElJocho Jan 15, 2024
0eb111b
chore: formatting
ElJocho Jan 16, 2024
4e30a68
fix: removed wrapping in list
ElJocho Jan 16, 2024
e4ab896
chore: update to new name
ElJocho Jan 16, 2024
68cd66f
fix: fix creation of footprint tutorial
ElJocho Jan 16, 2024
2e93fa7
test: test creation of footprint tutorial
ElJocho Jan 16, 2024
769191a
chore: update outdated test data
ElJocho Jan 16, 2024
012c09d
fix: share columns influenced agreement calculation
ElJocho Jan 16, 2024
49e6556
chore: formatting
ElJocho Jan 16, 2024
6bc48dc
fix: remove unused unnamed columns in agg_results
ElJocho Jan 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ jobs:
env:
FIREBASE_API_KEY: ${{ secrets.FIREBASE_API_KEY }}
FIREBASE_DB: ${{ secrets.FIREBASE_DB }}
FIREBASE_STORAGE_BUCKET: ${{ secrets.FIREBASE_STORAGE_BUCKET }}
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Please refer to the documentation for more information: https://mapswipe-workers
- MapSwipe OSM-Wiki: https://wiki.openstreetmap.org/wiki/MapSwipe


## Development Setup

Please see here: https://mapswipe-workers.readthedocs.io/en/master/dev_setup.html

## Contributing Guidelines

### Feature Branch
Expand Down
8 changes: 4 additions & 4 deletions docker-compose.development.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ services:
manager_dashboard:
container_name: manager_dashboard
build:
context: manager_dashboard/
context: manager-dashboard/
restart: unless-stopped
ports:
- '5000:80'
expose:
- "80"
volumes:
- ./manager_dashboard/manager_dashboard:/usr/share/nginx/html/manager_dashboard
- ./manager-dashboard:/usr/share/nginx/html/manager_dashboard
networks:
- manager_dashboard

Expand All @@ -39,6 +39,6 @@ services:
- ./postgres-data:/var/lib/postgresql/mapswipe
restart: unless-stopped
ports:
- "5431:5432"
- "${POSTGRES_PORT}:5432"
networks:
- postgres
- postgres
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ x-mapswipe-workers: &base_mapswipe_workers
FIREBASE_DB: '${FIREBASE_DB}'
FIREBASE_API_KEY: '${FIREBASE_API_KEY}'
FIREBASE_TOKEN: '${FIREBASE_TOKEN}'
FIREBASE_STORAGE_BUCKET: '${FIREBASE_STORAGE_BUCKET}'
GOOGLE_APPLICATION_CREDENTIALS: 'serviceAccountKey.json'
POSTGRES_PASSWORD: '${POSTGRES_PASSWORD}'
POSTGRES_USER: '${POSTGRES_USER}'
Expand Down
3 changes: 2 additions & 1 deletion docs/source/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Most of the configuration is stored in environment variables.
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.

> 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)`
> 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)`

In following chapters configuration values and keys are discussed for each part of the MapSwipe Back-end.

Expand All @@ -17,6 +17,7 @@ Required environment variables are:
- FIREBASE_API_KEY
- FIREBASE_DB
- FIREBASE_TOKEN
- FIREBASE_STORAGE_BUCKET
- GOOGLE_APPLICATION_CREDENTIALS

### Postgres DB
Expand Down
37 changes: 19 additions & 18 deletions docs/source/dev_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,6 @@ The path to the Service Account Key is defined in the `GOOGLE_APPLICATION_CREDEN
You could also set up your own Firebase instance. However, this is not recommended.
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).

#### Directories

MapSwipe Workers needs access to a data directory for logs and data for the API:

To create this directories run:
```
mkdir --parents ~/.local/share/mapswipe_workers
```

> Note: XDG Base Directory Specification is respected


### Database

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

### Mapswipe-Workers Python Package

1. Export environment variables to current shell.
2. Create a Python virtual environment with `system-site-packages` option enabled to get access to GDAL/OGR Python packages
3. Activate the virtual environment.
5. Install MapSwipe Workers using pip.
6. Run it.
#### Installation

The Python virtual environment needs to be created with `system-site-packages` option enabled to get access to GDAL/OGR Python packages.
Also the MapSwipe Workers Python Package needs access to a data directory for logs and data for the API.

```bash
export $(cat .env | xargs)
cd mapswipe_workers
python -m venv --system-site-packages venv
source venv/bin/activate
pip install --editable mapswipe_workers/
pip install --editable .
pre-commit install
mkdir --parents ~/.local/share/mapswipe_workers
```

#### Usage

```bash
export $(cat .env | xargs) # See section on Configuration above
mapswipe_workers --help
```

#### Tests

```bash
python -m unittest
```

> 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.

# Further Information
Expand Down
1 change: 1 addition & 0 deletions example.env
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ FIREBASE_API_KEY=
FIREBASE_TOKEN=
GOOGLE_APPLICATION_CREDENTIALS="$HOME/.config/mapswipe_workers/serviceAccountKey.json"
FIREBASE_AUTH_SITE=
FIREBASE_STORAGE_BUCKET=

# postgres configuration
POSTGRES_USER=mapswipe_workers
Expand Down
2 changes: 1 addition & 1 deletion firebase/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ RUN npm install firebase-functions@latest firebase-admin@latest --save
RUN npm install -g firebase-tools@11.30.0
COPY . /firebase
RUN cd firebase/functions && npm install
WORKDIR /firebase/
WORKDIR /firebase/
2 changes: 1 addition & 1 deletion firebase/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Create a docker image from the latest data.
* `docker-compose build firebase_image`

Alternatively you could do
* `sudo docker build -t . firebase_image`
* `sudo docker build -t firebase_image .`

Then run the container interactively and open a bash shell.
* `sudo docker run -it firebase_image /bin/bash`
Expand Down
4 changes: 2 additions & 2 deletions mapswipe_workers/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ repos:
rev: 22.3.0
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
- repo: https://github.com/PyCQA/flake8/
rev: 3.8.3
hooks:
- id: flake8
- repo: https://github.com/PyCQA/isort
rev: 5.5.2
rev: 5.12.0
hooks:
- id: isort
19 changes: 13 additions & 6 deletions mapswipe_workers/mapswipe_workers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,25 @@ def get_tileserver_url(tileserver: str) -> str:


def firebaseDB() -> db:
"""Initialize the app.

Credentials will be retrieved from of following environment variable:
`GOOGLE_APPLICATION_CREDENTIALS` which contains the path to service account
key.
"""

try:
# Is an App instance already initialized?
firebase_admin.get_app()
# Return the imported Firebase Realtime Database module
return db
except ValueError:
databaseURL = f"https://{FIREBASE_DB}.firebaseio.com"
# Initialize the app.
# Credentials will be retrieved from of following environment variable:
# GOOGLE_APPLICATION_CREDENTIALS (Path to service account key in json format)
firebase_admin.initialize_app(options={"databaseURL": databaseURL})
# Return the imported Firebase Realtime Database module
if "https://" in FIREBASE_DB:
firebase_admin.initialize_app(options={"databaseURL": FIREBASE_DB})
else:
databaseURL = f"https://{FIREBASE_DB}.firebaseio.com"
firebase_admin.initialize_app(options={"databaseURL": databaseURL})

return db


Expand Down
1 change: 1 addition & 0 deletions mapswipe_workers/mapswipe_workers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

FIREBASE_API_KEY = os.environ["FIREBASE_API_KEY"]
FIREBASE_DB = os.getenv("FIREBASE_DB", default="mapswipe")
FIREBASE_STORAGE_BUCKET = os.getenv("FIREBASE_STORAGE_BUCKET")

POSTGRES_DB = os.getenv("POSTGRES_DB", default="mapswipe")
POSTGRES_HOST = os.getenv("POSTGRES_HOST", "postgres")
Expand Down
47 changes: 24 additions & 23 deletions mapswipe_workers/mapswipe_workers/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,44 +132,45 @@ class ProjectType(Enum):
FOOTPRINT = 2
CHANGE_DETECTION = 3
COMPLETENESS = 4
MEDIA_CLASSIFICATION = 5
DIGITIZATION = 6

@property
def constructor(self):
# Imports are first made once this method get called to avoid circular imports.
from mapswipe_workers.project_types.arbitrary_geometry.project import (
Project as ag_project,
)
from mapswipe_workers.project_types.tile_map_service_grid.project import (
Project as tmsg_project,
from mapswipe_workers.project_types import (
ChangeDetectionProject,
ClassificationProject,
CompletenessProject,
DigitizationProject,
FootprintProject,
MediaClassificationProject,
)

project_type_classes = {
1: tmsg_project,
2: ag_project,
3: tmsg_project,
4: tmsg_project,
1: ClassificationProject,
2: FootprintProject,
3: ChangeDetectionProject,
4: CompletenessProject,
5: MediaClassificationProject,
6: DigitizationProject,
}
return project_type_classes[self.value]

@property
def tutorial(self):
# Imports are first made once this method get called to avoid circular imports.
from mapswipe_workers.project_types.arbitrary_geometry.tutorial import (
Tutorial as ArbitraryGeometryTutorial,
)
from mapswipe_workers.project_types.tile_map_service_grid.tutorial import (
Tutorial as tmsg_tutorial,
from mapswipe_workers.project_types import (
ChangeDetectionTutorial,
ClassificationTutorial,
CompletenessTutorial,
FootprintTutorial,
)

# TODO: implement for arbitrary geometries
# from mapswipe_workers.project_types.arbitrary_geometries.arbitrary_geometries_tutorial import ( # noqa E501
# ArbitraryGeometriesTutorial,
# )

project_type_classes = {
1: tmsg_tutorial,
2: ArbitraryGeometryTutorial,
3: tmsg_tutorial,
4: tmsg_tutorial,
1: ClassificationTutorial,
2: FootprintTutorial,
3: ChangeDetectionTutorial,
4: CompletenessTutorial,
}
return project_type_classes[self.value]
112 changes: 112 additions & 0 deletions mapswipe_workers/mapswipe_workers/firebase/firebase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
from dataclasses import asdict

from mapswipe_workers import auth
from mapswipe_workers.definitions import CustomError, logger
from mapswipe_workers.utils import gzip_str


class Firebase:
def __init__(self):
self.fb_db = auth.firebaseDB()
self.ref = self.fb_db.reference("")

def save_project_to_firebase(self, project):
# if a geometry exists in projects we want to delete it.
# This geometry is not used in clients.
project.pop("geometry", None)
# save project
self.ref.update({f"v2/projects/{project['projectId']}": project})
logger.info(
f"{project['projectId']} -"
f" uploaded project to firebase realtime database"
)

def save_groups_to_firebase(self, projectId, groups):

# save groups
self.ref.update({f"v2/groups/{projectId}": groups})
logger.info(f"{projectId} -" f" uploaded groups to firebase realtime database")

def save_tasks_to_firebase(self, projectId, groupsOfTasks, useCompression: bool):
task_upload_dict = {}
for group_counter, group_id in enumerate(groupsOfTasks.keys()):
for i in range(0, len(groupsOfTasks[group_id])):
groupsOfTasks[group_id][i].pop("geometry", None)

tasks_list = groupsOfTasks[group_id]
# for tasks of a building footprint project
# we use compression to reduce storage size in firebase
# since the tasks hold geometries their storage size
# can get quite big otherwise
if useCompression:
# removing properties from each task and compress
for task in tasks_list:
task.pop("properties", None)

tasks_list = gzip_str.compress_tasks(tasks_list)

task_upload_dict[f"v2/tasks/{projectId}/{group_id}"] = tasks_list

# we upload tasks in batches of maximum 150 groups
# this is to avoid the maximum write size limit in firebase
if len(task_upload_dict) % 150 == 0 or (group_counter + 1) == len(
groupsOfTasks
):
self.ref.update(task_upload_dict)
logger.info(
f"{projectId} -"
f" uploaded 150 groups with tasks to firebase realtime database"
)
task_upload_dict = {}

def delete_project_draft_from_firebase(self, projectId):
self.ref.update({f"v2/projectDrafts/{projectId}": {}})
logger.info(
f"{projectId} -" f" deleted project draft from firebase realtime database"
)

def save_tutorial_to_firebase(
self, tutorial, groups: dict, groupsOfTasks: dict, useCompression: bool
):
if useCompression:
tasks = {101: gzip_str.compress_tasks(groupsOfTasks[101])}
else:
tasks = {k: [asdict(i) for i in v] for k, v in groupsOfTasks.items()}

groups = {k: asdict(v) for k, v in groups.items()}

tutorialDict = vars(tutorial)

tutorialDict.pop("groups", None)
tutorialDict.pop("tasks", None)
tutorialDict.pop("raw_tasks", None)
tutorialDict.pop("examplesFile", None)
tutorialDict.pop("tutorial_tasks", None)

if not tutorial.projectId or tutorial.projectId == "":
raise CustomError(
"Given argument resulted in invalid "
"Firebase Realtime Database reference. "
f"Project Id is invalid: {tutorial.projectId}"
)

self.ref.update(
{
f"v2/projects/{tutorial.projectId}": tutorialDict,
}
)
self.ref.update(
{
f"v2/groups/{tutorial.projectId}": groups,
}
)
self.ref.update(
{
f"v2/tasks/{tutorial.projectId}": tasks,
}
)

logger.info(f"uploaded tutorial data to firebase for {tutorial.projectId}")

def drop_tutorial_draft(self, tutorialDraftId: str):
self.ref.update({f"v2/tutorialDrafts/{tutorialDraftId}": {}})
Loading