llm-optimizer-processor 0.1.0

Data processing and transformation pipeline
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# Stream Processor

High-performance, production-ready stream processing library for LLM Auto-Optimizer.

## Features

- **Windowing**: Tumbling, Sliding, and Session windows
- **Aggregations**: Count, Sum, Average, Min, Max, Percentiles, Standard Deviation
- **Time Semantics**: Event-time processing with watermark support
- **Late Events**: Configurable handling for out-of-order events
- **Performance**: Sub-100ms latency, 100K+ events/sec throughput
- **Statistics**: Online variance, EMA, sliding window stats, anomaly detection

## Quick Start

```rust
use processor::{StreamProcessorBuilder, aggregation::CompositeAggregator};
use chrono::Duration;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Create a stream processor with 5-minute tumbling windows
    let mut processor = StreamProcessorBuilder::new()
        .build_tumbling(
            Duration::minutes(5),
            |_key| {
                let mut agg = CompositeAggregator::new();
                agg.add_count("requests")
                   .add_avg("avg_latency")
                   .add_p95("p95_latency")
                   .add_p99("p99_latency");
                agg
            },
        );

    // Get results stream
    let mut results = processor.results().unwrap();

    // Process events
    processor.process_event(
        "my-service".to_string(),
        chrono::Utc::now(),
        123.45  // metric value
    ).await?;

    // Collect results
    tokio::spawn(async move {
        while let Some(result) = results.next().await {
            println!("Window: {}", result.window);
            println!("Results: {:?}", result.results);
        }
    });

    Ok(())
}
```

## Window Types

### Tumbling Windows
Fixed-size, non-overlapping windows for periodic aggregations:

```rust
let processor = StreamProcessorBuilder::new()
    .build_tumbling(Duration::minutes(5), aggregator_factory);
```

### Sliding Windows
Overlapping windows for moving averages and smoothed metrics:

```rust
let processor = StreamProcessorBuilder::new()
    .build_sliding(
        Duration::minutes(10),  // Window size
        Duration::seconds(30),  // Slide interval
        aggregator_factory
    );
```

### Session Windows
Gap-based windows for activity clustering:

```rust
let processor = StreamProcessorBuilder::new()
    .build_session(
        Duration::seconds(30),  // Inactivity gap
        aggregator_factory
    );
```

## Aggregations

### Composite Aggregator
Run multiple aggregations efficiently:

```rust
let mut agg = CompositeAggregator::new();
agg.add_count("total_requests")
   .add_avg("avg_response_time")
   .add_min("min_response_time")
   .add_max("max_response_time")
   .add_p50("p50_response_time")
   .add_p95("p95_response_time")
   .add_p99("p99_response_time")
   .add_stddev("stddev_response_time");
```

### Simple Aggregators
Use individual aggregators for specific metrics:

```rust
use processor::aggregation::{
    AverageAggregator,
    PercentileAggregator,
    StandardDeviationAggregator,
};

let avg = AverageAggregator::new();
let p95 = PercentileAggregator::new(95);
let stddev = StandardDeviationAggregator::new();
```

## Configuration

```rust
use processor::StreamProcessorConfig;
use chrono::Duration;

let config = StreamProcessorConfig {
    allow_late_events: true,
    late_event_threshold: Duration::seconds(30),
    watermark_interval: Duration::seconds(1),
    window_retention: Duration::hours(1),
    max_windows_per_key: 1000,
    result_buffer_size: 1000,
};

let processor = StreamProcessorBuilder::new()
    .with_config(config)
    .build_tumbling(Duration::minutes(5), aggregator_factory);
```

## Advanced Statistics

The library includes advanced statistical utilities:

### Online Variance
Numerically stable variance calculation:

```rust
use processor::aggregation::statistics::OnlineVariance;

let mut variance = OnlineVariance::new();
for value in values {
    variance.update(value);
}
let std_dev = variance.std_dev();
```

### Exponential Moving Average
Smoothed metrics over time:

```rust
use processor::aggregation::statistics::ExponentialMovingAverage;

let mut ema = ExponentialMovingAverage::with_span(10.0)?;
ema.update(value);
let smoothed = ema.value();
```

### Sliding Window Statistics
Fixed-size window with percentiles:

