oximedia-batch 0.1.8

Comprehensive batch processing engine for OxiMedia
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
//! Audit log: track who submitted, modified, and cancelled each job.
//!
//! Every state transition and configuration change is recorded as an
//! [`AuditEntry`] with a timestamp, actor, action, and optional details.
//! The log is append-only, thread-safe, and supports filtering and export.

use std::collections::VecDeque;

use parking_lot::RwLock;
use serde::{Deserialize, Serialize};

use crate::types::JobId;

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

/// The kind of action that was performed on a job.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AuditAction {
    /// A new job was submitted.
    JobSubmitted,
    /// A job was started by the execution engine.
    JobStarted,
    /// A job completed successfully.
    JobCompleted,
    /// A job failed.
    JobFailed,
    /// A job was cancelled by an operator.
    JobCancelled,
    /// A job was retried.
    JobRetried {
        /// Which attempt this is (1-based).
        attempt: u32,
    },
    /// A job's priority was changed.
    PriorityChanged {
        /// The old priority level.
        old_priority: String,
        /// The new priority level.
        new_priority: String,
    },
    /// A job was moved to the dead letter queue.
    MovedToDeadLetter,
    /// A job was replayed from the dead letter queue.
    ReplayedFromDeadLetter,
    /// A job's configuration was modified.
    ConfigModified {
        /// Which field was changed.
        field: String,
    },
    /// A job's dependency was added or removed.
    DependencyChanged {
        /// Description of the change.
        change: String,
    },
    /// A checkpoint was saved for a job.
    CheckpointSaved {
        /// Step index at which the checkpoint was taken.
        step: usize,
    },
    /// A custom / free-form action.
    Custom {
        /// Action label.
        action: String,
    },
}

impl std::fmt::Display for AuditAction {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::JobSubmitted => write!(f, "job_submitted"),
            Self::JobStarted => write!(f, "job_started"),
            Self::JobCompleted => write!(f, "job_completed"),
            Self::JobFailed => write!(f, "job_failed"),
            Self::JobCancelled => write!(f, "job_cancelled"),
            Self::JobRetried { attempt } => write!(f, "job_retried(attempt={attempt})"),
            Self::PriorityChanged {
                old_priority,
                new_priority,
            } => write!(f, "priority_changed({old_priority}->{new_priority})"),
            Self::MovedToDeadLetter => write!(f, "moved_to_dead_letter"),
            Self::ReplayedFromDeadLetter => write!(f, "replayed_from_dead_letter"),
            Self::ConfigModified { field } => write!(f, "config_modified({field})"),
            Self::DependencyChanged { change } => write!(f, "dependency_changed({change})"),
            Self::CheckpointSaved { step } => write!(f, "checkpoint_saved(step={step})"),
            Self::Custom { action } => write!(f, "custom({action})"),
        }
    }
}

/// Identifies who performed an action.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Actor {
    /// The batch engine (system-initiated).
    System,
    /// A human user.
    User {
        /// Username or identifier.
        username: String,
    },
    /// An API client.
    ApiClient {
        /// Client identifier or API key prefix.
        client_id: String,
    },
    /// A scheduled task or cron trigger.
    Scheduler {
        /// Schedule name or expression.
        schedule: String,
    },
}

impl std::fmt::Display for Actor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::System => write!(f, "system"),
            Self::User { username } => write!(f, "user:{username}"),
            Self::ApiClient { client_id } => write!(f, "api:{client_id}"),
            Self::Scheduler { schedule } => write!(f, "scheduler:{schedule}"),
        }
    }
}

/// A single audit log entry.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditEntry {
    /// Unique entry ID.
    pub entry_id: String,
    /// The job this action pertains to.
    pub job_id: JobId,
    /// Who performed the action.
    pub actor: Actor,
    /// What was done.
    pub action: AuditAction,
    /// Optional human-readable details / reason.
    pub details: Option<String>,
    /// Unix timestamp (seconds since epoch).
    pub timestamp_secs: u64,
    /// IP address or origin (if available).
    pub source_ip: Option<String>,
}

impl AuditEntry {
    /// Create a new audit entry.
    #[must_use]
    pub fn new(job_id: JobId, actor: Actor, action: AuditAction) -> Self {
        Self {
            entry_id: uuid::Uuid::new_v4().to_string(),
            job_id,
            actor,
            action,
            details: None,
            timestamp_secs: current_timestamp(),
            source_ip: None,
        }
    }

    /// Builder: add details.
    #[must_use]
    pub fn with_details(mut self, details: impl Into<String>) -> Self {
        self.details = Some(details.into());
        self
    }

    /// Builder: add source IP.
    #[must_use]
    pub fn with_source_ip(mut self, ip: impl Into<String>) -> Self {
        self.source_ip = Some(ip.into());
        self
    }

