llm-optimizer-processor 0.1.1

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
//! Event ID extraction strategies
//!
//! This module provides various strategies for extracting unique event IDs
//! from events for deduplication purposes.

use super::error::{DeduplicationError, DeduplicationResult};
use super::id::EventId;
use llm_optimizer_types::events::{CloudEvent, FeedbackEvent};
use serde::Serialize;
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::marker::PhantomData;
use std::sync::Arc;

/// Trait for extracting unique event identifiers
///
/// Implementations define how to extract a stable, unique ID from an event.
/// The ID must be deterministic (same event = same ID) and unique (different
/// events = different IDs).
pub trait EventIdExtractor<T>: Send + Sync {
    /// Extract a unique event ID from an event
    ///
    /// # Arguments
    ///
    /// * `event` - The event to extract an ID from
    ///
    /// # Returns
    ///
    /// A unique EventId or an error if extraction fails
    fn extract_id(&self, event: &T) -> DeduplicationResult<EventId>;
}

// ============================================================================
// UUID-Based Extractors
// ============================================================================

/// Extract event ID from FeedbackEvent's UUID field
#[derive(Debug, Clone, Default)]
pub struct FeedbackEventUuidExtractor;

impl EventIdExtractor<FeedbackEvent> for FeedbackEventUuidExtractor {
    fn extract_id(&self, event: &FeedbackEvent) -> DeduplicationResult<EventId> {
        Ok(EventId::from(event.id))
    }
}

/// Extract event ID from CloudEvent's ID field
#[derive(Debug, Clone, Default)]
pub struct CloudEventUuidExtractor;

impl EventIdExtractor<CloudEvent> for CloudEventUuidExtractor {
    fn extract_id(&self, event: &CloudEvent) -> DeduplicationResult<EventId> {
        Ok(EventId::from_string(event.id.clone()))
    }
}

// ============================================================================
// Hash-Based Extractor
// ============================================================================

/// Extract ID by hashing event content
///
/// Generates a deterministic hash from the event's content, optionally
/// excluding fields like timestamps that change on every retry.
#[derive(Debug, Clone)]
pub struct ContentHashExtractor<H = Sha256> {
    /// Fields to include in hash (None = all fields)
    include_fields: Option<Vec<String>>,

    /// Fields to exclude from hash (e.g., "timestamp", "id")
    exclude_fields: Vec<String>,

    /// Hash algorithm marker
    _phantom: PhantomData<H>,
}

impl ContentHashExtractor<Sha256> {
    /// Create a SHA256-based hash extractor
    ///
    /// By default, excludes "timestamp", "id", and "processing_time" fields
    /// to ensure idempotent retries produce the same ID.
    pub fn sha256() -> Self {
        Self {
            include_fields: None,
            exclude_fields: vec![
                "timestamp".to_string(),
                "id".to_string(),
                "processing_time".to_string(),
            ],
            _phantom: PhantomData,
        }
    }

    /// Create a hash extractor with custom excluded fields
    pub fn sha256_excluding(exclude_fields: Vec<String>) -> Self {
        Self {
            include_fields: None,
            exclude_fields,
            _phantom: PhantomData,
        }
    }

    /// Create a hash extractor with specific included fields
    pub fn sha256_including(include_fields: Vec<String>) -> Self {
        Self {
            include_fields: Some(include_fields),
            exclude_fields: Vec::new(),
            _phantom: PhantomData,
        }
    }

    /// Filter JSON value based on include/exclude rules
    fn filter_fields(&self, value: &serde_json::Value) -> serde_json::Value {
        match value {
            serde_json::Value::Object(map) => {
                let filtered: serde_json::Map<String, serde_json::Value> = map
                    .iter()
                    .filter(|(k, _)| {
                        // If include_fields is set, only include specified fields
                        if let Some(ref includes) = self.include_fields {
                            return includes.contains(&k.to_string());
                        }
                        // Otherwise, exclude specified fields
                        !self.exclude_fields.contains(&k.to_string())
                    })
                    .map(|(k, v)| (k.clone(), self.filter_fields(v)))
                    .collect();
                serde_json::Value::Object(filtered)
            }
            serde_json::Value::Array(arr) => {
                let filtered: Vec<serde_json::Value> =
                    arr.iter().map(|v| self.filter_fields(v)).collect();
                serde_json::Value::Array(filtered)
            }
            _ => value.clone(),
        }
    }
}

