pipeflow 0.0.4

A lightweight, configuration-driven data pipeline framework
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//! Window step for time/count-based message aggregation
//!
//! Buffers incoming messages and emits aggregated output when trigger conditions are met.
//! Supports time-based (duration) and count-based (size) triggers.

use std::sync::Mutex;
use std::time::{Duration, Instant};

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::common::message::Message;
use crate::error::{Error, Result};
use crate::transform::step::Step;

/// Window step configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WindowStepConfig {
    /// Time trigger duration (e.g., "30s", "1m", "500ms")
    #[serde(default, deserialize_with = "deserialize_duration_opt")]
    pub duration: Option<Duration>,

    /// Count trigger - emit after this many messages
    #[serde(default)]
    pub size: Option<usize>,

    /// Aggregation operation
    #[serde(default)]
    pub operation: WindowOperation,

    /// Strategy for select_one operation
    #[serde(default)]
    pub strategy: SelectStrategy,

    /// Maximum buffer capacity (default: 10000)
    #[serde(default = "default_max_messages")]
    pub max_messages: usize,

    /// Overflow behavior when buffer is full
    #[serde(default)]
    pub on_overflow: OverflowStrategy,
}

fn default_max_messages() -> usize {
    10000
}

/// Deserialize optional duration from string like "30s", "1m", "500ms"
fn deserialize_duration_opt<'de, D>(
    deserializer: D,
) -> std::result::Result<Option<Duration>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let opt: Option<String> = Option::deserialize(deserializer)?;
    match opt {
        None => Ok(None),
        Some(s) => parse_duration(&s)
            .map(Some)
            .map_err(serde::de::Error::custom),
    }
}

/// Parse duration string like "30s", "1m", "500ms"
fn parse_duration(s: &str) -> Result<Duration> {
    let s = s.trim();
    if s.is_empty() {
        return Err(Error::config("Empty duration string"));
    }

    // Find where digits end and unit begins
    let (num_str, unit) = s
        .char_indices()
        .find(|(_, c)| !c.is_ascii_digit())
        .map(|(i, _)| (&s[..i], &s[i..]))
        .unwrap_or((s, ""));

    let num: u64 = num_str
        .parse()
        .map_err(|_| Error::config(format!("Invalid duration number: {}", num_str)))?;

    let duration = match unit.trim() {
        "ms" => Duration::from_millis(num),
        "s" | "" => Duration::from_secs(num),
        "m" => Duration::from_secs(num * 60),
        "h" => Duration::from_secs(num * 3600),
        other => return Err(Error::config(format!("Unknown duration unit: {}", other))),
    };

    Ok(duration)
}

/// Window aggregation operation
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum WindowOperation {
    /// Merge all buffered payloads into a single object
    #[default]
    Merge,
    /// Select one message from the buffer
    SelectOne,
}

/// Strategy for selecting one message
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum SelectStrategy {
    /// Select the first message in window
    #[default]
    First,
    /// Select the last message in window
    Last,
}

/// Behavior when buffer reaches max capacity
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum OverflowStrategy {
    /// Drop oldest messages to make room
    #[default]
    DropOldest,
    /// Return an error
    Error,
}

/// Internal state for window buffer
struct WindowState {
    buffer: Vec<Message>,
    last_emit: Instant,
}

impl WindowState {
    fn new() -> Self {
        Self {
            buffer: Vec::new(),
            last_emit: Instant::now(),
        }
    }
}

/// Window step that aggregates messages based on time/count triggers
pub struct WindowStep {
    duration: Option<Duration>,
    size: Option<usize>,
    operation: WindowOperation,
    strategy: SelectStrategy,
    max_messages: usize,
    on_overflow: OverflowStrategy,
    state: Mutex<WindowState>,
}

impl std::fmt::Debug for WindowStep {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WindowStep")
            .field("duration", &self.duration)
            .field("size", &self.size)
            .field("operation", &self.operation)
            .field("strategy", &self.strategy)
            .field("max_messages", &self.max_messages)
            .field("on_overflow", &self.on_overflow)
            .finish_non_exhaustive()
    }
}

impl WindowStep {
    /// Create a new window step from configuration
    pub fn new(config: WindowStepConfig) -> Result<Self> {
        // Validate: at least one trigger must be specified
        if config.duration.is_none() && config.size.is_none() {
            return Err(Error::config(
                "Window step requires at least one trigger: 'duration' or 'size'",
            ));
        }

        // Validate: max_messages must be positive
        if config.max_messages == 0 {
            return Err(Error::config("max_messages must be greater than 0"));
        }

        // Validate: size trigger must not exceed max_messages
        if let Some(size) = config.size
            && size > config.max_messages
        {
            return Err(Error::config(format!(
                "size ({}) cannot exceed max_messages ({})",
                size, config.max_messages
            )));
        }

        Ok(Self {
            duration: config.duration,
            size: config.size,
            operation: config.operation,
            strategy: config.strategy,
            max_messages: config.max_messages,
            on_overflow: config.on_overflow,
            state: Mutex::new(WindowState::new()),
        })
    }