    /// Age of this entry in seconds.
    #[must_use]
    pub fn age_secs(&self) -> u64 {
        current_timestamp().saturating_sub(self.timestamp_secs)
    }
}

// ---------------------------------------------------------------------------
// Audit log
// ---------------------------------------------------------------------------

/// Maximum entries kept in memory before oldest are evicted.
const DEFAULT_MAX_ENTRIES: usize = 100_000;

/// Append-only, thread-safe audit log.
#[derive(Debug)]
pub struct AuditLog {
    entries: RwLock<VecDeque<AuditEntry>>,
    max_entries: usize,
}

impl AuditLog {
    /// Create a new audit log with the default capacity.
    #[must_use]
    pub fn new() -> Self {
        Self {
            entries: RwLock::new(VecDeque::new()),
            max_entries: DEFAULT_MAX_ENTRIES,
        }
    }

    /// Create a new audit log with a custom maximum capacity.
    #[must_use]
    pub fn with_capacity(max_entries: usize) -> Self {
        Self {
            entries: RwLock::new(VecDeque::new()),
            max_entries: max_entries.max(1),
        }
    }

    /// Append an entry to the log.
    ///
    /// If the log exceeds `max_entries`, the oldest entries are evicted.
    pub fn log(&self, entry: AuditEntry) {
        let mut entries = self.entries.write();
        entries.push_back(entry);
        while entries.len() > self.max_entries {
            entries.pop_front();
        }
    }

    /// Convenience: log a simple action with system actor.
    pub fn log_system(&self, job_id: JobId, action: AuditAction) {
        self.log(AuditEntry::new(job_id, Actor::System, action));
    }

    /// Convenience: log a simple action with a user actor.
    pub fn log_user(&self, job_id: JobId, username: impl Into<String>, action: AuditAction) {
        self.log(AuditEntry::new(
            job_id,
            Actor::User {
                username: username.into(),
            },
            action,
        ));
    }

    /// Total number of entries in the log.
    #[must_use]
    pub fn len(&self) -> usize {
        self.entries.read().len()
    }

    /// Whether the log is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.entries.read().is_empty()
    }

    /// Retrieve all entries for a specific job, in chronological order.
    #[must_use]
    pub fn entries_for_job(&self, job_id: &JobId) -> Vec<AuditEntry> {
        self.entries
            .read()
            .iter()
            .filter(|e| &e.job_id == job_id)
            .cloned()
            .collect()
    }

    /// Retrieve all entries by a specific actor.
    #[must_use]
    pub fn entries_by_actor(&self, actor: &Actor) -> Vec<AuditEntry> {
        self.entries
            .read()
            .iter()
            .filter(|e| &e.actor == actor)
            .cloned()
            .collect()
    }

    /// Retrieve entries matching a predicate, newest first.
    #[must_use]
    pub fn query<F>(&self, predicate: F) -> Vec<AuditEntry>
    where
        F: Fn(&AuditEntry) -> bool,
    {
        self.entries
            .read()
            .iter()
            .rev()
            .filter(|e| predicate(e))
            .cloned()
            .collect()
    }

    /// Retrieve the most recent `n` entries, newest first.
    #[must_use]
    pub fn recent(&self, n: usize) -> Vec<AuditEntry> {
        self.entries.read().iter().rev().take(n).cloned().collect()
    }

    /// Retrieve entries within a time range (inclusive).
    #[must_use]
    pub fn entries_in_range(&self, from_secs: u64, to_secs: u64) -> Vec<AuditEntry> {
        self.entries
            .read()
            .iter()
            .filter(|e| e.timestamp_secs >= from_secs && e.timestamp_secs <= to_secs)
            .cloned()
            .collect()
    }

    /// Count entries by action type for a specific job.
    #[must_use]
    pub fn action_counts_for_job(
        &self,
        job_id: &JobId,
    ) -> std::collections::HashMap<String, usize> {
        let mut counts = std::collections::HashMap::new();
        for entry in self.entries.read().iter() {
            if &entry.job_id == job_id {
                *counts.entry(entry.action.to_string()).or_insert(0) += 1;
            }
        }
        counts
    }

    /// Export the entire log as a JSON string.
    ///
    /// # Errors
    ///
    /// Returns a serialization error if JSON encoding fails.
    pub fn export_json(&self) -> std::result::Result<String, serde_json::Error> {
        let entries: Vec<AuditEntry> = self.entries.read().iter().cloned().collect();
        serde_json::to_string_pretty(&entries)
    }

    /// Clear the entire log.
    pub fn clear(&self) {
        self.entries.write().clear();
    }

    /// Summary statistics.
    #[must_use]
    pub fn stats(&self) -> AuditStats {
        let entries = self.entries.read();
        let total = entries.len();
        let mut by_action = std::collections::HashMap::new();
        let mut by_actor = std::collections::HashMap::new();
        let mut unique_jobs = std::collections::HashSet::new();

        for e in entries.iter() {
            *by_action.entry(e.action.to_string()).or_insert(0usize) += 1;
            *by_actor.entry(e.actor.to_string()).or_insert(0usize) += 1;
            unique_jobs.insert(e.job_id.as_str().to_string());
        }

        AuditStats {
            total_entries: total,
            unique_jobs: unique_jobs.len(),
            by_action,
            by_actor,
        }
    }
}

