A simple microservice to keep pushing in metrics into a timeseries database.
Why?
- Needed something for tracking various hourly/weekly/daily etc numbers
- Wanted to write a fun learning project to use fastapi. Many of the examples online I saw didn't have code explaining master-detail schema and usage of async db apis in one project.
- I wanted to do a useful web-api swith
sqlite
, preferably async. - Straight-forward to run and debug! Can be put into docker if needed.
What is with this name?
It is an abbreviation of Meter + Write and not a spelling mistake for meteorite.
- Metric has a code, name some limits and belongs to an org
- Org is identified by a authentication key. Note that keys and orgs are only in config and not manageable by APIs. rendered by mrindoc is a good place to start
- Meters are recorded to a Metric.
Best way to peruse those is to clone repo and deploy this and test it with auto generated docs.
I am assuming you've installed Anaconda Python in the PATH. If not, just pip
will do fine.
conda create -n py82 python=3.8.2
conda activate py82
pip install -r requirements.txt
Create a settings file named .env
in this folder. Sample contents
are below.
database_url="sqlite:////tmp/meterite.db"
api_key_name="x-auth-token"
cookie_domain="localtest.me"
api_tokens={"123456": "org_01", "567890": "org_02"}
Then, start uvicorn as below.
uvicorn app.main:app
# add `--reload` to the command above to do live debugging - useful in VS Code
And now navigate to http://localtest.me:8000/docs?x-auth-token=123456
An org is a group of metrics; think of it like a company in a multi-tenanted design. You can have multiple auth tokens pointing to same org (if you fancy).
You can load sample data by running
scripts/load_sample.sh
- Harden the code for exceptions like DB not opening, query failed etc.
- Raise events or background tasks when meters are recorded with values outside of min/max settings. We should've some mechanism to plugin handlers outside the repo to this.
- Add separate background tasks to mark readings as anomalies; fbprophet is pretty neat for that.
- Setup with gunicorn as well.
- Provide a UI for configuration. Seriously? No!