Code Monkey home page Code Monkey logo

python-mapswipe-workers's Introduction

MapSwipe Back-End

MapSwipe is a mobile app that lets you search satellite imagery to help put the world's most vulnerable people on the map. If you are new to MapSwipe it might be good to have a look at the FAQs first.

The MapSwipe Back-End consists of a number of components:

  1. Firebase Project
  2. MapSwipe Workers
  3. Postgres Database
  4. Manager Dashboard
  5. API

Please refer to the documentation for more information: https://mapswipe-workers.readthedocs.io/

Resources

Development Setup

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

Contributing Guidelines

Feature Branch

To contribute to the MapSwipe back-end please create dedicated feature branches based on the dev branch. After the changes create a Pull Request of the feature branch into the dev branch on GitHub:

git checkout dev
git checkout -b featureA
# Hack away ...
git commit -am 'Describe changes.'
git push -u origin featureA
# Create a Pull Request from feature branch into the dev branch on GitHub.

Note: If a bug in production (master branch) needs fixing before a new versions of MapSwipe Workers gets released (merging dev into master branch), a hotfix branch should be created. In the hotfix branch the bug should be fixed and then merged with the master branch (and also dev).

Style Guide

This project uses black and flake8 to achieve a unified style.

Use pre-commit to run black and flake8 prior to any git commit. pre-commit, black and flake8 should already be installed in your virtual environment since they are listed in requirements.txt. To setup pre-commit simply run:

pre-commit install

From now on black and flake8 should run automatically whenever git commit is executed.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details

Authors

See also the list of contributors who participated in this project.

Acknowledgements

  • Humanitarian organizations can't help people if they can't find them.

python-mapswipe-workers's People

Contributors

adityakhatri avatar danbjoseph avatar ddevault avatar eljocho avatar ericboucher avatar faevourite avatar frozenhelium avatar gcaria avatar hagellach37 avatar itisacloud avatar jhollowe avatar k9845 avatar kopitek8 avatar laurents avatar masain avatar mathcass avatar matthias-schaub avatar matthiasschaub avatar maze2point0 avatar mcauer avatar ofr1tz avatar poulamb avatar puranban avatar samshara avatar shreeyash07 avatar tahirau avatar thenav56 avatar tnagorra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-mapswipe-workers's Issues

Change from mysql to psql

As mentioned in #42 we changed our database infrastructure from mysql to psql for easier json handling.
Therefore we have to:

  • setup postgres databases (dev is already done)
  • add postgres credentials to cfg
  • go over the scripts rewrite the db connection, change the variable naming

Update proccess - no users in firebase

12-20 17:28:12 WARNING === === === === ===>>> started update <<<=== === === === ===
12-20 17:28:12 WARNING ALL - get_environment - use development instance
12-20 17:32:04 WARNING === === === === ===>>> started update <<<=== === === === ===
12-20 17:32:04 WARNING ALL - get_environment - use development instance
12-20 17:32:05 ERROR ALL - run_mapswipe_workers - got exception
Traceback (most recent call last):
File "run_mapswipe_worker.py", line 63, in
b.run_update(args.modus, args.filter, args.output)
File "/python-mapswipe-workers/mapswipe_workers/basic/BaseFunctions.py", line 365, in run_update
update_users_postgres(firebase, postgres)
File "/python-mapswipe-workers/mapswipe_workers/basic/BaseFunctions.py", line 225, in update_users_postgres
for user in users:
TypeError: 'NoneType' object is not iterable
12-20 17:32:05 WARNING XXX XXX XXX XXX XXX>>> errored update <<<XXX XXX XXX XXX XXX
12-20 17:32:05 WARNING zZz zZz zZz zZz zZz>>> sleep for 900 seconds <<<zZz zZz zZz zZz zZz

add another process "archive" to run_mapswipe_worker

Currently projects and all groups per project stay in firebase. Also imports stay in firebase forever. An "archive" function would help us to reduce the storage costs in firebase, increase firebase performance.

Mapswipe Workers will look at all projects in Firebase and archive them based on certain terms:

  • archive flag = true (The idea is that project managers can set an archive flag to true if they decide the project is finished)

Additionally we might think about doint this automatically:

  • project.progress = 100%
  • No new results for e.g. 3 months

Note: Imports (projects draft) will now be deleted after successfull creation of the project

The archive function will:

  • set status of a project to archived
  • will delete all tasks and groups in firebase

Add linter

Now that Travis.ci is setup we should add a linter to automatically run on the Python code.
Note that this will probably require a lot of formatting changes in the existing code.

HTTPError: 401 Client Error: Unauthorized for url

While running our scripts we got this error:

google-mapswipe-workers: run_export.py: error occured
(<class 'requests.exceptions.HTTPError'>, HTTPError(HTTPError('401 Client Error: Unauthorized for url: https://msf-mapswipe.firebaseio.com/projects.json',), '{\n  "error" : "Unauthorized request."\n}\n'), <traceback object at 0x7fa90708b1c8>)