impl Default for AuditLog {
    fn default() -> Self {
        Self::new()
    }
}

/// Summary statistics for the audit log.
#[derive(Debug, Clone)]
pub struct AuditStats {
    /// Total number of entries.
    pub total_entries: usize,
    /// Number of distinct jobs referenced.
    pub unique_jobs: usize,
    /// Entry count by action type.
    pub by_action: std::collections::HashMap<String, usize>,
    /// Entry count by actor.
    pub by_actor: std::collections::HashMap<String, usize>,
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn current_timestamp() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn jid(s: &str) -> JobId {
        JobId::from(s)
    }

    #[test]
    fn test_audit_log_basic() {
        let log = AuditLog::new();
        assert!(log.is_empty());
        log.log_system(jid("j1"), AuditAction::JobSubmitted);
        assert_eq!(log.len(), 1);
        assert!(!log.is_empty());
    }

    #[test]
    fn test_audit_log_entries_for_job() {
        let log = AuditLog::new();
        log.log_system(jid("j1"), AuditAction::JobSubmitted);
        log.log_system(jid("j1"), AuditAction::JobStarted);
        log.log_system(jid("j2"), AuditAction::JobSubmitted);
        let j1_entries = log.entries_for_job(&jid("j1"));
        assert_eq!(j1_entries.len(), 2);
    }

    #[test]
    fn test_audit_log_entries_by_actor() {
        let log = AuditLog::new();
        log.log_user(jid("j1"), "alice", AuditAction::JobSubmitted);
        log.log_user(jid("j2"), "bob", AuditAction::JobSubmitted);
        log.log_user(jid("j3"), "alice", AuditAction::JobCancelled);

        let alice_entries = log.entries_by_actor(&Actor::User {
            username: "alice".into(),
        });
        assert_eq!(alice_entries.len(), 2);
    }

    #[test]
    fn test_audit_log_recent() {
        let log = AuditLog::new();
        for i in 0..10 {
            log.log_system(jid(&format!("j{i}")), AuditAction::JobSubmitted);
        }
        let recent = log.recent(3);
        assert_eq!(recent.len(), 3);
        // Newest first: j9, j8, j7
        assert_eq!(recent[0].job_id.as_str(), "j9");
        assert_eq!(recent[1].job_id.as_str(), "j8");
        assert_eq!(recent[2].job_id.as_str(), "j7");
    }

    #[test]
    fn test_audit_log_query_filter() {
        let log = AuditLog::new();
        log.log_system(jid("j1"), AuditAction::JobSubmitted);
        log.log_system(jid("j1"), AuditAction::JobStarted);
        log.log_system(jid("j1"), AuditAction::JobFailed);
        log.log_system(jid("j1"), AuditAction::JobRetried { attempt: 1 });
        log.log_system(jid("j1"), AuditAction::JobCompleted);

        let failures = log.query(|e| matches!(&e.action, AuditAction::JobFailed));
        assert_eq!(failures.len(), 1);
    }

    #[test]
    fn test_audit_log_eviction() {
        let log = AuditLog::with_capacity(5);
        for i in 0..10 {
            log.log_system(jid(&format!("j{i}")), AuditAction::JobSubmitted);
        }
        assert_eq!(log.len(), 5);
        // Should have j5..j9 (oldest evicted).
        let entries = log.recent(5);
        assert_eq!(entries[0].job_id.as_str(), "j9");
        assert_eq!(entries[4].job_id.as_str(), "j5");
    }

    #[test]
    fn test_audit_log_action_counts() {
        let log = AuditLog::new();
        log.log_system(jid("j1"), AuditAction::JobSubmitted);
        log.log_system(jid("j1"), AuditAction::JobStarted);
        log.log_system(jid("j1"), AuditAction::JobFailed);
        log.log_system(jid("j1"), AuditAction::JobRetried { attempt: 1 });
        log.log_system(jid("j1"), AuditAction::JobStarted);
        log.log_system(jid("j1"), AuditAction::JobCompleted);

        let counts = log.action_counts_for_job(&jid("j1"));
        assert_eq!(counts.get("job_submitted").copied().unwrap_or(0), 1);
        assert_eq!(counts.get("job_started").copied().unwrap_or(0), 2);
        assert_eq!(counts.get("job_completed").copied().unwrap_or(0), 1);
        assert_eq!(counts.get("job_failed").copied().unwrap_or(0), 1);
    }

