Git Product home page Git Product logo

measuring-stations-api's Introduction

measuring-stations-api

API to manage measuring stations and the data they collect

Overview

This project provides a REST API to store and retrieve sensor data from multiple measuring stations. The REST API is implemented using FastAPI and the data is stored as timeseries data in an InfluxDB database.

Each measuring station has an ID, an address and a list of sensors. Each sensor takes a measurement once per minute which has a timestamp and a value within the range [0, 100].

Running the project

After cloning the repo, go to the project directory and create a virtual environment for python and activate it. The commands vary depending on the operating system and the shell, so check the documentation if you are unsure (https://docs.python.org/3/library/venv.html). For example, for Linux with bash:

python -m venv .venv
source .venv/bin/activate

Then install the required dependencies:

python -m pip install -r requirements.txt

Creating the database

Create and start InfluxDB in a docker container:

docker run \
    --name measuring-stations-influxdb2 \
    --publish 8086:8086 \
    --mount type=volume,source=measuring-stations-influxdb2-data,target=/var/lib/influxdb2 \
    --mount type=volume,source=measuring-stations-influxdb2-config,target=/etc/influxdb2 \
    --env DOCKER_INFLUXDB_INIT_MODE=setup \
    --env DOCKER_INFLUXDB_INIT_USERNAME=ADMIN_USERNAME \
    --env DOCKER_INFLUXDB_INIT_PASSWORD=ADMIN_PASSWORD \
    --env DOCKER_INFLUXDB_INIT_ORG=ORG_NAME \
    --env DOCKER_INFLUXDB_INIT_BUCKET=BUCKET_NAME \
    --env DOCKER_INFLUXDB_INIT_ADMIN_TOKEN=AUTH_TOKEN \
    --detach \
    influxdb:2

This will create a docker container named measuring-stations-influxdb2 and create 2 volumes measuring-stations-influxdb2-data and measuring-stations-influxdb2-config. It uses the port 8086 (a browser-based GUI can be accessed at http://localhost:8086, but it's not needed for our project).

To see the live logs, run docker logs -f measuring-stations-influxdb2.

Once created, you can stop the container with docker stop measuring-stations-influxdb2 and start it again with docker start measuring-stations-influxdb2.

To remove it, run docker rm measuring-stations-influxdb2. To remove the volumes, run docker volume rm measuring-stations-influxdb2-config measuring-stations-influxdb2-data.

Running the API server

Finally start the API server:

  • Development mode: fastapi dev main.py
  • Production mode: fastapi run main.py

Usage

With the server running, you should be able to access the API at http://localhost:8000 and the API documentation at http://localhost:8000/docs or alternatively at http://localhost:8000/redoc. The endpoints are documented there.

Here are a few examples using curl.

Create Measuring Station

Measuring Stations can be created using the endpoint /measuring-stations/{station_id} with the PUT method. For example:

curl -X 'PUT' 'http://127.0.0.1:8000/measuring-stations/station-1?address=Paris%2C%20France' -H 'accept: application/json'

Retrieve Measuring Station

Use /measuring-stations/{station_id} with GET, e.g.:

curl -X 'GET' 'http://127.0.0.1:8000/measuring-stations/station-1' -H 'accept: application/json'

Upload Sensors Data

Use /measuring-stations/{station_id}/sensor-data with PUT, e.g.:

curl -X 'PUT' \
  'http://127.0.0.1:8000/measuring-stations/station-1/sensor-data' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "sensor-1": [
    {
      "ts": 1718628540,
      "value": 32.0
    }
  ]
}'

Retrieve Measurements

Use /measuring-stations/{station_id}/sensor-data/{sensor_id} with GET, e.g.:

curl -X 'GET' \
  'http://127.0.0.1:8000/measuring-stations/station-1/sensor-data/sensor-1?start=0&stop=1722866400' \
  -H 'accept: application/json'

Next steps and ideas for improvement

  • Write data batchwise to DB instead of point by point
  • Write tests and run them in the CI
  • Replace hardcoded values and make them configurable
  • Benchmarks, check how many requests and data points the system can handle per second
  • Authentication for the REST API endpoints
  • Pre-commit hooks
  • Create a docker image for the API and a compose file for the 2 services (InfluxDB, API) so that the entire application can be started and stopped with a single command

measuring-stations-api's People

Contributors

fitzoreilly avatar

Watchers

 avatar

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.