It seems is if it occurs randomly, after several minutes the error didn't occur anymore.

This seems to been a know issue with oauth2client, which us used by pyrebase: thisbejim/Pyrebase#117

Setup new mysql structure

In order to handle additional project types we need to restructure the mysql db.
We need a project table with attributes for:

  • id(integer)
  • name(text)
  • objective(lookFor)(text)
  • project type(integer)
    • in order to distinguish results from different project types like built area or footprint.

part of #38
For the results table base attributes are:

  • Task-id
  • User-Id
  • Project-Id
  • Timestamp

Specific per project type:
Built area project:

  • result (integer of user interaction)
    Footprint project:
  • result (integer)
  • footprint geometry (geom)
    • We will store if necessary every task of a specific project type, for the export we can then join the result with the task geometry

project progress/history

At the moment we store the progress of projects which is the amount of tasks which have been evaluated divided by the amount of tasks which need to be evaluated in a text file on the nginx server.
In order to keep all data in one central place like mentioned in #49 we will setup an additional table with timestamp, project-, and contributors stats.

setup postgres as main data storage

We concluded that it's worth to better differentiate the functionalities of our data stores.
Firebase is for communication with the mapswipe client app only. Right now we store everything except results there. We want to shift that storing functionality completely over to the postgres database. Firebase remains the database for active projects.

Improvements needed on postgres side:

  • Setup tables for group information (right now only tasks are stored). This table provides information on which project the groups are assigned to, the group id, completed and needed count.
  • At the moment we store not all project information we have in firebase
  • Additionally we add an attribute to each project if it is archived

Import process - Save input geometries of projects

We create a kml or geojson during the import. This file contains the geometries for each project. The file should be stored in data/input_geometries folder and should use a better naming of the files.

e.g. data/input_geometries/input_geometries_{id}.kml or data/input_geometries/input_geometries_{id}.geojson

make logging easier to read

Currently logging info does not follow a common format.

E.g.:

2018-10-25 23:33:52,823 - root - WARNING - got project contributors from mysql for project: 13531

2018-10-25 23:33:53,006 - root - WARNING - log contributors to file for project 13531 successful

project id should be easier to spot, as well as the corresponding function

delete projects

use a single sql statement to delete information in postgres instead of several functions

come up with a mechanism to delete project data in firebase and being able to roll back.

do we still need `setup_database_tables.py`?

When using docker the database tables are created by the following script: docker/postgres_create_tables.sql

Is there another use case for setup_database_tables.py?

We should remove it entirely or link to the existing sql script.

transfer results script is too slow for many results

When many results are uploaded to the mapswipe database, we may not be able to import them quick enough.

Potential bottlenecks are:

  • insert into mysql database and check for duplicates
  • remove entries in firebase

Add tests

Now that Travis.ci is setup we should add extensive tests and aim for an 80% test coverage.

adjust import of new project type

When importing a new project, we need to adjust some things.

In project:

  • add the url to the tileserver for each project

In Tasks:

  • remove projectId from tasks, since it's already in the groups (project id can be generated from the task_id, and is also in the group)
  • feature_id --> featureId

Testing - create script which simulates setup of a project

  • we need to make sure to have an user
  • the script should take an import and insert it to firebase
  • the trigger the run_import function to import the project in firebase, create groups etc and do the stuff in postgres
  • simulate results for the imported project
  • get projects
  • get group per project
  • randomly generate results for tasks
  • run transfer results
  • run update
  • run export

API - Landing page

The Api should have a landing page with description and link to the documentation.

