harbor run -t LiteCoder/sensor-data-pipeline-apiharbor download LiteCoder/sensor-data-pipeline-apiBuild an end-to-end Python data pipeline that ingests gzipped ND-JSON sensor data, cleans it with quality flags, stores it in Parquet, computes rolling statistics, and exposes a REST API for querying recent readings.
After running setup.py, gzipped ND-JSON files are available under /data/raw/. Each file is named sensor_data_YYYYMMDD_HH.json.gz and contains one JSON object per line with these fields:
device_id (string): e.g. "SENSOR-001" through "SENSOR-025"timestamp (string): ISO 8601 format, e.g. "2024-01-15T00:00:00"temperature (float or null): degrees Celsiushumidity (float or null): percentagepower_consumption (float): kilowattsair_quality (int): AQI indexmotion_detected (bool): true/falseThe data contains intentional quality issues: null values, out-of-range sensor readings, and occasional duplicate timestamps.
Create a Python module at /app/pipeline/clean.py that provides a function:
clean_sensor_file(input_path: str) -> pd.DataFrame
This function must:
qf_null — True if temperature or humidity is null/NaN.qf_range — True if temperature is outside [-40, 60] or humidity is outside [0, 100].qf_duplicate — True for rows that share the same (device_id, timestamp) as another row (mark all duplicates, not just the second occurrence).qf_* columns.Create a script at /app/pipeline/store.py that provides a function:
store_cleaned_data(df: pd.DataFrame, output_dir: str) -> None
This function must:
output_dir.date (derived as YYYY-MM-DD from timestamp) and device_id.output_dir should contain exactly one copy of each unique (device_id, timestamp) record.The default cleaned data output directory is /app/data/cleaned/.
Create a script at /app/pipeline/analytics.py that provides a function:
compute_rolling_stats(cleaned_dir: str, output_path: str) -> pd.DataFrame
This function must:
cleaned_dir.device_id.temperature, humidity, and power_consumption.<col>_rolling_mean and <col>_rolling_std.output_path as a single Parquet file.The default analytics output path is /app/data/analytics/rolling_stats.parquet.
Create a FastAPI application at /app/pipeline/api.py that:
0.0.0.0:8000./readings/{device_id} (e.g. /readings/SENSOR-001).{
"device_id": "SENSOR-001",
"count": 42,
"readings": [
{
"timestamp": "2024-01-15T02:30:00",
"temperature": 20.5,
"humidity": 45.2,
"power_consumption": 2.1,
"air_quality": 75,
"motion_detected": false,
"qf_null": false,
"qf_range": false,
"qf_duplicate": false
}
]
}
readings array must contain only records from the most recent 30 minutes of available data for that device, sorted by timestamp ascending.count must equal the length of the readings array.device_id is not found, return HTTP 404 with {"detail": "Device not found"}.Create a script at /app/pipeline/run_pipeline.py that, when executed with python /app/pipeline/run_pipeline.py:
.json.gz files from /data/raw/./app/data/cleaned/./app/data/analytics/rolling_stats.parquet."Pipeline complete" to stdout when finished.