|
| 1 | +use super::sort::sort_by_timestamp; |
| 2 | +use aw_models::Event; |
| 3 | +use chrono::{DateTime, Utc}; |
| 4 | +use std::collections::VecDeque; |
| 5 | + |
| 6 | +/// Takes a list of two events and returns a new list of events covering the union |
| 7 | +/// of the timeperiods contained in the eventlists with no overlapping events. |
| 8 | +/// |
| 9 | +/// aw-core implementation: https://github.com/ActivityWatch/aw-core/blob/b11fbe08a0405dec01380493f7b3261163cc6878/aw_transform/filter_period_intersect.py#L92 |
| 10 | +/// |
| 11 | +/// WARNING: This function strips all data from events as it cannot keep it consistent. |
| 12 | +/// |
| 13 | +/// |
| 14 | +/// # Example |
| 15 | +/// ```ignore |
| 16 | +/// events1 | ------- --------- | |
| 17 | +/// events2 | ------ --- -- ---- | |
| 18 | +/// result | ----------- -- --------- | |
| 19 | +/// ``` |
| 20 | +pub fn period_union(events1: &[Event], events2: &[Event]) -> Vec<Event> { |
| 21 | + let mut sorted_events: VecDeque<Event> = VecDeque::new(); |
| 22 | + sorted_events.extend(sort_by_timestamp([events1, events2].concat())); |
| 23 | + |
| 24 | + let mut events_union = Vec::new(); |
| 25 | + |
| 26 | + if !events1.is_empty() { |
| 27 | + events_union.push(sorted_events.pop_front().unwrap()) |
| 28 | + } |
| 29 | + |
| 30 | + for e in sorted_events { |
| 31 | + let last_event = events_union.last().unwrap(); |
| 32 | + |
| 33 | + let e_p = e.interval(); |
| 34 | + let le_p = last_event.interval(); |
| 35 | + |
| 36 | + match e_p.union(&le_p) { |
| 37 | + Some(new_period) => { |
| 38 | + // If no gap and could be unioned, modify last event |
| 39 | + let mut e_mod = events_union.pop().unwrap(); |
| 40 | + e_mod.duration = new_period.duration(); |
| 41 | + events_union.push(e_mod); |
| 42 | + } |
| 43 | + None => { |
| 44 | + // If gap and could not be unioned, push event |
| 45 | + events_union.push(e); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // for event in merged_events: |
| 51 | + // # Clear data |
| 52 | + // event.data = {} |
| 53 | + |
| 54 | + events_union |
| 55 | +} |
| 56 | + |
| 57 | +#[cfg(test)] |
| 58 | +mod tests { |
| 59 | + use std::str::FromStr; |
| 60 | + |
| 61 | + use chrono::DateTime; |
| 62 | + use chrono::Duration; |
| 63 | + use chrono::Utc; |
| 64 | + use serde_json::json; |
| 65 | + |
| 66 | + use aw_models::Event; |
| 67 | + |
| 68 | + use super::period_union; |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn test_period_union_empty() { |
| 72 | + let e_result = period_union(&[], &[]); |
| 73 | + assert_eq!(e_result.len(), 0); |
| 74 | + } |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn test_period_union() { |
| 78 | + let e1 = Event { |
| 79 | + id: None, |
| 80 | + timestamp: DateTime::from_str("2000-01-01T00:00:01Z").unwrap(), |
| 81 | + duration: Duration::seconds(1), |
| 82 | + data: json_map! {"test": json!(1)}, |
| 83 | + }; |
| 84 | + |
| 85 | + let mut e2 = e1.clone(); |
| 86 | + e2.timestamp = DateTime::from_str("2000-01-01T00:00:02Z").unwrap(); |
| 87 | + |
| 88 | + let e_result = period_union(&[e1], &[e2]); |
| 89 | + assert_eq!(e_result.len(), 1); |
| 90 | + |
| 91 | + let dt: DateTime<Utc> = DateTime::from_str("2000-01-01T00:00:01.000Z").unwrap(); |
| 92 | + assert_eq!(e_result[0].timestamp, dt); |
| 93 | + assert_eq!(e_result[0].duration, Duration::milliseconds(2000)); |
| 94 | + } |
| 95 | +} |
0 commit comments