```rust
use processor::aggregation::statistics::SlidingWindowStats;

let mut stats = SlidingWindowStats::new(100)?;
for value in values {
    stats.update(value);
}
let p95 = stats.percentile(95.0)?;
let median = stats.median();
```

### Anomaly Detection
Z-score based anomaly detection:

```rust
use processor::aggregation::statistics::ZScoreAnomalyDetector;

let mut detector = ZScoreAnomalyDetector::new(3.0); // 3-sigma threshold
let is_anomaly = detector.check(value);
```

## Event Deduplication

Prevent duplicate events from being processed multiple times:

```rust
use processor::state::{RedisStateBackend, RedisConfig, StateBackend};
use std::sync::Arc;
use std::time::Duration;

// Create Redis backend for deduplication
let redis_config = RedisConfig::builder()
    .url("redis://localhost:6379")
    .key_prefix("dedup:")
    .default_ttl(Duration::from_secs(3600))
    .build()?;

let backend = Arc::new(RedisStateBackend::new(redis_config).await?);

// Check for duplicates
let event_id = "event_123";
let key = format!("dedup:{}", event_id);

if backend.get(key.as_bytes()).await?.is_none() {
    // First time seeing this event - process it
    backend.put_with_ttl(key.as_bytes(), b"1", Duration::from_secs(3600)).await?;
    process_event(event).await?;
} else {
    // Duplicate - skip processing
    info!("Duplicate event detected: {}", event_id);
}
```

### Features

- **Multiple Backends**: Redis (distributed), Sled (local), In-memory
- **Configurable TTL**: Automatic cleanup of old deduplication records
- **High Performance**: 100K+ events/sec with Redis, 200K+ with Sled
- **Bloom Filters**: Probabilistic deduplication for ultra-high throughput
- **Multi-Key Strategies**: Event ID, transaction ID, idempotency keys, content hash

### Configuration Options

```rust
// Basic configuration with in-memory backend
let backend = MemoryStateBackend::new(Some(Duration::from_secs(3600)));

// Production configuration with Redis
let redis_config = RedisConfig::builder()
    .url("redis://redis-cluster:6379")
    .key_prefix("dedup:")
    .default_ttl(Duration::from_secs(86400)) // 24 hours
    .pool_size(10, 50)  // min 10, max 50 connections
    .build()?;

// High-performance local storage with Sled
let sled_backend = SledStateBackend::new("./dedup.db")?;
```

### Use Cases

1. **Kafka Message Deduplication**: Handle at-least-once delivery semantics
2. **API Idempotency**: Prevent duplicate requests using idempotency keys
3. **Multi-Source Aggregation**: Deduplicate events from multiple sources
4. **Retry Safety**: Ensure safe retries without duplicate processing
5. **Exactly-Once Processing**: Maintain exactly-once semantics across restarts

See [deduplication/README.md](./src/deduplication/README.md) for complete documentation.

## Time-Series Normalization

Transform irregularly sampled time-series data into uniform intervals for analysis and processing:

```rust
use processor::normalization::{TimeSeriesNormalizer, NormalizationConfig, FillStrategy};
use chrono::Duration;

// Configure normalization with 1-second intervals
let config = NormalizationConfig::builder()
    .interval(Duration::seconds(1))
    .fill_strategy(FillStrategy::Linear)
    .max_gap_duration(Duration::seconds(60))
    .out_of_order_threshold(Duration::seconds(5))
    .build()?;

let mut normalizer = TimeSeriesNormalizer::new(config);

// Process irregular events
normalizer.process_event(timestamp1, value1).await?;
normalizer.process_event(timestamp2, value2).await?;

// Get normalized time-series with uniform intervals
let normalized = normalizer.normalize().await?;
```

### Visual Example

```
Original (irregular sampling):
  t=0.0s: 10.0
  t=0.3s: 12.0
  t=1.7s: 18.0
  t=4.9s: 35.0

Normalized (1-second intervals, linear interpolation):
  t=0s: 10.0
  t=1s: 14.4   (interpolated)
  t=2s: 18.0
  t=3s: 24.3   (interpolated)
  t=4s: 30.7   (interpolated)
  t=5s: 35.0
```

### Fill Strategies