impl<T, H> EventIdExtractor<T> for ContentHashExtractor<H>
where
    T: Serialize,
    H: Digest,
{
    fn extract_id(&self, event: &T) -> DeduplicationResult<EventId> {
        // Serialize event to JSON
        let json = serde_json::to_value(event)?;

        // Filter fields
        let filtered = self.filter_fields(&json);

        // Serialize to canonical string (sorted keys)
        let canonical = serde_json::to_string(&filtered)?;

        // Hash the canonical representation
        let mut hasher = H::new();
        hasher.update(canonical.as_bytes());
        let hash = hasher.finalize();

        Ok(EventId::from_bytes(&hash))
    }
}

// ============================================================================
// Composite Key Extractor
// ============================================================================

/// Field extraction strategy
#[derive(Debug, Clone)]
pub enum FieldExtractor {
    /// Extract from a named field in the event
    Field(String),

    /// Extract from metadata map (for FeedbackEvent)
    Metadata(String),

    /// Extract from nested path (e.g., "data.user.id")
    Path(Vec<String>),

    /// Extract using custom function
    Custom(Arc<dyn Fn(&serde_json::Value) -> Option<String> + Send + Sync>),
}

impl FieldExtractor {
    /// Extract value from JSON
    fn extract(&self, value: &serde_json::Value) -> Option<String> {
        match self {
            FieldExtractor::Field(name) => value.get(name)?.as_str().map(String::from),

            FieldExtractor::Metadata(key) => {
                value.get("metadata")?.get(key)?.as_str().map(String::from)
            }

            FieldExtractor::Path(path) => {
                let mut current = value;
                for segment in path {
                    current = current.get(segment)?;
                }
                current.as_str().map(String::from)
            }

            FieldExtractor::Custom(f) => f(value),
        }
    }
}

/// Extract ID from multiple fields combined
///
/// Creates a composite key from multiple event fields, useful for
/// multi-tenant systems or when events need scoped deduplication.
#[derive(Debug, Clone)]
pub struct CompositeExtractor {
    /// Field extractors in order
    fields: Vec<FieldExtractor>,

    /// Separator between fields
    separator: String,
}

impl CompositeExtractor {
    /// Create a new composite extractor
    ///
    /// # Arguments
    ///
    /// * `fields` - List of field extractors
    /// * `separator` - String to separate field values (default: ":")
    pub fn new(fields: Vec<FieldExtractor>, separator: String) -> Self {
        Self { fields, separator }
    }

    /// Create a composite extractor for model + session
    ///
    /// Extracts "model_id" and "session_id" from metadata.
    pub fn model_session() -> Self {
        Self {
            fields: vec![
                FieldExtractor::Metadata("model_id".to_string()),
                FieldExtractor::Metadata("session_id".to_string()),
            ],
            separator: ":".to_string(),
        }
    }

    /// Create a composite extractor for user + session
    pub fn user_session() -> Self {
        Self {
            fields: vec![
                FieldExtractor::Metadata("user_id".to_string()),
                FieldExtractor::Metadata("session_id".to_string()),
            ],
            separator: ":".to_string(),
        }
    }
}

impl<T> EventIdExtractor<T> for CompositeExtractor
where
    T: Serialize,
{
    fn extract_id(&self, event: &T) -> DeduplicationResult<EventId> {
        // Serialize to JSON
        let json = serde_json::to_value(event)?;

        // Extract all fields
        let mut parts = Vec::new();
        for field in &self.fields {
            let value = field.extract(&json).ok_or_else(|| {
                DeduplicationError::ExtractionFailed {
                    reason: format!("Failed to extract field: {:?}", field),
                }
            })?;
            parts.push(value);
        }

        // Join with separator
        let composite_id = parts.join(&self.separator);
        Ok(EventId::from_string(composite_id))
    }
}

// ============================================================================
// Time-Window Hash Extractor
// ============================================================================

