You now have a complete Python GraphQL API for generic sensor data management with PostgreSQL integration. The project includes:
-
Complete Project Structure
- FastAPI + Strawberry GraphQL backend
- SQLAlchemy ORM with PostgreSQL
- Alembic database migrations
- Virtual environment setup (.venv)
-
Data Model (Generic Sensor System)
- SensorType: Configurable sensor types (temperature, humidity, etc.)
- Location: Hierarchical location management (building → floor → room)
- Sensor: Individual sensor devices with metadata
- SensorReading: Time-series sensor measurements
- Alert: Alert system for monitoring thresholds
-
GraphQL API
- Complete CRUD operations for all entities
- Time-series data queries
- Real-time sensor status monitoring
- Flexible filtering and pagination
-
Development Tools
- Sample data creation script
- Development helper script (dev.sh)
- Code formatting and quality checks
- Comprehensive documentation
- Log into your Aiven console
- Create a new PostgreSQL service
- Copy the connection details
- Edit
.envfile and replace the DATABASE_URL:
DATABASE_URL=postgresql://avnadmin:your_password@your-host.aivencloud.com:port/defaultdb# Activate virtual environment
source .venv/bin/activate
# Run full development setup (migrations + sample data + start server)
./dev.sh dev- GraphQL Playground: http://localhost:8000/graphql
- API Documentation: http://localhost:8000/docs
- Health Check: http://localhost:8000/health
- Temperature (°C), Humidity (%RH), Pressure (hPa)
- Air Quality (AQI), Light Level (lux)
- Configurable units, ranges, and data types
Main Office Building
├── Floor 1
│ ├── Conference Room A
│ ├── Open Office
│ └── Server Room
├── Floor 2
│ └── ...
└── Floor 3
└── ...
- Device ID, manufacturer, model, firmware version
- Calibration settings, sampling intervals
- Online status and last seen timestamps
- Sensor readings with timestamps
- Quality indicators (good/uncertain/bad)
- Confidence scores
- Raw and calibrated values
# Start development environment
./dev.sh dev
# Just run migrations
./dev.sh migrate
# Create sample data
./dev.sh sample-data
# Start server only
./dev.sh start
# Run tests
./dev.sh test
# Format code
./dev.sh format
# Check code quality
./dev.sh checkquery {
sensors {
deviceId
name
sensorType { name, unit }
location { name }
latestReading { value, timestamp, quality }
}
}mutation {
createSensorReading(input: {
sensorId: "sensor-uuid"
value: 23.5
quality: "good"
}) {
id
value
timestamp
}
}- Support any sensor type without code changes
- Configurable units, ranges, and data types
- Flexible metadata storage with JSON fields
- Time-series optimized database schema
- Efficient indexing for large datasets
- RESTful and GraphQL endpoints
- Sensor online/offline status tracking
- Alert system for threshold monitoring
- Quality indicators for data reliability
- Hierarchical location structure
- Geographic coordinates support
- Multi-level organization (building/floor/room)
- Database credentials in .env file
- Configurable security settings
- Debug mode toggle
- Set
DEBUG=Falsein production - Use strong SECRET_KEY
- Configure proper CORS origins
- Set up SSL/TLS termination
- Use connection pooling
- Implement proper logging
- GraphQL Schema: Auto-generated in playground
- Query Examples: See GRAPHQL_EXAMPLES.md
- REST Docs: Available at /docs endpoint
- Data Models: Defined in app/database/models.py
The project uses Alembic for database migrations:
# Create new migration
alembic revision --autogenerate -m "Description"
# Apply migrations
alembic upgrade head
# Rollback migration
alembic downgrade -1sensorapi/
├── app/
│ ├── core/ # Configuration
│ ├── database/ # Models and database setup
│ └── graphql/ # GraphQL schema and resolvers
├── alembic/ # Database migrations
├── scripts/ # Utility scripts
├── .venv/ # Virtual environment
├── main.py # Application entry point
├── dev.sh # Development helper script
└── requirements.txt # Python dependencies
Add sensor readings, query time-series data, and test the alert system:
- Use the sample data to explore the API
- Try different GraphQL queries in the playground
- Monitor sensor status and readings
- Test location hierarchy and filtering
- Connect to Aiven: Configure your PostgreSQL connection
- Run Migrations: Set up the database schema
- Explore Data: Use sample data to test queries
- Customize: Add your specific sensor types and locations
- Deploy: Set up production environment
The API is designed to be production-ready with proper error handling, data validation, and scalable architecture patterns.