# codingforentrepreneurs/django-iot-with-timescaledb architecture

> Architecture diagram of the GitHub repository codingforentrepreneurs/django-iot-with-timescaledb, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/codingforentrepreneurs/django-iot-with-timescaledb
- Repository: https://github.com/codingforentrepreneurs/django-iot-with-timescaledb

## Overview

##### Project Identification
- The project is a Django IoT application that collects time series sensor data using TimescaleDB.
- It is designed to run on multiple Raspberry Pi devices (“djangopi”, “djangopi-2”, etc.), each hosting a Django instance that interacts with local sensors.
- The deployment and configuration are handled via Ansible; additional critical services include Redis (for Celery tasks) and external TimescaleDB Cloud for data storage.

##### File Structure
- The top-level directories include configuration files like the Dockerfile, .env samples, and a “compose.yaml” indicating containerization.
- The “src” directory holds the Django application (with a project module “cfehome” and an application called “sensors”). This illustrates the typical web server, API, and background task (Celery) structure.
- The “ansible” directory contains an assortment of playbooks and roles (e.g., deploy_django, connect_hosts, redis, services_config) which automate configuration across multiple nodes (Raspberry Pis).
- Additional elements, like “requirements.txt” and “requirements.dev.txt”, detail package dependencies.

##### Components & Relationships
- Ansible Automation Engine:
- Central component responsible for orchestrating deployments, connecting hosts, and managing service restarts across nodes.
- Uses various playbooks (connect_hosts, deploy_django, restart_services) to manage both the application and ancillary services.
- IoT Nodes (Raspberry Pis):
- Each device runs a Django instance (using Gunicorn and possibly behind an Nginx proxy), accesses local sensors via the “sensors” app, and communicates with peers using Ansible-configured host connections.
- They operate with Celery workers (using Redis as a broker) for asynchronous tasks.
- External Services:
- TimescaleDB Cloud: This is the main database component for storing the time series data coming from the sensors.
- Redis: Likely configured locally on each node or as a shared service for task queuing with Celery in the Django app.
- Application Components:
- Web Application: The Django project (found under src/cfehome) handles API endpoints (api.py), configuration (settings.py), task scheduling (celery.py), and web serving (wsgi.py/asgi.py).
- Sensors Module: Special functionality for collecting sensor data, as seen in the “sensors” sub-folder with models, views, tasks, etc.
- Static and Template Layers: Static resources and Django templates provide the frontend for administrative/user interfaces.

##### Architectural Patterns & Design Principles
- Distributed Deployment: The use of Ansible playbooks and a dynamic host inventory.
- Separation of Concerns: Clear separation between the web application (Django), background task execution (Celery with Redis), and the data layer (TimescaleDB).
- Containerization: Presence of Docker-related files suggests that containerization may be a deployment method.
- Scalability & Modularity: Each component (Django instance, Celery, Redis, and TimescaleDB) can be scaled or maintained separately.

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["Ansible Automation Engine"]:::automation
    C["Containerization Setup"]:::container
    D["IoT Node Deployment"]:::deployment
    subgraph "Raspberry Pi Node: djangopi"
        DA["Django Web Application"]:::django
        SM["Sensors Module for Data Collection"]:::sensors
        CW["Celery Worker"]:::celery
    end
    CC["Celery Service Config"]:::config
    R["Redis Broker"]:::redis
    DB["TimescaleDB Cloud"]:::database

    A -->|"deploys"| D
    A -->|"deploys"| C
    D -->|"installs"| DA
    DA -->|"integrates"| SM
    DA -->|"queuesTasks"| R
    DA -->|"storesData"| DB
    CW -->|"processesTasks"| R
    CW -->|"configuredBy"| CC

    click A "https://github.com/codingforentrepreneurs/django-iot-with-timescaledb/tree/main/ansible/"
    click C "https://github.com/codingforentrepreneurs/django-iot-with-timescaledb/tree/main/Dockerfile"
    click D "https://github.com/codingforentrepreneurs/django-iot-with-timescaledb/tree/main/src/"
    click DA "https://github.com/codingforentrepreneurs/django-iot-with-timescaledb/tree/main/src/cfehome"
    click SM "https://github.com/codingforentrepreneurs/django-iot-with-timescaledb/tree/main/src/sensors"
    click CW "https://github.com/codingforentrepreneurs/django-iot-with-timescaledb/blob/main/src/cfehome/celery.py"
    click CC "https://github.com/codingforentrepreneurs/django-iot-with-timescaledb/blob/main/ansible/roles/services_config/templates/celery-worker.service.j2"
    click R "https://github.com/codingforentrepreneurs/django-iot-with-timescaledb/tree/main/ansible/roles/redis"

    classDef automation fill:#cce5ff,stroke:#009,stroke-width:2px;
    classDef container fill:#e1ccec,stroke:#800080,stroke-width:2px;
    classDef deployment fill:#d1e7dd,stroke:#0a4,stroke-width:2px;
    classDef django fill:#fff3cd,stroke:#c69500,stroke-width:2px;
    classDef sensors fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
    classDef celery fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef redis fill:#fce5cd,stroke:#ff8000,stroke-width:2px;
    classDef database fill:#d1d8e0,stroke:#708090,stroke-width:2px;
    classDef config fill:#e0e0e0,stroke:#606060,stroke-width:2px;
```