    /// Check if window should emit based on current state
    fn should_emit(&self, state: &WindowState) -> bool {
        // Count trigger
        if let Some(size) = self.size
            && state.buffer.len() >= size
        {
            return true;
        }

        // Time trigger
        if let Some(duration) = self.duration
            && state.last_emit.elapsed() >= duration
        {
            return true;
        }

        false
    }

    /// Aggregate buffered messages into output
    fn aggregate(&self, messages: Vec<Message>) -> Option<Message> {
        if messages.is_empty() {
            return None;
        }

        match self.operation {
            WindowOperation::Merge => Some(self.merge_messages(messages)),
            WindowOperation::SelectOne => self.select_one(messages),
        }
    }

    /// Merge all message payloads into a single message
    fn merge_messages(&self, messages: Vec<Message>) -> Message {
        let mut merged = serde_json::Map::new();

        // Use first message as base for metadata
        let first_meta = messages[0].meta.clone();

        // Collect all payloads into an array, or merge if objects
        let payloads: Vec<Value> = messages.into_iter().map(|m| m.payload).collect();

        // If all payloads are objects, merge them; otherwise collect as array
        let all_objects = payloads.iter().all(|p| p.is_object());

        let merged_payload = if all_objects {
            // Deep merge objects (later values overwrite earlier)
            for payload in payloads {
                if let Value::Object(obj) = payload {
                    for (k, v) in obj {
                        merged.insert(k, v);
                    }
                }
            }
            Value::Object(merged)
        } else {
            // Collect as array
            Value::Array(payloads)
        };

        Message {
            meta: first_meta,
            payload: merged_payload,
        }
    }

    /// Select one message from buffer
    fn select_one(&self, mut messages: Vec<Message>) -> Option<Message> {
        match self.strategy {
            SelectStrategy::First => messages.into_iter().next(),
            SelectStrategy::Last => messages.pop(),
        }
    }
}