    #[test]
    fn test_audit_log_clear() {
        let log = AuditLog::new();
        log.log_system(jid("j1"), AuditAction::JobSubmitted);
        log.clear();
        assert!(log.is_empty());
    }

    #[test]
    fn test_audit_log_stats() {
        let log = AuditLog::new();
        log.log_system(jid("j1"), AuditAction::JobSubmitted);
        log.log_user(jid("j2"), "alice", AuditAction::JobSubmitted);
        log.log_system(jid("j1"), AuditAction::JobCompleted);

        let stats = log.stats();
        assert_eq!(stats.total_entries, 3);
        assert_eq!(stats.unique_jobs, 2);
        assert_eq!(
            stats.by_action.get("job_submitted").copied().unwrap_or(0),
            2
        );
    }

    #[test]
    fn test_audit_entry_with_details() {
        let entry = AuditEntry::new(jid("j1"), Actor::System, AuditAction::JobFailed)
            .with_details("OOM killed")
            .with_source_ip("192.168.1.1");
        assert_eq!(entry.details, Some("OOM killed".to_string()));
        assert_eq!(entry.source_ip, Some("192.168.1.1".to_string()));
    }

    #[test]
    fn test_audit_entry_age() {
        let entry = AuditEntry::new(jid("j1"), Actor::System, AuditAction::JobSubmitted);
        assert!(entry.age_secs() < 5);
    }

    #[test]
    fn test_audit_action_display() {
        assert_eq!(AuditAction::JobSubmitted.to_string(), "job_submitted");
        assert_eq!(AuditAction::JobCancelled.to_string(), "job_cancelled");
        assert_eq!(
            AuditAction::JobRetried { attempt: 3 }.to_string(),
            "job_retried(attempt=3)"
        );
        assert_eq!(
            AuditAction::PriorityChanged {
                old_priority: "Normal".into(),
                new_priority: "High".into(),
            }
            .to_string(),
            "priority_changed(Normal->High)"
        );
    }

    #[test]
    fn test_actor_display() {
        assert_eq!(Actor::System.to_string(), "system");
        assert_eq!(
            Actor::User {
                username: "bob".into()
            }
            .to_string(),
            "user:bob"
        );
        assert_eq!(
            Actor::ApiClient {
                client_id: "cli-1".into()
            }
            .to_string(),
            "api:cli-1"
        );
    }

    #[test]
    fn test_audit_log_export_json() {
        let log = AuditLog::new();
        log.log_system(jid("j1"), AuditAction::JobSubmitted);
        let json = log.export_json().expect("JSON export should succeed");
        assert!(json.contains("job_submitted"));
        assert!(json.contains("j1"));
    }

    #[test]
    fn test_audit_log_entries_in_range() {
        let log = AuditLog::new();
        let now = current_timestamp();

        let mut entry1 = AuditEntry::new(jid("j1"), Actor::System, AuditAction::JobSubmitted);
        entry1.timestamp_secs = now - 100;
        log.log(entry1);

        let mut entry2 = AuditEntry::new(jid("j2"), Actor::System, AuditAction::JobStarted);
        entry2.timestamp_secs = now - 50;
        log.log(entry2);

        let entry3 = AuditEntry::new(jid("j3"), Actor::System, AuditAction::JobCompleted);
        log.log(entry3);

        let range = log.entries_in_range(now - 60, now + 10);
        // Should include j2 and j3 but not j1.
        assert_eq!(range.len(), 2);
    }

    #[test]
    fn test_audit_log_default() {
        let log = AuditLog::default();
        assert!(log.is_empty());
    }

    #[test]
    fn test_audit_log_custom_action() {
        let log = AuditLog::new();
        log.log(AuditEntry::new(
            jid("j1"),
            Actor::System,
            AuditAction::Custom {
                action: "manual_override".into(),
            },
        ));
        let entries = log.entries_for_job(&jid("j1"));
        assert_eq!(entries.len(), 1);
        assert_eq!(
            entries[0].action,
            AuditAction::Custom {
                action: "manual_override".into()
            }
        );
    }

    #[test]
    fn test_audit_log_checkpoint_action() {
        let log = AuditLog::new();
        log.log_system(jid("j1"), AuditAction::CheckpointSaved { step: 5 });
        let entries = log.entries_for_job(&jid("j1"));
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].action.to_string(), "checkpoint_saved(step=5)");
    }
}