Luna Modelling API
Overview
Luna Modelling API is an advanced modelling and prediction service that provides sophisticated statistical filtering capabilities for time series data. The API specializes in Kalman filtering, enabling users to process noisy time series data and extract smooth, accurate estimates of underlying trends.Architecture
Key Concepts
Account System
The API uses an account-based authentication and authorization system. Each account has the following attributes:- Account Name: A unique identifier for the account
- API Key: A UUID-based key used for authentication (passed in the
X-API-Keyheader) - Quota: The number of API calls remaining for the account
Quota Management
Quota is a critical concept in the Luna Modelling API:- Each account has a finite quota of API calls
- Every successful request to processing endpoints (e.g., Kalman filter) consumes 1 quota unit
- The quota check is performed atomically before processing to prevent race conditions
- When quota reaches 0, the API returns a
403 Forbiddenerror - The
/account/quotaendpoint allows users to check their remaining quota without consuming it
Kalman Filtering
The core functionality of the API is Kalman filtering for time series data:- Input: Multi-dimensional time series data as nested arrays
- Processing:
- Forward pass: Predicts and corrects state estimates
- Smoothing pass: Refines estimates using all available data
- Output:
- Filtered data (predictions)
- Raw state estimates
- Smoothed state estimates
save parameter is set to true, the API stores the input data with a unique identifier and processes all historical data for that identifier, enabling cumulative analysis over time.
Prerequisites
Before installing and running Luna Modelling API, ensure you have the following:- Python: Version 3.10 or higher
- PostgreSQL: Version 12 or higher
- pip: Python package installer
- Virtual Environment (recommended):
venvorvirtualenv
System Dependencies
- PostgreSQL Development Headers: Required for
psycopg2-binary- Ubuntu/Debian:
sudo apt-get install libpq-dev - macOS:
brew install postgresql - Windows: Included with PostgreSQL installation
- Ubuntu/Debian:
Installation
1. Clone the Repository
2. Create a Virtual Environment
3. Install Dependencies
4. Configure Environment Variables
Create a.env file in the project root with the following configuration:
5. Set Up the Database
Create the Database
Run Migrations
6. Create an Initial Account (Optional)
You can manually create an account in the database or use a script to seed initial data:Running the API
Development Mode
Start the API server with auto-reload enabled:Production Mode
For production deployment:Docker Deployment (Optional)
If you have a Dockerfile:API Documentation
Once the API is running, you can access the interactive documentation:- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- OpenAPI Specification: http://localhost:8000/api/v1/openapi.json
Available Endpoints
Root
GET /- Welcome message and API information
Health
GET /api/v1/health- Health check endpoint
Account
GET /api/v1/account/quota- Check remaining quota (requires API key)
Kalman Filter
POST /api/v1/{account_id}/kalman- Apply Kalman filtering to time series data (requires API key, consumes quota)
Authentication
All protected endpoints require an API key to be passed in the request headers:Example Usage
Check Quota
Process Kalman Filter
Process and Save Data
Project Structure
Development
Running Tests
Code Formatting
Database Migrations
Troubleshooting
Database Connection Issues
- Verify PostgreSQL is running:
pg_isready - Check credentials in
.envfile - Ensure database exists:
psql -l
API Key Authentication Failures
- Verify the API key exists in the
accountstable - Ensure the
X-API-Keyheader is correctly formatted - Check for UUID formatting (no quotes, proper format)
Quota Exceeded Errors
- Check remaining quota:
GET /api/v1/account/quota - Update quota in database if needed:
UPDATE accounts SET quota = 1000 WHERE id = 1;