Skip to content

Commit

Permalink
v4.5.2: 📦 Fix containerization (#16)
Browse files Browse the repository at this point in the history
* add docker-compose.yml and update docker configuration

* update .gitignore

* update translations and babel conf

* add docstrings

* Move scripts

* Update README
  • Loading branch information
fabriziomiano committed Feb 27, 2021
2 parents ebedaf9 + fefda72 commit cdaa4c1
Show file tree
Hide file tree
Showing 22 changed files with 331 additions and 800 deletions.
4 changes: 2 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ node_modules/
# git / github stuff
.git/
.github/
*.gitignore
.gitignore
*.md
LICENSE
docs/
previews/

# misc
scripts/
*.log
*.json
*.bat
*.sh
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ VAX_COLLECTION=changeme
VAX_SUMMARY_COLLECTION=changeme
GH_WEBHOOK_SECRET=changeme
API_KEY=changeme
REDIS_URL=changeme
REDIS_URL=changeme
PORT=5000
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ dpc*.json

# env vars
.env

# translations
*.pot
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# maintainer stuff
LABEL maintainer='[email protected]'

#Grab the latest alpine image
FROM python:3.8

Expand All @@ -10,9 +13,6 @@ RUN pip3 install --upgrade pip && \
ADD . /opt/app/
WORKDIR /opt/app

# Uncomment for development purposes only as $PORT is automatically set by Heroku
# ENV PORT 5000

# Run the app. CMD is required to run on Heroku
# crate the collections on DB before running the gunicorn server
CMD python3 -m flask create-collections && \
Expand Down
133 changes: 75 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# COVID-19 Italy Monitor | [\#StayAtHome](https://twitter.com/hashtag/StayAtHome)

Versione Italiana [qui](https://github.com/fabriziomiano/covidashit/blob/main/README_IT.md)
[![Awesome](https://awesome.re/badge.svg)](https://github.com/soroushchehresa/awesome-coronavirus#applications-and-bots)
[![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://github.com/ellerbrock/open-source-badges/)
[![Made with Pthon](https://img.shields.io/badge/Made%20with-Python-1f425f.svg)](https://www.python.org/)

A simple dashboard to display and monitor the official data of the COVID-19 outbreak in Italy released by the [Civil Protection Dept.](https://github.com/pcm-dpc/COVID-19) and updated on a daily basis.
![alt_text](https://raw.githubusercontent.com/fabriziomiano/covidashit/main/previews/mockup.png)

## Previews
Versione Italiana [qui](https://github.com/fabriziomiano/covidashit/blob/main/README_IT.md)

### Pandemic View
![alt_text](https://raw.githubusercontent.com/fabriziomiano/covidashit/main/previews/pandemic.png)
A simple dashboard to display and monitor the official data of the COVID-19 outbreak in Italy released by the [Civil Protection Dept.](https://github.com/pcm-dpc/COVID-19) and updated on a daily basis.

### Vaccines View
![alt_text](https://raw.githubusercontent.com/fabriziomiano/covidashit/main/previews/vaccines.png)
**The app is deployed on Heroku [here](https://www.covidash.it/)**

##### The app is deployed on Heroku → [here](https://www.covidash.it/)
**Pandemic data from the [official CP Dept repository](https://github.com/pcm-dpc/COVID-19/)**

##### The data is taken from the official CP Dept repository → [here](https://github.com/pcm-dpc/COVID-19/blob/master/dati-json/dpc-covid19-ita-andamento-nazionale.json)
**Vaccine data from the [official open-data repository/](https://github.com/italia/covid19-opendata-vaccini)**


## For developers
Expand All @@ -33,57 +33,69 @@ Additionally, mongo collections must be updated on a daily basis. The Flask cont
update the DB every time the `master` branch of the CP Dept repository is updated, via a GitHub webhook (see the GitHub workflow [here](https://github.com/fabriziomiano/COVID-19/blob/master/.github/workflows/merge-upstream.yml)).
Ultimately, the webhooks for the following APIs must be set on the CP forked repository:

* `/update/national`
* `/update/national/series`
* `/update/national/trends`
* `/update/regional`
* `/update/regional/breakdown`
* `/update/regional/series`
* `/update/regional/trends`
* `/update/provincial`
* `/update/provincial/breakdown`
* `/update/provincial/series`
* `/update/provincial/trends`
* `/update/vax/`
* `/update/vax/summary`

#### Set up a local version
* `POST /update/national`
* `POST /update/national/series`
* `POST /update/national/trends`
* `POST /update/regional`
* `POST /update/regional/breakdown`
* `POST /update/regional/series`
* `POST /update/regional/trends`
* `POST /update/provincial`
* `POST /update/provincial/breakdown`
* `POST /update/provincial/series`
* `POST /update/provincial/trends`
* `POST /update/vax/`
* `POST /update/vax/summary`

### Local deployment (DEV)
* create and activate a virtual environment [(follow this)](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/)
* install the requirements in `requirements.txt`

The `.env` file contains all the env vars needed by the webapp.
In particular, the `MONGO_URI` and the various collection names string must be set.
Before the Flask server is started, but after the virtual environment has been activated,
the DB must be populated.
For this purpose a Flask CLI, which runs a very basic ETL procedures that populates the various collections, is included
and can be run with
For this purpose a Flask CLI, that populates the various collections, is included.
This, with a very basic ETL procedure, will populate the various collections on
the DB with the official data released by the Civil Protection Dept.

`python -m flask create-collections`

This, with a very basic ETL procedure, will populate the various collections on the DB with the official data released by the Civil Protection Dept.


##### Development
Clone the repo, `cd` into it, and
Clone the repo, `cd` into it, activate the virtual environment, and run the procedure
```shell
flask create-collections
```
then run the worker
```shell
celery -A celery_worker.celery worker
```
$ export FLASK_ENV=development
$ export FLASK_DEBUG=1
$ python -m flask run
and, in a new shell, run the application server
```shell
flask run
```
Flask will be listening at [http://127.0.0.1:5000](http://127.0.0.1:5000)

##### Production
The provided Dockerfile is ready to be used to deploy the app on Heroku.
To test the production environment locally, uncomment L17, add the
above-mentioned env variables, and run:
### Local deployment (PROD)
First, replace the value of `APPLICATION_ENV` in `.env` with `production`
#### Procfile
to test the `Procfile` configuration, Simply run the heroku CLI
```shell
heroku local
```
$ docker build --tag covidashit:latest .
$ docker run --name covidashit -d -p 80:5000 covidashit

#### Docker
To test the containerization locally spawn the container with:
```shell
docker-compose up -d
```

The docker container will be listening at [http://127.0.0.1](http://127.0.0.1)
The docker container will be listening at `http://127.0.0.1:PORT` with `PORT`
being set in the `.env` file

Stop it with
```shell
docker-compose down
```

##### Additional note
### Deployment on Heroku
The app can be deployed on Heroku either as a docker container or simply using the Procfile

## Plots API
Expand Down Expand Up @@ -140,32 +152,37 @@ Padova, Rovigo, Treviso, Venezia, Verona, Vicenza]
```

### Examples
#### A national plot
* `/api/plot?data_type=national&varname=<varname>`
#### A regional plot
* `/api/plot?data_type=regional&area=<region>&varname=<varname>`
#### A regional plot
* `/api/plot?data_type=provincial&area=<province>&varname=[nuovi_positivi,nuovi_positivi_ma,totale_casi]>`
#### National plot
`GET /api/plot?data_type=national&varname=<varname>`

#### Regional plot
`GET /api/plot?data_type=regional&area=<region>&varname=<varname>`

##### To get the base64-encoded image in a JSON response
###### JSON
#### Provincial plot
`GET /api/plot?data_type=provincial&area=<province>&varname=[nuovi_positivi,nuovi_positivi_ma,totale_casi]>`

###### Request

#### To get the base64-encoded image in a JSON response
#### JSON

#### Request
```
curl --request GET \
--url 'https://www.covidash.it/api/plot?data_type=national&varname=totale_casi'
```

###### Response
`{
#### Response
```json
{
"errors":[],
"img":"iVBORw0KGgoAA...",
"status":"ok"
}`
}
```

#### To download the file
###### Request
```
#### Request
```shell
curl --request GET \
--url 'https://www.covidash.it/api/plot?data_type=national&varname=totale_casi&download=true' \
--output plot.png
Expand Down
Loading

0 comments on commit cdaa4c1

Please sign in to comment.