codeforIATI / iati-datastore

An open-source datastore for IATI data with RESTful web API providing XML, JSON, CSV plus ETL tools
https://datastore.codeforiati.org
Other
1 stars 1 forks source link
iati

IATI Datastore Classic

Build Status Coverage Status License: AGPL v3

Introduction

The International Aid Transparency Initiative (IATI) aims to make information about aid spending easier to access.

IATI maintains the IATI Standard and keeps a Registry of IATI data.

The IATI Datastore was originally built in 2013 by the Open Knowledge Foundation. Code for IATI has updated the software to use Python3 and modern dependencies. Over time, we will include other bugfixes and feature improvements. Our fork of the software is called IATI Datastore Classic.

A public instance is available here:

https://datastore.codeforiati.org

Requirements

You will need Redis, Postgres (version >= 12), Python 3, pip and develpment libraries (for libpq, libxml2 and libxslt) to run the full setup. For example, on Ubuntu:

sudo apt-get install postgresql redis-server python3 python3-pip libpq-dev libxml2-dev libxslt-dev libevent-dev python3-dev

Installing for development

# Clone the source
git clone https://github.com/codeforIATI/iati-datastore.git

# Install development dependencies
pip install -r requirements_dev.txt

# Run the tests  (You need to create a postgres database for these to use and set the connection string)
TEST_SQLALCHEMY_DATABASE_URI=... nosetests iati_datastore

# Create a new PostgreSQL database
sudo -u postgres psql -c "CREATE DATABASE iati_datastore"

# Set an environment variable for `IATI_DATASTORE_DATABASE_URL` linking to the database created
export IATI_DATASTORE_DATABASE_URL=postgres:///iati_datastore

# Create the db tables
iati db upgrade

# Note: To create the tables the new database may need access privileges granted to your system user
# See http://dba.stackexchange.com/questions/117109/how-to-manage-default-privileges-for-users-on-a-database-vs-schema/117661#117661
sudo -u postgres psql -c "CREATE USER [SYSTEM USER]"
sudo -u postgres psql -c "GRANT ALL ON DATABASE iati_datastore TO [SYSTEM USER]"

# Create the front page
iati build-query-builder --deploy-url=http://127.0.0.1:5000

# Start the process of grabbing the source data
iati crawler download-and-update

# Start a development server – this should be run in a seperate terminal window
iati run

# Run a worker. This will download and index the datafiles
iati queue background

# The progess of the worker can be checked using:
iati crawler status

# A local API is available at: http://127.0.0.1:5000

Development with vagrant

A Vagrant box is also provided. vagrant up as normal, then vagrant ssh.

# Run the tests
nosetests --exe iati_datastore

# Create the db tables
iati db upgrade

# Create the front page
iati build-query-builder --deploy-url=http://127.0.0.1:5000

# Start the process of grabbing the source data
iati crawler download-and-update

# Start a development server – this should be run in a seperate terminal window
iati run --host 0.0.0.0

# Run a worker. This will download and index the datafiles
iati queue background

# The progess of the worker can be checked using:
iati crawler status

# A local API is available at: http://127.0.0.1:5000

Python Profiling

If you want to profile some of the code, follow these additional instructions:

Edit iati_datastore/iatilib/frontend/app.py, and add an import and change the return of create_app (Do not check these changes in!):

from werkzeug.middleware.profiler import ProfilerMiddleware

def create_app(config_object='iatilib.config.Config'):
    ... as usual ....
    return  ProfilerMiddleware(app, profile_dir='/vagrant/profiles')

Run:

mkdir -p profiles
pip install snakeviz
iati run --host 0.0.0.0

In another window run:

snakeviz  -H 0.0.0.0 -p 5555 -s   /vagrant/profiles/

Go to a URL as normal.

You should see files appear in the profiles directory.

Go to http://127.0.0.1:5555/snakeviz/%2Fvagrant%2Fprofiles and you can browse them with pretty graphs.

To turn this off, just undo your changes to iati_datastore/iatilib/frontend/app.py.

Database Logging

As root, create and edit the file /etc/postgresql/12/main/conf.d/log.conf and put in:

log_min_duration_statement = 0

Restart Postgres:

sudo /etc/init.d/postgresql restart

Open a new window and see queries made as other tasks are carried out:

sudo tail -f /var/log/postgresql/postgresql-12-main.log

Note if you do something like a data import this will include A LOT of queries - make sure you turn this off when you don't need this any more!

To turn this off, just delete the config file you created and restart postgres again.

Deploying with nginx

Deploying with apache

Updating activities after changing import code

Generation of Documentation

API documentation in the docs folder is generated using Sphinx.

iati build-docs

Generation of the front page and query builder

iati build-query-builder

By default, the query builder will look for files served from http://127.0.0.1:5000 - if you would like to point it elsewhere (e.g. to https://datastore.codeforiati.org) then you should add the argument deploy-url:

iati build-query-builder --deploy-url https://datastore.codeforiati.org

Python Requirements

To update requirements, use pip-compile.

Make sure you are in a Python 3.8 virtual environment, and run:

pip install pip-tools
pip-compile requirements.in
pip-compile requirements_dev.in

Then in the 2 requirements*.txt files, look for the line:

-e file:///vagrant/iati_datastore

And edit them to:

-e iati_datastore

I18N - Flask Application

cd iati_datastore/iatilib

To add a new locale:

pybabel extract -F babel.cfg -o messages.pot .
pybabel init -i messages.pot -d translations -l fr

If strings change in app and you want to reparse the app for new strings, run:

pybabel extract -F babel.cfg  -k lazy_gettext -o messages.pot .
pybabel update -i messages.pot -d translations

When .po files change with new content, or when deploying the app:

pybabel compile -d translations