- **Linear**: Interpolate between known points (best for continuous metrics)
- **Forward Fill**: Carry last value forward (best for discrete states)
- **Backward Fill**: Carry next value backward (batch processing)
- **Zero Fill**: Fill with zero (best for event counts)
- **Mean Fill**: Fill with average (best for noisy data)
- **Polynomial**: Higher-order interpolation (scientific data)
- **Spline**: Smooth curves without oscillation (high-quality visualization)

### Features

- **Irregular Sampling**: Handle variable-rate data collection
- **Out-of-Order Events**: Automatic buffering and reordering
- **Gap Filling**: Intelligent interpolation for missing data
- **Timestamp Alignment**: Floor, ceil, or round to intervals
- **Resampling**: Upsample or downsample to target frequencies
- **Multi-Metric**: Normalize multiple metrics with different strategies
- **Outlier Detection**: Z-score based anomaly filtering
- **High Performance**: 500K+ events/sec with linear interpolation

### Configuration Options

```rust
let config = NormalizationConfig {
    // Time interval between normalized points
    interval: Duration::seconds(1),

    // Timestamp alignment strategy
    alignment: AlignmentStrategy::Floor,  // or Ceil, Round

    // Gap filling strategy
    fill_strategy: FillStrategy::Linear,

    // Maximum gap to fill (larger gaps left as missing)
    max_gap_duration: Some(Duration::seconds(60)),

    // Out-of-order handling
    out_of_order_buffer_size: 1000,
    out_of_order_threshold: Duration::seconds(5),
};
```

### Use Cases

1. **IoT Sensor Data**: Normalize irregular sensor readings for analysis
2. **Financial Data**: Align OHLC data to uniform intervals
3. **System Metrics**: Standardize monitoring data from multiple sources
4. **ML Feature Engineering**: Create uniform input for time-series models
5. **Visualization**: Generate smooth, consistent time-series plots
6. **Aggregation Preparation**: Align data before windowing operations

### Performance Characteristics

| Strategy | Throughput | Latency (avg) | Use Case |
|----------|------------|---------------|----------|
| Linear | 500K events/sec | 2.0 μs | Continuous metrics |
| Forward Fill | 800K events/sec | 1.25 μs | Discrete states |
| Zero Fill | 1M events/sec | 1.0 μs | Event counts |
| Spline | 50K events/sec | 20 μs | Visualization |

See [normalization/README.md](./src/normalization/README.md) for complete documentation.

## Examples

Run the examples to see the stream processor in action:

```bash
# Basic stream processor demo
cargo run --example stream_processor_demo

# Multi-key sliding window
cargo run --example multi_key_sliding_window

# Event deduplication
cargo run --example event_deduplication_demo
cargo run --example advanced_deduplication

# Time-series normalization
cargo run --example timeseries_normalization_demo
cargo run --example advanced_normalization

# Other examples
cargo run --example kafka_pipeline
cargo run --example aggregation_demo
cargo run --example watermark_demo
```

## Performance

Typical performance characteristics:

- **Latency**: 1-5ms processing time per event
- **Throughput**: 100,000+ events/sec (single key)
- **Throughput**: 1,000,000+ events/sec (multiple keys with parallelization)
- **Memory**: ~1KB per window + aggregation data

## Monitoring

Get processor statistics:

```rust
let stats = processor.stats().await;
println!("Events processed: {}", stats.events_processed);
println!("Windows fired: {}", stats.windows_fired);
println!("Active windows: {}", stats.active_windows);
println!("P99 latency: {:.2}ms", stats.latency_p99_ms);
```

## Architecture

See [STREAM_PROCESSOR.md](./STREAM_PROCESSOR.md) for detailed implementation documentation including:

- Architecture overview
- Implementation details
- Performance characteristics
- Tuning guidelines
- Best practices

## Testing

```bash
# Run all tests
cargo test -p processor

# Run with output
cargo test -p processor -- --nocapture

# Run benchmarks
cargo bench -p processor
```

## Integration

The stream processor integrates with:

- **Kafka**: Source and sink for event streaming
- **State Backends**: Redis, PostgreSQL, Sled for state management
- **Watermark Generators**: For event-time progress tracking
- **Pipeline Framework**: For complex stream processing workflows

## License

Apache-2.0

## Contributing

See [CONTRIBUTING.md](../../CONTRIBUTING.md) for contribution guidelines.