This project implements a time-based database with a Flask API, which allows users to interact with data that includes timestamps. It includes features like data retention, interpolation, and alert management for devices. The data and configuration are stored in JSON files, and the system supports periodic retention of outdated data.
- Data Retention: The system enforces a retention policy to automatically delete data older than a specified number of days.
- Interpolation: Users can request interpolated data within a time range, based on existing data points.
- Alerts Management: Users can modify alerts for devices.
- Notifications: Access to a list of all notifications for the system.
- Locations: Retrieve the locations associated with the devices.
-
Clone or download the repository.
-
Install the necessary dependencies:
pip install flask apscheduler
-
Ensure the following files are present:
ressources/db_info.json: Contains configuration data such as device alerts, locations, and notifications (already pre-filled with example data).ressources/data.json: Contains the time-series data points (also pre-filled with example data).
-
Run the Flask application:
python app.py
This will start the Flask web server, and the API will be accessible at
http://localhost:5000.
Provides details about the API and its endpoints.
Response:
{
"endpoints": {
"/data": {
"POST": {
"description": "Add new data point.",
"parameters": {
"device_id": "Device ID",
"timestamp": "Timestamp (ISO format)",
"value": "Value"
}
},
"GET": {
"description": "Query data with optional interpolation.",
"parameters": {
"device_id": "Device ID",
"start_time": "Start time (ISO format)",
"end_time": "End time (ISO format)",
"interval": "Interpolation interval in seconds (default: 1)"
}
}
}
}
}Retrieve the location information for devices.
Response:
{
"location": {
"device1": "Location A",
"device2": "Location B"
}
}Retrieve alerts for a specific device.
Response:
{
"alerts": [
{"type": "temperature", "value": 50, "timestamp": "2024-12-01T10:00:00"}
]
}Get all notifications for the system.
Response:
[
{"message": "Battery low for device1", "timestamp": "2024-12-01T08:00:00"},
{"message": "Device2 offline", "timestamp": "2024-12-01T09:00:00"}
]Modify alerts for a specific device.
Request Body:
{
"alerts": [
{"type": "temperature", "value": 60, "timestamp": "2024-12-02T12:00:00"}
]
}Response:
{
"message": "Alerts updated successfully",
"alerts": [
{"type": "temperature", "value": 60, "timestamp": "2024-12-02T12:00:00"}
]
}Add a new data point.
Request Body:
{
"device_id": "device1",
"timestamp": "2024-12-02T10:30:00",
"value": 45.0
}Response:
{
"message": "Data added successfully"
}Query data with optional interpolation between a time range for a specific device.
Query Parameters:
device_id: Device IDstart_time: Start time (ISO format)end_time: End time (ISO format)interval: Interpolation interval in seconds (default: 1)
Response:
[
{
"timestamp": "2024-12-02T10:30:00",
"value": 45.0
},
{
"timestamp": "2024-12-02T10:31:00",
"value": 46.5
}
]- db_info.json: Contains information about device alerts, notifications, and locations (already populated with example data).
- data.json: Contains time-series data points for devices (also pre-populated with example data).
The application runs a background task every 12 hours to enforce the data retention policy, removing any data older than 30 days.
To run the application:
-
Clone the repository.
-
Ensure
db_info.jsonanddata.jsonare populated with example data (these are included in the repository). -
Install dependencies:
pip install flask apscheduler
-
Start the Flask server:
python app.py
The server will run at
http://localhost:5000.
This Flask-based time-series database API provides easy-to-use endpoints for managing device data, alerts, and notifications. It supports interpolation of time-series data and automatic retention of outdated data based on a configurable retention policy.