← Back to the chapter · Previous detail · Next detail →
Technical detail · Edge Data: Sampling and Statistical Compression
A timestamped statistical window
import statistics
from dataclasses import dataclass
from typing import Optional
@dataclass
class AggregatedWindow:
timestamp: int # Window start
count: int # Number of samples
mean: float
min_val: float
max_val: float
std_dev: float
p95: Optional[float] = None # Optional percentile
class WindowAggregator:
def __init__(self, window_seconds: int = 60):
self.window_seconds = window_seconds
self.buffer: list[float] = []
self.window_start: Optional[int] = None
def add_sample(self, timestamp: int, value: float) -> Optional[AggregatedWindow]:
if self.window_start is None:
self.window_start = timestamp
# Check if window complete
if timestamp - self.window_start >= self.window_seconds:
result = self._compute_aggregate()
self.buffer = [value]
self.window_start = timestamp
return result
self.buffer.append(value)
return None
def _compute_aggregate(self) -> AggregatedWindow:
sorted_buffer = sorted(self.buffer)
p95_idx = int(len(sorted_buffer) * 0.95)
return AggregatedWindow(
timestamp=self.window_start,
count=len(self.buffer),
mean=statistics.mean(self.buffer),
min_val=min(self.buffer),
max_val=max(self.buffer),
std_dev=statistics.stdev(self.buffer) if len(self.buffer) > 1 else 0,
p95=sorted_buffer[p95_idx] if p95_idx < len(sorted_buffer) else None
)
# Example: 1 Hz sensor, 60-second windows
# Input: 60 samples x 8 bytes = 480 bytes
# Output: 1 aggregate x 48 bytes = 48 bytes
# Compression ratio: 10:1
# Preserved: Trend (mean), anomaly detection (min/max/std), health (count)