/// Hash-based extractor with time window bucketing
///
/// Combines content hash with time bucket for time-bounded deduplication.
/// Events in different time windows will have different IDs even if content
/// is identical.
#[derive(Debug, Clone)]
pub struct TimeWindowHashExtractor {
    /// Base hash extractor
    base_extractor: ContentHashExtractor,

    /// Window size for bucketing (in seconds)
    window_size_secs: i64,
}

impl TimeWindowHashExtractor {
    /// Create a new time-window hash extractor
    ///
    /// # Arguments
    ///
    /// * `window_size_secs` - Window size in seconds
    pub fn new(window_size_secs: i64) -> Self {
        Self {
            base_extractor: ContentHashExtractor::sha256(),
            window_size_secs,
        }
    }

    /// Create with 1-hour windows
    pub fn hourly() -> Self {
        Self::new(3600)
    }

    /// Create with 1-day windows
    pub fn daily() -> Self {
        Self::new(86400)
    }
}

impl<T> EventIdExtractor<T> for TimeWindowHashExtractor
where
    T: Serialize,
{
    fn extract_id(&self, event: &T) -> DeduplicationResult<EventId> {
        // Get base hash
        let base_id = self.base_extractor.extract_id(event)?;

        // Get current time bucket
        let now = chrono::Utc::now();
        let bucket = now.timestamp() / self.window_size_secs;

        // Combine hash with time bucket
        let windowed_id = format!("{}:{}", bucket, base_id.as_str());
        Ok(EventId::from_string(windowed_id))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use llm_optimizer_types::events::{EventSource, EventType};

    #[test]
    fn test_feedback_event_uuid_extractor() {
        let event = FeedbackEvent::new(
            EventSource::Observatory,
            EventType::Metric,
            serde_json::json!({}),
        );

        let extractor = FeedbackEventUuidExtractor;
        let id = extractor.extract_id(&event).unwrap();

        assert_eq!(id.as_str(), event.id.to_string());
    }

    #[test]
    fn test_cloud_event_uuid_extractor() {
        let event = CloudEvent::new("test.event", "test-source", serde_json::json!({}));

        let extractor = CloudEventUuidExtractor;
        let id = extractor.extract_id(&event).unwrap();

        assert_eq!(id.as_str(), event.id);
    }

    #[test]
    fn test_content_hash_extractor() {
        #[derive(Serialize)]
        struct TestEvent {
            id: String,
            timestamp: i64,
            data: String,
        }

        let event = TestEvent {
            id: "test-1".to_string(),
            timestamp: 123456,
            data: "important-data".to_string(),
        };

        let extractor = ContentHashExtractor::sha256();
        let id = extractor.extract_id(&event).unwrap();

        // Same data should produce same hash (even with different timestamp/id)
        let event2 = TestEvent {
            id: "test-2".to_string(),
            timestamp: 789012,
            data: "important-data".to_string(),
        };

        let id2 = extractor.extract_id(&event2).unwrap();
        assert_eq!(id, id2); // Should be equal since timestamp/id are excluded
    }

    #[test]
    fn test_composite_extractor_model_session() {
        let mut event = FeedbackEvent::new(
            EventSource::Observatory,
            EventType::Metric,
            serde_json::json!({}),
        );
        event
            .metadata
            .insert("model_id".to_string(), "gpt-4".to_string());
        event
            .metadata
            .insert("session_id".to_string(), "sess-123".to_string());

        let extractor = CompositeExtractor::model_session();
        let id = extractor.extract_id(&event).unwrap();

        assert_eq!(id.as_str(), "gpt-4:sess-123");
    }

    #[test]
    fn test_time_window_hash_extractor() {
        #[derive(Serialize)]
        struct TestEvent {
            data: String,
        }

        let event = TestEvent {
            data: "test".to_string(),
        };

        let extractor = TimeWindowHashExtractor::new(3600);
        let id1 = extractor.extract_id(&event).unwrap();

        // Should start with time bucket
        assert!(id1.as_str().contains(':'));

        // Same event should produce same ID within same window
        let id2 = extractor.extract_id(&event).unwrap();
        assert_eq!(id1, id2);
    }
}