Skip to main content

cmtraceopen_parser/intune/
timeline.rs

1use std::collections::HashSet;
2use std::path::Path;
3
4use chrono::NaiveDateTime;
5
6use super::models::{IntuneEvent, IntuneEventType, IntuneStatus, IntuneTimestampBounds};
7
8struct TimelineEvent {
9    event: IntuneEvent,
10    parsed_time: Option<NaiveDateTime>,
11    source_identity: String,
12}
13
14#[derive(Hash, PartialEq, Eq)]
15struct PairedEventKey {
16    guid: Option<String>,
17    event_type: IntuneEventType,
18    source_identity: String,
19}
20
21#[derive(Hash, PartialEq, Eq)]
22struct ExactEventKey {
23    source_identity: String,
24    event_type: IntuneEventType,
25    status: IntuneStatus,
26    start_time: Option<String>,
27    end_time: Option<String>,
28    guid: Option<String>,
29    name: String,
30    detail: String,
31}
32
33/// Sort events chronologically and deduplicate paired events.
34/// After `event_tracker::extract_events` has already paired start/end events,
35/// this function cleans up the timeline by:
36/// 1. Removing "end" events that were consumed by pairing
37/// 2. Sorting by start_time
38/// 3. Re-assigning sequential IDs
39pub fn build_timeline(events: Vec<IntuneEvent>) -> Vec<IntuneEvent> {
40    let mut timeline = deduplicate_events(events);
41
42    // Sort by the cached parsed timestamp first, then source+line for deterministic ordering.
43    timeline.sort_by(|a, b| {
44        a.parsed_time
45            .cmp(&b.parsed_time)
46            .then_with(|| a.event.source_file.cmp(&b.event.source_file))
47            .then_with(|| a.event.line_number.cmp(&b.event.line_number))
48            .then_with(|| a.event.name.cmp(&b.event.name))
49    });
50
51    // Re-assign sequential IDs and populate epoch timestamps
52    for (i, entry) in timeline.iter_mut().enumerate() {
53        entry.event.id = i as u64;
54        entry.event.start_time_epoch = entry
55            .event
56            .start_time
57            .as_deref()
58            .and_then(parse_timestamp)
59            .map(|dt| dt.and_utc().timestamp_millis());
60        entry.event.end_time_epoch = entry
61            .event
62            .end_time
63            .as_deref()
64            .and_then(parse_timestamp)
65            .map(|dt| dt.and_utc().timestamp_millis());
66    }
67
68    timeline.into_iter().map(|entry| entry.event).collect()
69}
70
71/// Remove duplicate or already-consumed events before timeline ordering.
72/// This keeps paired start events, drops consumed completion rows when they
73/// still leak through, and removes exact duplicate entries from the same file.
74fn deduplicate_events(events: Vec<IntuneEvent>) -> Vec<TimelineEvent> {
75    let prepared_events: Vec<TimelineEvent> = events
76        .into_iter()
77        .map(|event| TimelineEvent {
78            parsed_time: parsed_event_time(&event),
79            source_identity: normalized_source_identity(&event.source_file),
80            event,
81        })
82        .collect();
83
84    let mut paired_keys: HashSet<PairedEventKey> = HashSet::with_capacity(prepared_events.len());
85    for prepared_event in &prepared_events {
86        if prepared_event.event.end_time.is_some() {
87            paired_keys.insert(PairedEventKey {
88                guid: prepared_event.event.guid.clone(),
89                event_type: prepared_event.event.event_type,
90                source_identity: prepared_event.source_identity.clone(),
91            });
92        }
93    }
94
95    let mut seen_exact: HashSet<ExactEventKey> = HashSet::with_capacity(prepared_events.len());
96    let mut result = Vec::with_capacity(prepared_events.len());
97    for prepared_event in prepared_events {
98        let exact_key = ExactEventKey {
99            source_identity: prepared_event.source_identity.clone(),
100            event_type: prepared_event.event.event_type,
101            status: prepared_event.event.status,
102            start_time: prepared_event.event.start_time.clone(),
103            end_time: prepared_event.event.end_time.clone(),
104            guid: prepared_event.event.guid.clone(),
105            name: prepared_event.event.name.clone(),
106            detail: prepared_event.event.detail.clone(),
107        };
108
109        if !seen_exact.insert(exact_key) {
110            continue;
111        }
112
113        if prepared_event.event.end_time.is_some() {
114            result.push(prepared_event);
115            continue;
116        }
117
118        if prepared_event.event.guid.is_none() {
119            result.push(prepared_event);
120            continue;
121        }
122
123        let key = PairedEventKey {
124            guid: prepared_event.event.guid.clone(),
125            event_type: prepared_event.event.event_type,
126            source_identity: prepared_event.source_identity.clone(),
127        };
128        let is_consumed_end = matches!(
129            prepared_event.event.status,
130            IntuneStatus::Success | IntuneStatus::Failed | IntuneStatus::Timeout
131        ) && paired_keys.contains(&key);
132
133        if !is_consumed_end {
134            result.push(prepared_event);
135        }
136    }
137
138    result
139}
140
141pub fn normalized_source_identity(source_file: &str) -> String {
142    let stem = Path::new(source_file)
143        .file_stem()
144        .map(|name| name.to_string_lossy().to_string())
145        .unwrap_or_else(|| source_file.to_string());
146
147    for separator in [".", "-", "_"] {
148        if let Some((base, suffix)) = stem.rsplit_once(separator) {
149            if is_rotation_suffix(suffix) {
150                return base.to_ascii_lowercase();
151            }
152        }
153    }
154
155    stem.to_ascii_lowercase()
156}
157
158fn is_rotation_suffix(value: &str) -> bool {
159    let normalized = value.trim().to_ascii_lowercase();
160    if normalized.is_empty() {
161        return false;
162    }
163
164    if normalized.chars().all(|ch| ch.is_ascii_digit()) {
165        return true;
166    }
167
168    if normalized.starts_with("lo_") || normalized == "bak" || normalized == "old" {
169        return true;
170    }
171
172    normalized.len() == 8 && normalized.chars().all(|ch| ch.is_ascii_digit())
173}
174
175fn parsed_event_time(event: &IntuneEvent) -> Option<NaiveDateTime> {
176    event
177        .start_time
178        .as_deref()
179        .and_then(parse_timestamp)
180        .or_else(|| event.end_time.as_deref().and_then(parse_timestamp))
181}
182
183pub fn parse_timestamp(value: &str) -> Option<NaiveDateTime> {
184    const FORMATS: &[&str] = &[
185        "%m-%d-%Y %H:%M:%S%.f",
186        "%Y-%m-%d %H:%M:%S%.f",
187        "%Y/%m/%d %H:%M:%S%.f",
188    ];
189
190    for format in FORMATS {
191        if let Ok(parsed) = NaiveDateTime::parse_from_str(value, format) {
192            return Some(parsed);
193        }
194    }
195
196    // RFC 3339 / ISO 8601 with timezone (e.g. "2024-01-15T10:30:00.000Z")
197    // IME parser produces timestamp_utc in this format via to_rfc3339_opts().
198    chrono::DateTime::parse_from_rfc3339(value)
199        .map(|dt| dt.naive_utc())
200        .ok()
201}
202
203pub fn calculate_timestamp_bounds(events: &[IntuneEvent]) -> Option<IntuneTimestampBounds> {
204    let mut earliest: Option<(NaiveDateTime, String)> = None;
205    let mut latest: Option<(NaiveDateTime, String)> = None;
206
207    for event in events {
208        if let Some(timestamp) = event.start_time.as_deref() {
209            update_timestamp_bounds(&mut earliest, &mut latest, timestamp);
210        }
211
212        if let Some(timestamp) = event.end_time.as_deref() {
213            update_timestamp_bounds(&mut earliest, &mut latest, timestamp);
214        }
215    }
216
217    match (earliest, latest) {
218        (Some((_, first_timestamp)), Some((_, last_timestamp))) => Some(IntuneTimestampBounds {
219            first_timestamp: Some(first_timestamp),
220            last_timestamp: Some(last_timestamp),
221        }),
222        _ => None,
223    }
224}
225
226fn update_timestamp_bounds(
227    earliest: &mut Option<(NaiveDateTime, String)>,
228    latest: &mut Option<(NaiveDateTime, String)>,
229    value: &str,
230) {
231    let Some(parsed) = parse_timestamp(value) else {
232        return;
233    };
234
235    match earliest {
236        Some((current, current_raw))
237            if parsed > *current || (parsed == *current && value >= current_raw.as_str()) => {}
238        _ => *earliest = Some((parsed, value.to_string())),
239    }
240
241    match latest {
242        Some((current, current_raw))
243            if parsed < *current || (parsed == *current && value <= current_raw.as_str()) => {}
244        _ => *latest = Some((parsed, value.to_string())),
245    }
246}
247
248/// Calculate the time span covered by a set of events.
249/// Returns a human-readable string like "2h 15m 30s".
250pub fn calculate_time_span(events: &[IntuneEvent]) -> Option<String> {
251    let bounds = calculate_timestamp_bounds(events)?;
252    let start = parse_timestamp(bounds.first_timestamp.as_deref()?)?;
253    let end = parse_timestamp(bounds.last_timestamp.as_deref()?)?;
254    let duration = end.signed_duration_since(start).num_milliseconds() as f64 / 1000.0;
255
256    if duration < 0.0 {
257        None
258    } else {
259        Some(format_duration(duration))
260    }
261}
262
263/// Estimate duration between two timestamp strings in seconds.
264#[cfg(test)]
265fn estimate_duration_secs(start: &str, end: &str) -> Option<f64> {
266    let parse_seconds = |ts: &str| -> Option<f64> {
267        let time_part = ts.split_whitespace().last()?;
268        let parts: Vec<&str> = time_part.split(':').collect();
269        if parts.len() >= 3 {
270            let h: f64 = parts[0].parse().ok()?;
271            let m: f64 = parts[1].parse().ok()?;
272            let s: f64 = parts[2].parse().ok()?;
273            Some(h * 3600.0 + m * 60.0 + s)
274        } else {
275            None
276        }
277    };
278
279    let start_secs = parse_seconds(start)?;
280    let end_secs = parse_seconds(end)?;
281
282    let diff = end_secs - start_secs;
283    if diff >= 0.0 {
284        Some(diff)
285    } else {
286        Some(diff + 86400.0) // crossed midnight
287    }
288}
289
290/// Format seconds into a human-readable duration string.
291fn format_duration(total_secs: f64) -> String {
292    let total = total_secs as u64;
293    let hours = total / 3600;
294    let minutes = (total % 3600) / 60;
295    let seconds = total % 60;
296
297    if hours > 0 {
298        format!("{}h {}m {}s", hours, minutes, seconds)
299    } else if minutes > 0 {
300        format!("{}m {}s", minutes, seconds)
301    } else {
302        format!("{}s", seconds)
303    }
304}
305
306#[cfg(test)]
307mod tests {
308    use super::*;
309    use chrono::Timelike;
310
311    #[test]
312    fn test_format_duration() {
313        assert_eq!(format_duration(0.0), "0s");
314        assert_eq!(format_duration(45.0), "45s");
315        assert_eq!(format_duration(125.0), "2m 5s");
316        assert_eq!(format_duration(3661.0), "1h 1m 1s");
317    }
318
319    #[test]
320    fn test_estimate_duration() {
321        let d = estimate_duration_secs("01-01-2024 10:00:00.000", "01-01-2024 10:05:30.000");
322        assert_eq!(d, Some(330.0));
323    }
324
325    #[test]
326    fn test_estimate_duration_midnight() {
327        let d = estimate_duration_secs("01-01-2024 23:59:00.000", "01-02-2024 00:01:00.000");
328        assert_eq!(d, Some(120.0));
329    }
330
331    #[test]
332    fn calculate_timestamp_bounds_prefers_parsed_order() {
333        let bounds = calculate_timestamp_bounds(&[
334            IntuneEvent {
335                id: 0,
336                event_type: super::super::models::IntuneEventType::Win32App,
337                name: "Later".to_string(),
338                guid: None,
339                status: IntuneStatus::InProgress,
340                start_time: Some("12-31-2024 10:00:00.000".to_string()),
341                end_time: None,
342                duration_secs: None,
343                error_code: None,
344                detail: "later".to_string(),
345                source_file: "b.log".to_string(),
346                line_number: 2,
347                start_time_epoch: None,
348                end_time_epoch: None,
349                script_body: None,
350                parent_app_guid: None,
351            },
352            IntuneEvent {
353                id: 1,
354                event_type: super::super::models::IntuneEventType::Win32App,
355                name: "Earlier".to_string(),
356                guid: None,
357                status: IntuneStatus::InProgress,
358                start_time: Some("01-01-2024 10:00:00.000".to_string()),
359                end_time: Some("01-01-2024 10:05:00.000".to_string()),
360                duration_secs: None,
361                error_code: None,
362                detail: "earlier".to_string(),
363                source_file: "a.log".to_string(),
364                line_number: 1,
365                start_time_epoch: None,
366                end_time_epoch: None,
367                script_body: None,
368                parent_app_guid: None,
369            },
370        ])
371        .expect("timestamp bounds");
372
373        assert_eq!(
374            bounds.first_timestamp.as_deref(),
375            Some("01-01-2024 10:00:00.000")
376        );
377        assert_eq!(
378            bounds.last_timestamp.as_deref(),
379            Some("12-31-2024 10:00:00.000")
380        );
381    }
382
383    #[test]
384    fn calculate_time_span_uses_parsed_dates() {
385        let events = vec![
386            IntuneEvent {
387                id: 0,
388                event_type: super::super::models::IntuneEventType::Win32App,
389                name: "Later".to_string(),
390                guid: None,
391                status: IntuneStatus::InProgress,
392                start_time: Some("12-31-2024 10:00:00.000".to_string()),
393                end_time: None,
394                duration_secs: None,
395                error_code: None,
396                detail: "later".to_string(),
397                source_file: "b.log".to_string(),
398                line_number: 2,
399                start_time_epoch: None,
400                end_time_epoch: None,
401                script_body: None,
402                parent_app_guid: None,
403            },
404            IntuneEvent {
405                id: 1,
406                event_type: super::super::models::IntuneEventType::Win32App,
407                name: "Earlier".to_string(),
408                guid: None,
409                status: IntuneStatus::InProgress,
410                start_time: Some("01-01-2024 10:00:00.000".to_string()),
411                end_time: Some("01-01-2024 10:05:00.000".to_string()),
412                duration_secs: None,
413                error_code: None,
414                detail: "earlier".to_string(),
415                source_file: "a.log".to_string(),
416                line_number: 1,
417                start_time_epoch: None,
418                end_time_epoch: None,
419                script_body: None,
420                parent_app_guid: None,
421            },
422        ];
423
424        assert_eq!(
425            calculate_time_span(&events),
426            Some("8760h 0m 0s".to_string())
427        );
428    }
429
430    #[test]
431    fn build_timeline_sorts_by_parsed_timestamp() {
432        let timeline = build_timeline(vec![
433            IntuneEvent {
434                id: 0,
435                event_type: super::super::models::IntuneEventType::Win32App,
436                name: "Later".to_string(),
437                guid: None,
438                status: IntuneStatus::InProgress,
439                start_time: Some("12-31-2024 10:00:00.000".to_string()),
440                end_time: None,
441                duration_secs: None,
442                error_code: None,
443                detail: "later".to_string(),
444                source_file: "b.log".to_string(),
445                line_number: 2,
446                start_time_epoch: None,
447                end_time_epoch: None,
448                script_body: None,
449                parent_app_guid: None,
450            },
451            IntuneEvent {
452                id: 1,
453                event_type: super::super::models::IntuneEventType::Win32App,
454                name: "Earlier".to_string(),
455                guid: None,
456                status: IntuneStatus::InProgress,
457                start_time: Some("01-01-2024 10:00:00.000".to_string()),
458                end_time: None,
459                duration_secs: None,
460                error_code: None,
461                detail: "earlier".to_string(),
462                source_file: "a.log".to_string(),
463                line_number: 1,
464                start_time_epoch: None,
465                end_time_epoch: None,
466                script_body: None,
467                parent_app_guid: None,
468            },
469        ]);
470
471        assert_eq!(timeline[0].name, "Earlier");
472        assert_eq!(timeline[1].name, "Later");
473    }
474
475    #[test]
476    fn build_timeline_deduplicates_exact_duplicate_events() {
477        let event = IntuneEvent {
478            id: 0,
479            event_type: super::super::models::IntuneEventType::Win32App,
480            name: "Duplicate".to_string(),
481            guid: Some("a1b2c3d4-e5f6-7890-abcd-ef1234567890".to_string()),
482            status: IntuneStatus::Success,
483            start_time: Some("01-01-2024 10:00:00.000".to_string()),
484            end_time: None,
485            duration_secs: None,
486            error_code: None,
487            detail: "same".to_string(),
488            source_file: "a.log".to_string(),
489            line_number: 1,
490            start_time_epoch: None,
491            end_time_epoch: None,
492            script_body: None,
493            parent_app_guid: None,
494        };
495
496        let timeline = build_timeline(vec![event.clone(), event]);
497        assert_eq!(timeline.len(), 1);
498    }
499
500    #[test]
501    fn build_timeline_deduplicates_rotated_duplicate_events() {
502        let event = IntuneEvent {
503            id: 0,
504            event_type: super::super::models::IntuneEventType::ContentDownload,
505            name: "AppWorkload Hash Validation (abcd1234...)".to_string(),
506            guid: Some("a1b2c3d4-e5f6-7890-abcd-ef1234567890".to_string()),
507            status: IntuneStatus::Failed,
508            start_time: Some("01-15-2024 10:00:00.000".to_string()),
509            end_time: None,
510            duration_secs: None,
511            error_code: None,
512            detail: "Hash validation failed after staging cached content".to_string(),
513            source_file: "C:/Logs/AppWorkload.log".to_string(),
514            line_number: 12,
515            start_time_epoch: None,
516            end_time_epoch: None,
517            script_body: None,
518            parent_app_guid: None,
519        };
520
521        let mut rotated = event.clone();
522        rotated.id = 1;
523        rotated.source_file = "C:/Logs/AppWorkload-1.log".to_string();
524        rotated.line_number = 44;
525
526        let timeline = build_timeline(vec![event, rotated]);
527        assert_eq!(timeline.len(), 1);
528    }
529
530    #[test]
531    fn build_timeline_populates_epoch_fields() {
532        let timeline = build_timeline(vec![
533            IntuneEvent {
534                id: 0,
535                event_type: IntuneEventType::Win32App,
536                name: "Test".to_string(),
537                guid: None,
538                status: IntuneStatus::Success,
539                start_time: Some("01-15-2024 10:30:00.000".to_string()),
540                end_time: Some("01-15-2024 10:35:00.000".to_string()),
541                duration_secs: Some(300.0),
542                error_code: None,
543                detail: "test".to_string(),
544                source_file: "a.log".to_string(),
545                line_number: 1,
546                start_time_epoch: None,
547                end_time_epoch: None,
548                script_body: None,
549                parent_app_guid: None,
550            },
551            IntuneEvent {
552                id: 1,
553                event_type: IntuneEventType::Win32App,
554                name: "NoTime".to_string(),
555                guid: None,
556                status: IntuneStatus::Pending,
557                start_time: None,
558                end_time: None,
559                duration_secs: None,
560                error_code: None,
561                detail: "no time".to_string(),
562                source_file: "b.log".to_string(),
563                line_number: 1,
564                start_time_epoch: None,
565                end_time_epoch: None,
566                script_body: None,
567                parent_app_guid: None,
568            },
569        ]);
570
571        let timed = &timeline[1]; // sorted after NoTime (which has None timestamp)
572        assert!(
573            timed.start_time_epoch.is_some(),
574            "start_time_epoch should be populated"
575        );
576        assert!(
577            timed.end_time_epoch.is_some(),
578            "end_time_epoch should be populated"
579        );
580
581        let untimed = &timeline[0]; // None timestamps sort first
582        assert!(untimed.start_time_epoch.is_none());
583        assert!(untimed.end_time_epoch.is_none());
584    }
585
586    #[test]
587    fn parse_timestamp_handles_rfc3339() {
588        let result = parse_timestamp("2024-01-15T10:30:00.000Z");
589        assert!(result.is_some());
590        let dt = result.unwrap();
591        assert_eq!(dt.hour(), 10);
592        assert_eq!(dt.minute(), 30);
593    }
594
595    #[test]
596    fn parse_timestamp_handles_rfc3339_with_offset() {
597        let result = parse_timestamp("2024-01-15T10:30:00.000+05:00");
598        assert!(result.is_some());
599        let dt = result.unwrap();
600        // naive_utc converts to UTC: 10:30 +05:00 = 05:30 UTC
601        assert_eq!(dt.hour(), 5);
602        assert_eq!(dt.minute(), 30);
603    }
604
605    #[test]
606    fn build_timeline_sorts_rfc3339_timestamps_across_files() {
607        let timeline = build_timeline(vec![
608            IntuneEvent {
609                id: 0,
610                event_type: IntuneEventType::Win32App,
611                name: "Later".to_string(),
612                guid: None,
613                status: IntuneStatus::InProgress,
614                start_time: Some("2024-12-31T10:00:00.000Z".to_string()),
615                end_time: None,
616                duration_secs: None,
617                error_code: None,
618                detail: "later".to_string(),
619                source_file: "b.log".to_string(),
620                line_number: 2,
621                start_time_epoch: None,
622                end_time_epoch: None,
623                script_body: None,
624                parent_app_guid: None,
625            },
626            IntuneEvent {
627                id: 1,
628                event_type: IntuneEventType::Win32App,
629                name: "Earlier".to_string(),
630                guid: None,
631                status: IntuneStatus::InProgress,
632                start_time: Some("2024-01-01T10:00:00.000Z".to_string()),
633                end_time: None,
634                duration_secs: None,
635                error_code: None,
636                detail: "earlier".to_string(),
637                source_file: "a.log".to_string(),
638                line_number: 1,
639                start_time_epoch: None,
640                end_time_epoch: None,
641                script_body: None,
642                parent_app_guid: None,
643            },
644        ]);
645
646        // Events should be interleaved by time, not grouped by file
647        assert_eq!(timeline[0].name, "Earlier");
648        assert_eq!(timeline[0].source_file, "a.log");
649        assert_eq!(timeline[1].name, "Later");
650        assert_eq!(timeline[1].source_file, "b.log");
651        assert!(timeline[0].start_time_epoch.is_some());
652        assert!(timeline[1].start_time_epoch.is_some());
653    }
654
655    #[test]
656    fn build_timeline_reassigns_ids_after_sorting() {
657        let timeline = build_timeline(vec![
658            IntuneEvent {
659                id: 41,
660                event_type: super::super::models::IntuneEventType::Win32App,
661                name: "Later".to_string(),
662                guid: None,
663                status: IntuneStatus::InProgress,
664                start_time: Some("12-31-2024 10:00:00.000".to_string()),
665                end_time: None,
666                duration_secs: None,
667                error_code: None,
668                detail: "later".to_string(),
669                source_file: "b.log".to_string(),
670                line_number: 2,
671                start_time_epoch: None,
672                end_time_epoch: None,
673                script_body: None,
674                parent_app_guid: None,
675            },
676            IntuneEvent {
677                id: 99,
678                event_type: super::super::models::IntuneEventType::Win32App,
679                name: "Earlier".to_string(),
680                guid: None,
681                status: IntuneStatus::InProgress,
682                start_time: Some("01-01-2024 10:00:00.000".to_string()),
683                end_time: None,
684                duration_secs: None,
685                error_code: None,
686                detail: "earlier".to_string(),
687                source_file: "a.log".to_string(),
688                line_number: 1,
689                start_time_epoch: None,
690                end_time_epoch: None,
691                script_body: None,
692                parent_app_guid: None,
693            },
694        ]);
695
696        assert_eq!(timeline[0].id, 0);
697        assert_eq!(timeline[1].id, 1);
698        assert_eq!(timeline[0].name, "Earlier");
699        assert_eq!(timeline[1].name, "Later");
700    }
701}