impl Step for WindowStep {
    fn step_type(&self) -> &'static str {
        "window"
    }

    fn process(&self, msg: Message) -> Result<Option<Message>> {
        let mut state = self
            .state
            .lock()
            .map_err(|_| Error::transform("Lock poisoned"))?;

        // Handle buffer capacity
        if state.buffer.len() >= self.max_messages {
            match self.on_overflow {
                OverflowStrategy::DropOldest => {
                    state.buffer.remove(0);
                    tracing::debug!(step = "window", "Buffer full, dropped oldest message");
                }
                OverflowStrategy::Error => {
                    return Err(Error::transform(format!(
                        "Window buffer full (max_messages={})",
                        self.max_messages
                    )));
                }
            }
        }

        // Add message to buffer
        state.buffer.push(msg);

        // Check if we should emit
        if self.should_emit(&state) {
            let messages = std::mem::take(&mut state.buffer);
            state.last_emit = Instant::now();
            Ok(self.aggregate(messages))
        } else {
            Ok(None)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn make_msg(payload: Value) -> Message {
        Message::new("test", payload)
    }

    // ========== Duration Parsing Tests ==========

    #[test]
    fn test_parse_duration_seconds() {
        assert_eq!(parse_duration("30s").unwrap(), Duration::from_secs(30));
        assert_eq!(parse_duration("1s").unwrap(), Duration::from_secs(1));
    }

    #[test]
    fn test_parse_duration_milliseconds() {
        assert_eq!(parse_duration("500ms").unwrap(), Duration::from_millis(500));
    }

    #[test]
    fn test_parse_duration_minutes() {
        assert_eq!(parse_duration("5m").unwrap(), Duration::from_secs(300));
    }

    #[test]
    fn test_parse_duration_hours() {
        assert_eq!(parse_duration("2h").unwrap(), Duration::from_secs(7200));
    }

    // ========== Config Validation Tests ==========

    #[test]
    fn test_config_requires_trigger() {
        let config = WindowStepConfig {
            duration: None,
            size: None,
            operation: WindowOperation::Merge,
            strategy: SelectStrategy::First,
            max_messages: 1000,
            on_overflow: OverflowStrategy::DropOldest,
        };
        let result = WindowStep::new(config);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("trigger"));
    }

    #[test]
    fn test_config_size_exceeds_max() {
        let config = WindowStepConfig {
            duration: None,
            size: Some(100),
            operation: WindowOperation::Merge,
            strategy: SelectStrategy::First,
            max_messages: 50,
            on_overflow: OverflowStrategy::DropOldest,
        };
        let result = WindowStep::new(config);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("exceed"));
    }

    // ========== Count Trigger Tests ==========

    #[test]
    fn test_window_count_trigger() {
        let config = WindowStepConfig {
            duration: None,
            size: Some(3),
            operation: WindowOperation::Merge,
            strategy: SelectStrategy::First,
            max_messages: 1000,
            on_overflow: OverflowStrategy::DropOldest,
        };
        let step = WindowStep::new(config).unwrap();

        // First two messages should buffer
        assert!(step.process(make_msg(json!({"a": 1}))).unwrap().is_none());
        assert!(step.process(make_msg(json!({"b": 2}))).unwrap().is_none());

        // Third message should trigger emit
        let result = step.process(make_msg(json!({"c": 3}))).unwrap();
        assert!(result.is_some());

        let output = result.unwrap();
        // Merged payload should contain all keys
        assert_eq!(output.payload["a"], 1);
        assert_eq!(output.payload["b"], 2);
        assert_eq!(output.payload["c"], 3);
    }

    // ========== Select Operation Tests ==========

    #[test]
    fn test_window_select_first() {
        let config = WindowStepConfig {
            duration: None,
            size: Some(3),
            operation: WindowOperation::SelectOne,
            strategy: SelectStrategy::First,
            max_messages: 1000,
            on_overflow: OverflowStrategy::DropOldest,
        };
        let step = WindowStep::new(config).unwrap();

        step.process(make_msg(json!({"value": "first"}))).unwrap();
        step.process(make_msg(json!({"value": "second"}))).unwrap();
        let result = step.process(make_msg(json!({"value": "third"}))).unwrap();

        assert!(result.is_some());
        assert_eq!(result.unwrap().payload["value"], "first");
    }

    #[test]
    fn test_window_select_last() {
        let config = WindowStepConfig {
            duration: None,
            size: Some(3),
            operation: WindowOperation::SelectOne,
            strategy: SelectStrategy::Last,
            max_messages: 1000,
            on_overflow: OverflowStrategy::DropOldest,
        };
        let step = WindowStep::new(config).unwrap();

        step.process(make_msg(json!({"value": "first"}))).unwrap();
        step.process(make_msg(json!({"value": "second"}))).unwrap();
        let result = step.process(make_msg(json!({"value": "third"}))).unwrap();

        assert!(result.is_some());
        assert_eq!(result.unwrap().payload["value"], "third");
    }

    // ========== Overflow Tests ==========

    #[test]
    fn test_overflow_drop_oldest() {
        // Use a very large duration so we trigger manually via size
        // But set size = max_messages so we can fill and overflow
        let config = WindowStepConfig {
            duration: Some(Duration::from_secs(3600)), // 1 hour, won't trigger
            size: Some(3),
            operation: WindowOperation::SelectOne,
            strategy: SelectStrategy::First,
            max_messages: 3,
            on_overflow: OverflowStrategy::DropOldest,
        };
        let step = WindowStep::new(config).unwrap();

        // Fill buffer to max
        step.process(make_msg(json!({"value": 1}))).unwrap();
        step.process(make_msg(json!({"value": 2}))).unwrap();

        // This triggers emit (size=3), buffer cleared
        let result = step.process(make_msg(json!({"value": 3}))).unwrap();
        assert!(result.is_some());
        assert_eq!(result.unwrap().payload["value"], 1); // SelectStrategy::First selects first in window
    }

    #[test]
    fn test_overflow_error() {
        let config = WindowStepConfig {
            duration: Some(Duration::from_secs(3600)), // 1 hour, won't trigger
            size: None,
            operation: WindowOperation::Merge,
            strategy: SelectStrategy::First,
            max_messages: 2,
            on_overflow: OverflowStrategy::Error,
        };
        let step = WindowStep::new(config).unwrap();

        step.process(make_msg(json!({"a": 1}))).unwrap();
        step.process(make_msg(json!({"b": 2}))).unwrap();

        // Third message should error (buffer is full)
        let result = step.process(make_msg(json!({"c": 3})));
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("buffer full"));
    }

    // ========== Merge Array Test ==========

    #[test]
    fn test_merge_non_objects_as_array() {
        let config = WindowStepConfig {
            duration: None,
            size: Some(3),
            operation: WindowOperation::Merge,
            strategy: SelectStrategy::First,
            max_messages: 1000,
            on_overflow: OverflowStrategy::DropOldest,
        };
        let step = WindowStep::new(config).unwrap();

        step.process(make_msg(json!(1))).unwrap();
        step.process(make_msg(json!(2))).unwrap();
        let result = step.process(make_msg(json!(3))).unwrap();

        assert!(result.is_some());
        let payload = result.unwrap().payload;
        assert!(payload.is_array());
        assert_eq!(payload.as_array().unwrap().len(), 3);
    }
}