Server transition (Python-Mapswipe-Workers 1.5)

  • Setup a new server (Google Compute Engine)
    • Configure server backup
    • Install Python-MapSwipe-Workers (do NOT run the containers)
  • data transition to new server
    • Import Firebase data into Postgres (#66)
    • import, projects, groups
    • compare amount of rows for each table firebase vs. psql
    • Stop import process on the old server
  • Start import process on the new server
    • update web import formular
    • generate a test import in prodcution instance
    • test import was successfully created in production instance
  • Start transfer results on the new server
    • Migrate MySQL data (results) to Postgres (#65)
    • test transfer results with results generated from the production app
    • stop transfer results on the old server
    • transfer all results from mysql to postgresql which have not been transfered yet
    • start transfer results process on the new server
  • Start update on the new server
    • test update for a few old projects
    • update completed count of groups in postgres
    • update all projects in firebase using the new workers
    • stop update on the old server
    • start update process for active projects
  • Start export process on the new server
    • test export for a few old projects
    • manually generate exports for all projects
    • stop export on the old server
    • start export process for all active projects
    • make sure http://api.mapswipe.org/ points to the correct ip-address
  • Redirect API domain name to new server
  • Shutdown old server

create index.html for nginx

we should create an index.html file for the nginx server
this html file will provide a link to the documentation of the mapswipe api at read the docs

and furthermore show all available data sets in the respective folders.

currently there is an api documenation for the mapswipe api in a google doc. This should be replaced with the documenation within this github repository

API - Folder structure

Show all available data sets in the respective folders

+-- index.html
+-- stats.json
+-- projects.json
+-- users.json
+-- input_geometries
|     +-- input_geometries_1.kml
|     +-- input_geometries_3.geojson
+-- progress
|      +-- progress_1.json
|      +-- progress_3.json
+-- results
|      +-- results_1.json
|      +-- results_3.json
+-- tmp
|      +-- results.json
|      +-- raw_tasks.txt

Add more information to error messages

Currently our error messages do not provide much detail.

ERROR    FileNotFoundError processing TIN / NPI message! In <module>, the following error happened -
 2 at line 473.

Let's change this.

Load tasks in mysql too

We will change the import of projects, that every task is also stored in mysql.
This will help us to create exports of results joined by task specific attributes (like geometries)

setup docker for mapswipe workers

@ericboucher already started to setup docker for the mapswipe workers.

Currently we are not using docker at the backend. What's the current status and what stops us from solving this issue?

Seperation of development and production configuration necessary?

Development and production instance of MapsSwipe Workers should not run in the same container. Therefor there is no need for a shared configuration file (no need for the fields "dev_psql" and "dev_firebase") and also not for the dev_firebase_admin_auth() function and the dev_psqlDB class in the auth module.

Adjust path/ import of config in transfer results module

When running the transfer of the results from firebase to a mysql instance the following error occurs:

Unable to load configuration file at ./cfg/config.cfg. Exiting.
Traceback (most recent call last):
  File "transfer_results.py", line 12, in <module>
    from cfg.auth import firebase_admin_auth
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
  File "/data/environments/mapswipe_workers/lib/python3.4/site-packages/mapswipe_workers-0.0.1-py3.4.egg/cfg/auth.py", line 14, in <module>
FileNotFoundError: [Errno 2] No such file or directory: './cfg/config.cfg'

The same errors occurs for the import module, export module and the update module.

Missing Documentation

  • basic.BaseFunction.update_users_postgres()
  • basic.BaseFunction.run_delete()
  • basic.BaseFunction.run_archive()
  • basic.BaseFunction.run_export()
  • basic.BaseGroup
  • basic.BaseProject.set_groups_postgres()
  • basic.BaseProject.set_import_postgres()
  • basic.BaseProject.delete_tasks_postgres()
  • basic.BaseProject.delete_groups_postgres()
  • basic.BaseProject.delete_import_postgres()
  • basic.BaseProject.update_project()
  • basic.BaseProject.set_project_progress_postgres()
  • basic.BaseTask
  • ProjectTypes.BuildArea.BuildAreaGroup
  • ProjectTypes.BuildArea.BuildAreaProject.aggregate_results()
  • ProjectTypes.BuildArea.BuildAreaTask
  • ProjectTypes.BuildArea.GroupingFunctions
  • ProjectTypes.BuildArea.TileFunctions
  • ProjectTypes.Footprint.FootprintGroup
  • ProjectTypes.Footprint.FootprintProject.aggregate_results()
  • ProjectTypes.Footprint.FootprintTask
  • ProjectTypes.FootprintGroupingFunctions
  • utils.errorhandling
  • utils.slack
  • utils.migrate_mysql_psql
  • basic.auth.firebase_admin_auth()
  • basic.auth.dev_firebase_admin_auth()
  • basic.get_api_key()
  • basic.get_tileserver_url()
  • basic.get_submission_key()
  • basic.mysqlDB
  • basic.dev_mysqlDB
  • basic.psqlDB
  • basic.dev_psqlDB
  • basic.BaseFunctions.init_import()
  • basic.BaseFunctions.init_project()
  • basic.BaseFunctions.project_exists()
  • basic.BaseFunctions.project_exists_firebase()
  • basic.BaseFunctions.project_exists_postgres()
  • basic.BaseFunctions.run_transfer_results()
  • basic.BaseFunctions.delete_project_firebase()
  • basic.BaseFunctions.delete_project_postgres()
  • basic.BaseFunctions.delete_local_files()
  • basic.BaseFunctions.BaseImport.BaseImport
  • basic.BaseFunctions.BaseImport.set_import_postgres()
  • basic.BaseFunctions.BaseImport.set_project_postgres()
  • basic.BaseFunctions.BaseImport.set_groups_postgres()
  • basic.BaseFunctions.BaseImport.set_tasks_postgres()
  • basic.BaseFunctions.BaseImport.set_project_firebase()
  • basic.BaseFunctions.BaseImport.set_groups_firebase()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.