9

I have tseries data that has been stored in influxdb, I would like to serve this data through a web API and therefore are considering Django framework and its REST API framework as a solution.

At the moment, there is no known support for Influxdb on Django framework, however I have seen someone imply successful creation of this stack in a discussion thread here.

Any clear comments or thoughts on how this can be achieved would be appreciated, particularly how to integrate influxdb into the Django framework.

qboomerang
  • 1,931
  • 3
  • 15
  • 20

1 Answers1

15

You can use Django and InfluxDB together in Docker. First, create a project and app.

mkdir django-influxdb-example
cd django-influxdb-example
django-admin startproject project
cd project
django-admin startapp app
cd ..

Create a virtual environment and install django, postgres and influxdb.

python3 -m venv venv
source venv/bin/activate
pip install django psycopg2 influxdb
pip install --upgrade pip
pip freeze > requirements.txt

Right now, my requirements look like,

certifi==2018.8.24
chardet==3.0.4
Django==2.1.1
idna==2.7
influxdb==5.2.0
psycopg2==2.7.5
python-dateutil==2.7.3
pytz==2018.5
requests==2.19.1
six==1.11.0
urllib3==1.23

Create a Dockerfile,

FROM python:3.6-alpine3.7

ENV PYTHONUNBUFFERED 1

RUN apk update \
    && apk add libpq postgresql-dev \
    && apk add build-base

WORKDIR /usr/src/app

COPY requirements.txt ./
RUN pip install -r requirements.txt

COPY project ./

Create a docker-compose.yml file,

version: '3'

services:

  db:
    image: postgres

  web:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - ./project:/usr/web/app
    ports:
      - "8000:8000"
    depends_on:
      - db
      - influxdb
    links:
      - influxdb

  influxdb:
    image: influxdb
    ports:
      - "8083:8083"
      - "8086:8086"

I added the following to my Django settings.py,

INFLUXDB_HOST = 'influxdb'
INFLUXDB_PORT = 8086
INFLUXDB_USERNAME = None
INFLUXDB_PASSWORD = None
INFLUXDB_DATABASE = 'example'
INFLUXDB_TIMEOUT = 10

At this point, you could try to build the whole thing and see if it comes up,

docker-compose build
docker-compose run web python3 manage.py makemigrations
docker-compose run web python3 manage.py migrate
docker-compose up

If you go to localhost:8000, then you should see the default Django splash page. If you open an interactive Python session on the web container, you should be able to access InfluxDB through the Python influxdb module,

docker-compose run web python3
Starting django-influxdb-example_db_1 ... done
Starting django-influxdb-example_influxdb_1 ... done
Python 3.6.6 (default, Sep 12 2018, 02:19:14)
[GCC 6.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from influxdb import InfluxDBClient
>>> client = InfluxDBClient('influxdb', 8086)
>>> client.get_list_database()
[{'name': '_internal'}]
>>> client.create_database('example')
>>> client.get_list_database()
[{'name': '_internal'}, {'name': 'example'}]

Now everything is connected and running, you can focus on writing to code to create databases, and add points. I looked through the code for django-influxdb-metrics and found the code below, which I put in a utils.py file in my app/ directory. This creates an influxdb client using the constants we defined earlier in the settings.py file.

from django.conf import settings

from influxdb import InfluxDBClient

import logging
logger = logging.getLogger(__name__)


def get_influxdb_client():
    """Returns an ``InfluxDBClient`` instance."""
    client = InfluxDBClient(
    settings.INFLUXDB_HOST,
    settings.INFLUXDB_PORT,
    settings.INFLUXDB_USERNAME,
    settings.INFLUXDB_PASSWORD,
    settings.INFLUXDB_DATABASE,
    timeout=getattr(settings, 'INFLUXDB_TIMEOUT', 10),
    ssl=getattr(settings, 'INFLUXDB_SSL', False),
    verify_ssl=getattr(settings, 'INFLUXDB_VERIFY_SSL', False),
)
return client

Finally, from here, I can create databases, add points, and perform queries through the client.

cjohnson318
  • 3,154
  • 30
  • 33
  • It works when influx is started as an independent service. When I wrote the question, I was grappling with how to replace the underlying database used by Django for its models, perhaps a direction that was not necessary had I known about the concept of containerization. The solution makes a lot of sense now. – qboomerang Jun 21 '19 at 03:44
  • I see that this resets the connection with every request, do you have an idea how to use a persistent connection? This is out of scope but I am not sure it is worth a question of its own. – iDrwish Jun 09 '20 at 09:12