use smix_runner_client::RecordedEvent;
use crate::issued_ledger::IssuedAction;
pub const FOCUS_CHANGE_RAW_CODE: i32 = 1018;
pub const DEFAULT_RECONCILE_WINDOW_MS: u64 = 3000;
#[derive(Clone, Debug, PartialEq)]
pub struct CapsuleReconciliation {
pub issued_count: usize,
pub focus_change_count: usize,
pub attributed_count: usize,
pub unattributed_count: usize,
pub unattributed_events: Vec<RecordedEvent>,
}
pub fn reconcile(
issued: &[IssuedAction],
events: &[RecordedEvent],
window_ms: u64,
) -> CapsuleReconciliation {
let window = window_ms as f64;
let focus_changes: Vec<&RecordedEvent> = events
.iter()
.filter(|e| e.raw_code == FOCUS_CHANGE_RAW_CODE)
.collect();
let mut attributed_count = 0usize;
let mut unattributed_events: Vec<RecordedEvent> = Vec::new();
for event in &focus_changes {
let hit = issued
.iter()
.any(|act| (event.timestamp_ms - act.timestamp_ms).abs() <= window);
if hit {
attributed_count += 1;
} else {
unattributed_events.push((*event).clone());
}
}
CapsuleReconciliation {
issued_count: issued.len(),
focus_change_count: focus_changes.len(),
attributed_count,
unattributed_count: unattributed_events.len(),
unattributed_events,
}
}