SCADA Substation Event Ingestion and Cleansing Script
Build a robust Python database staging script to ingest, deduplicate, and validate high-frequency SCADA alarms.
Use this template when configuring data engineering pipelines that move asynchronous SCADA grid telemetry into database landing tables. It generates a modular ingestion script with quarantine logic for malformed sensor readings.
Role: Senior Data Engineer specializing in substation telemetry, grid automation, and SCADA data pipelines.
Context
- Grid operator: {{grid_operator}}
- Ingestion source format: {{source_stream_type}}
- Target warehouse database: {{target_warehouse}}
- Target staging schema: {{raw_event_schema}}
- Batch record threshold: {{batch_size_records}}
- Maximum allowable malformed rate: {{max_error_rate_pct}}%
Task
Create a resilient Python database ingestion script that extracts raw SCADA telemetry from {{source_stream_type}}, validates electrical sensor values, deduplicates message IDs, and batch-loads sanitized events into {{target_warehouse}} under schema {{raw_event_schema}}.
Method
- Configure database connection pooling and environment-driven credentials for {{target_warehouse}}.
- Parse incoming {{source_stream_type}} records into structured records (substation_id, feeder_id, voltage, frequency, timestamp).
- Implement validation filters for electrical anomalies (e.g., negative frequency, out-of-band voltage surges).
- Deduplicate incoming event payloads based on composite key (substation_id, event_uuid, timestamp).
- Direct anomalous and malformed payloads into a dedicated quarantine dead-letter staging table.
- Terminate pipeline execution if malformed records exceed {{max_error_rate_pct}}%.
- Execute bulk database inserts using parameter arrays when memory reaches {{batch_size_records}} items.
- Return execution statistics including processed count, quarantined count, and elapsed runtime.
Constraints
- MUST write PEP-8 compliant Python 3.10+ using standard libraries and SQLAlchemy/psycopg.
- MUST NOT hardcode database credentials or grid connection strings.
- All database writes MUST execute inside an explicit database transaction with rollback support.
- Include type hints for all function signatures.
Output format
Provide the script in the following format:
- Config & Imports Block
- Schema Definition DDL (DDL for main table & quarantine table)
- Main Ingestion & Sanitization Python Class
- Batch Write & Transaction Context Manager
- Entrypoint
main()execution stub Keep the total code length under 180 lines.
Self-review
- Ensure memory consumption remains constant regardless of stream length.
- Confirm malformed payload rate calculation triggers failure when exceeding {{max_error_rate_pct}}%.
- Validate that {{target_warehouse}} bulk write API uses parameter binding.
Explicit role, a named task, and discrete steps the model can follow.
Background, inputs and variables the model needs before it starts.
Hard boundaries — what the model must and must not do.
A named, field-level shape for the response.
Ordered work items that force analysis before an answer.
Length and structure that travel across frontier models.
Signal density — instruction weight without padding.
Documented variables so the scaffold adapts to new inputs.
Quality bar, assumptions and behaviour when inputs are thin.
How much real usage the template has behind it.