SPRITSTAT is a service that collects fuel price statistics for user-specified locations in Austria.
It uses the Spritpreisrechner API provided by the government regulator E-Control to collect the lowest prices for a location. This API returns all petrol stations that provide one of the lowest five prices for any given location at the current time. As only the lowest five price points are provided, it isn't possible to collect full price statistics (like maximum, median, or average prices).
Two different location types are supported by the Spritpreisrechner API:
- coordinates
- regions, which can be either states or districts.
The service provides both region types to the user, but the coordinates type is wrapped by address lookup functionality, that takes an address and uses the Google Geocoding API to get the coordinates for any given address in Austria. For each location a user creates, the five lowest prices are collected hourly and the minimum, maximum, average and median is calculated. If any of the minimum, maximum, average or median value has changed the price point is stored in the database. Of these values currently only the minimum value is exposed to the user as the other values aren't actually the absolute maximum, average or median price values. Usually there would be other petrol stations that offer higher prices as the ones provided by the Spritpreisrechner API. As this is confusing to the user these values are currently only stored, but might be made available as an "advanced" feature at a later point.
The frontend is a single page application implemented in Typescript / React and uses Bulma as CSS framework. Babel / Webpack is used to build the frontend code. Testing is currently done in an end-to-end fashion using Cypress. You can find the frontend implementation in the frontend/ directory.
The backend is implemented as a session-authenticated API using Django and Django Rest Framework. As database PostgreSQL is used. You can find the backend implementation in the project root directory. Notably, the user management API is found in the users/ and the implementation of the payload API in the spritstat/ directory.
The application is automatically tested and deployed to Amazon Web Services using GitHub Actions. To archive this, the application is first packaged using Docker and stored in the GitHub Container Registry. Deployment of the packaged application to the Amazon EC2 virtual machine is then done with Ansible. As database a hosted version of PostgreSQL in Amazon RDS is used.
The GitHub Actions workflows used to deploy the application can be found in the .github/workflows/ directory. The dockerfile and complementary files that are requierd to build the Docker image can be found at docker/ and the Ansible playbook and other files used to deploy the application can be found in directory /deployment.
While there is no reason that the application shouldn't run on any other operating system, it has only been tested with Linux. So, the following instructions assume that the operating system is Linux.
The application backend requires Python 3 to run. While most likely any maintained Python 3 version will work, I
recommend installing Python 3.9 as it has only been tested with this version. If your system Python version is
different, Python 3.9 can be easily installed using Conda. If Conda isn't used, I recommend using
a virtual environment instead (python -m venv venv && . venv/bin/activate
).
Install dependencies: pip install -r requirements_dev.txt
The easiest way do setup required services like the PostgreSQL database locally is to use Docker. You can find
installation instructions here. Make sure that the user is part of the
docker
group, otherwise all docker
commands need to be executed with sudo
.
A number of environment variables are required to execute the tests. The easiest way to do this is to create an
environment file .env
in the project directory that contains at least these environment variables (MACHINE IP
has
to be replaced by the IP :
POSTGRES_PASSWORD=postgres
DJANGO_POSTGRES_USER=postgres
DJANGO_POSTGRES_PASSWORD=postgres
DJANGO_POSTGRES_DATABASE=postgres
DJANGO_SECRET=notsecret
DJANGO_DEBUG=1
DJANGO_SUPERUSER_USERNAME=admin
DJANGO_SUPERUSER_EMAIL=admin@email.com
DJANGO_SUPERUSER_PASSWORD=admin
The frontend requires the npm package manager to run (it has only been tested with version 8). Yarn package manager will work just as well, but you have to adapt the commands below.
Install frontend requirements:
- Change to the frontend directory:
cd frontend
- Install dependencies:
npm install
Translation is done by Django for the email templates and by react-intl in the frontend.
In the project directory:
- Execute
django-admin makemessages -a -i frontend -i venv -e html,txt
to create the source language file for English atlocale/en/LC_MESSAGES/django.po
- Translate the content of the source language file
- Check if everything is translated by executing
django-admin checkmessages
- Execute
django-admin compilemessages
to compile the messages
In the frontend
directory:
- Execute
npm run trans
to create the source language file for English atfrontend/translation/locales/en.json
. The command will print added/deleted/changed text ids. Also, if a changed text has been found the previous translation is stored as "obsoleteMessage" in the source language file - Translate the content of the source language file
- Check if everything is translated by executing
npm run trans:manage --check
- Execute
npm run trans:compile
to compile the messages
In the project directory:
- Start local PostgreSQL server:
docker run --env-file .env -d -p 127.0.0.1:5432:5432 postgres:12
- Execute backend tests:
python manage.py test
- Change to frontend directory:
cd frontend
- Build frontend:
npm run build:dev
- Execute tests:
npm run test
For local testing Docker Compose is helpful, as it allows for easy deployment of a
local test version with one command. The deploy.sh
script depends on Docker Compose, so make sure you have installed
it.
- Change to development directory:
cd dev
- Build and deploy the application:
./deploy.sh
The application is then available at https://localhost.