FastAPI backend for analyzing and assessing the quality of high-frequency IIoT sensor data using PostgreSQL with TimescaleDB.
The recommended way to run the backend is using Docker Compose:
# From project root
./start-dev.shThis starts:
- TimescaleDB (PostgreSQL with time-series extension)
- Worker (background data aggregation service)
- Backend API (FastAPI server on port 8000)
The backend will be available at:
- API: http://localhost:8000
- API Docs: http://localhost:8000/docs
For local development without Docker:
- Python 3.11+
- PostgreSQL with TimescaleDB extension
uvpackage manager (install)
- Create virtual environment and install dependencies:
cd backend
uv sync
source .venv/bin/activate- Configure environment variables:
Create a
.envfile or export:
export DB_HOST=localhost
export DB_PORT=5432
export DB_NAME=iiot_dqa
export DB_USER=iiot_user
export DB_PASS=iiot_password
export OPENAI_API_KEY=your_key_here
export PYTHONPATH=$(pwd)- Run the server:
uvicorn main:app --host 0.0.0.0 --port 8000 --reloadbackend/
├── api/ # API route handlers (FastAPI routers)
│ ├── agent_chat.py
│ ├── data_import.py
│ └── visualization.py
├── core/ # Business logic and services
│ ├── analytics.py
│ ├── connection_manager.py
│ └── import_service.py
├── models/ # Database models and connection
│ └── database.py # SQLAlchemy engine and connection management
├── schemas/ # Pydantic schemas for request/response validation
│ ├── data_import.py
│ └── visualization.py
├── config/ # Configuration files
└── main.py # FastAPI application entry point
GET /health- Health check with database statusGET /tables- List available machine groupsGET /tables/{machine_group}- Get sensors for a machine group
GET /data?table={machine_group}&limit={n}- Get aggregated insights dataGET /data/preprocessed?table={machine_group}&limit={n}- Get preprocessed sensor dataGET /tags?table={machine_group}- Get sensor metadata/thresholdsGET /sensors?table={machine_group}- Get list of sensors
GET /analytics/aggregation_frequency?table={machine_group}- Get aggregation frequencyGET /analytics/missing?table={machine_group}- Get missing values analysisGET /analytics/visualization- Comprehensive visualization analytics
POST /import/upload- Upload and import sensor data filesGET /import/status/{job_id}- Check import job status
POST /agent/chat- Chat with AI-powered data quality assessment agent
# Health check
curl http://localhost:8000/health
# List machine groups
curl http://localhost:8000/tables
# Get sensors for a machine group
curl "http://localhost:8000/tables/MACHINE_GROUP_1"- FastAPI with Pydantic v2 for request/response validation
- SQLAlchemy 2.0+ with connection pooling for database operations
- PostgreSQL/TimescaleDB for time-series data storage
- Modular structure: Separation of API routes, business logic, and data access
- Pandas for data processing and analytics
- Ensure TimescaleDB is running:
docker-compose ps timescaledb - Check connection string in environment variables
- Verify database exists and user has proper permissions
- Port in use:
lsof -ti tcp:8000 | xargs kill -9 - Module not found: Ensure
PYTHONPATHincludes backend directory - Hot-reload not working: Check that code volumes are mounted in docker-compose.yml
- All dependencies are defined in
pyproject.toml - Use
uv syncto create virtual environment and install dependencies