1use crate::network::types::{ApplicationProtocol, Connection, ConnectionKey};
10use std::collections::{BTreeSet, HashMap, VecDeque};
11use std::net::SocketAddr;
12use std::time::{Duration, SystemTime};
13
14const UNKNOWN_NAME: &str = "Unknown";
15const OTHER_NAME: &str = "Other";
16
17#[derive(Debug, Clone)]
19pub struct ProcessActivityConfig {
20 pub max_completed_processes: usize,
24 pub max_destinations_per_process: usize,
28 pub window: Duration,
30}
31
32impl Default for ProcessActivityConfig {
33 fn default() -> Self {
34 Self {
35 max_completed_processes: 4096,
36 max_destinations_per_process: 256,
37 window: Duration::from_secs(60),
38 }
39 }
40}
41
42#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
44pub struct ProcessIdentity {
45 pub pid: Option<u32>,
46 pub name: String,
47 pub attributed: bool,
48}
49
50impl ProcessIdentity {
51 fn from_connection(conn: &Connection) -> Self {
52 let attributed = conn.pid.is_some() || conn.process_name.is_some();
53 Self {
54 pid: conn.pid,
55 name: conn
56 .process_name
57 .clone()
58 .unwrap_or_else(|| UNKNOWN_NAME.to_string()),
59 attributed,
60 }
61 }
62
63 fn overflow(attributed: bool) -> Self {
64 Self {
65 pid: None,
66 name: if attributed {
67 OTHER_NAME.to_string()
68 } else {
69 UNKNOWN_NAME.to_string()
70 },
71 attributed,
72 }
73 }
74
75 pub fn display_name(&self) -> String {
76 match self.pid {
77 Some(pid) => format!("{} ({pid})", self.name),
78 None => self.name.clone(),
79 }
80 }
81}
82
83#[derive(Debug, Clone)]
85pub struct DestinationActivity {
86 pub remote_addr: SocketAddr,
87 pub label: Option<String>,
88 pub tx_bytes: u64,
89 pub rx_bytes: u64,
90 pub connections: u64,
91}
92
93impl DestinationActivity {
94 pub fn display_name(&self) -> String {
95 self.label
96 .as_ref()
97 .map(|label| format!("{label}:{}", self.remote_addr.port()))
98 .unwrap_or_else(|| self.remote_addr.to_string())
99 }
100}
101
102#[derive(Debug, Clone)]
104pub struct ProcessActivity {
105 pub identity: ProcessIdentity,
106 pub current_tx_bps: f64,
107 pub current_rx_bps: f64,
108 pub window_tx_bytes: u64,
109 pub window_rx_bytes: u64,
110 pub peak_tx_bps: f64,
111 pub peak_rx_bps: f64,
112 pub retained_tx_bytes: u64,
113 pub retained_rx_bytes: u64,
114 pub active_connections: usize,
115 pub total_connections: u64,
116 pub unique_destinations: usize,
117 pub destinations_truncated: bool,
118 pub top_tx_destination: Option<DestinationActivity>,
119 pub top_rx_destination: Option<DestinationActivity>,
120 pub current_tx_share: f64,
121 pub current_rx_share: f64,
122 pub window_tx_share: f64,
123 pub window_rx_share: f64,
124 pub retained_tx_share: f64,
125 pub retained_rx_share: f64,
126 pub first_seen: SystemTime,
127 pub last_seen: SystemTime,
128}
129
130#[derive(Debug, Clone)]
132pub struct ProcessActivitySnapshot {
133 pub generated_at: SystemTime,
134 pub processes: Vec<ProcessActivity>,
135 pub current_tx_bps: f64,
136 pub current_rx_bps: f64,
137 pub window_tx_bytes: u64,
138 pub window_rx_bytes: u64,
139 pub retained_tx_bytes: u64,
140 pub retained_rx_bytes: u64,
141 pub attributed_tx_bytes: u64,
142 pub attributed_rx_bytes: u64,
143}
144
145impl Default for ProcessActivitySnapshot {
146 fn default() -> Self {
147 Self {
148 generated_at: SystemTime::UNIX_EPOCH,
149 processes: Vec::new(),
150 current_tx_bps: 0.0,
151 current_rx_bps: 0.0,
152 window_tx_bytes: 0,
153 window_rx_bytes: 0,
154 retained_tx_bytes: 0,
155 retained_rx_bytes: 0,
156 attributed_tx_bytes: 0,
157 attributed_rx_bytes: 0,
158 }
159 }
160}
161
162impl ProcessActivitySnapshot {
163 pub fn tx_attribution_pct(&self) -> f64 {
164 percentage(
165 self.attributed_tx_bytes as f64,
166 self.retained_tx_bytes as f64,
167 )
168 }
169
170 pub fn rx_attribution_pct(&self) -> f64 {
171 percentage(
172 self.attributed_rx_bytes as f64,
173 self.retained_rx_bytes as f64,
174 )
175 }
176}
177
178#[derive(Debug, Clone)]
179struct FlowActivity {
180 identity: ProcessIdentity,
181 bytes_sent: u64,
182 bytes_received: u64,
183 remote_addr: SocketAddr,
184 remote_label: Option<String>,
185 created_at: SystemTime,
186 last_activity: SystemTime,
187}
188
189impl FlowActivity {
190 fn from_connection(conn: &Connection) -> Self {
191 Self {
192 identity: ProcessIdentity::from_connection(conn),
193 bytes_sent: conn.bytes_sent,
194 bytes_received: conn.bytes_received,
195 remote_addr: conn.remote_addr,
196 remote_label: destination_label(conn),
197 created_at: conn.created_at,
198 last_activity: conn.last_activity,
199 }
200 }
201}
202
203#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
207struct FlowIdentity {
208 key: ConnectionKey,
209 created_nanos: u128,
210}
211
212impl FlowIdentity {
213 fn from_connection(conn: &Connection) -> Self {
214 Self {
215 key: ConnectionKey::new(conn.protocol, conn.local_addr, conn.remote_addr),
216 created_nanos: conn
217 .created_at
218 .duration_since(SystemTime::UNIX_EPOCH)
219 .unwrap_or_default()
220 .as_nanos(),
221 }
222 }
223}
224
225#[derive(Debug, Clone, Copy)]
226struct FlowCounters {
227 tx_bytes: u64,
228 rx_bytes: u64,
229 observed_generation: u64,
230}
231
232#[derive(Debug, Clone, Copy, Default)]
233struct TrafficDelta {
234 tx_bytes: u64,
235 rx_bytes: u64,
236}
237
238impl TrafficDelta {
239 fn add(&mut self, other: Self) {
240 self.tx_bytes = self.tx_bytes.saturating_add(other.tx_bytes);
241 self.rx_bytes = self.rx_bytes.saturating_add(other.rx_bytes);
242 }
243}
244
245#[derive(Debug, Clone)]
246struct ProcessAccumulator {
247 tx_bytes: u64,
248 rx_bytes: u64,
249 active_connections: usize,
250 completed_connections: u64,
251 destinations: HashMap<SocketAddr, DestinationActivity>,
252 first_seen: SystemTime,
253 last_seen: SystemTime,
254}
255
256impl ProcessAccumulator {
257 fn new(first_seen: SystemTime) -> Self {
258 Self {
259 tx_bytes: 0,
260 rx_bytes: 0,
261 active_connections: 0,
262 completed_connections: 0,
263 destinations: HashMap::new(),
264 first_seen,
265 last_seen: first_seen,
266 }
267 }
268
269 fn add_flow(&mut self, flow: &FlowActivity, completed: bool) {
270 self.tx_bytes = self.tx_bytes.saturating_add(flow.bytes_sent);
271 self.rx_bytes = self.rx_bytes.saturating_add(flow.bytes_received);
272 if completed {
273 self.completed_connections = self.completed_connections.saturating_add(1);
274 } else {
275 self.active_connections = self.active_connections.saturating_add(1);
276 }
277 self.first_seen = self.first_seen.min(flow.created_at);
278 self.last_seen = self.last_seen.max(flow.last_activity);
279 self.add_destination(flow);
280 }
281
282 fn add_destination(&mut self, flow: &FlowActivity) {
283 if let Some(destination) = self.destinations.get_mut(&flow.remote_addr) {
284 destination.tx_bytes = destination.tx_bytes.saturating_add(flow.bytes_sent);
285 destination.rx_bytes = destination.rx_bytes.saturating_add(flow.bytes_received);
286 destination.connections = destination.connections.saturating_add(1);
287 if destination.label.is_none() {
288 destination.label.clone_from(&flow.remote_label);
289 }
290 return;
291 }
292
293 self.destinations.insert(
294 flow.remote_addr,
295 DestinationActivity {
296 remote_addr: flow.remote_addr,
297 label: flow.remote_label.clone(),
298 tx_bytes: flow.bytes_sent,
299 rx_bytes: flow.bytes_received,
300 connections: 1,
301 },
302 );
303 }
304}
305
306#[derive(Debug, Clone, Copy)]
307struct ProcessSample {
308 timestamp: SystemTime,
309 tx_bytes: u64,
310 rx_bytes: u64,
311}
312
313#[derive(Debug, Default)]
314struct ProcessHistory {
315 samples: VecDeque<ProcessSample>,
316 cumulative_tx_bytes: u64,
317 cumulative_rx_bytes: u64,
318 current_tx_bps: f64,
319 current_rx_bps: f64,
320 peak_tx_bps: f64,
321 peak_rx_bps: f64,
322}
323
324impl ProcessHistory {
325 fn sample(
326 &mut self,
327 now: SystemTime,
328 tx_delta: u64,
329 rx_delta: u64,
330 window: Duration,
331 active: bool,
332 ) {
333 if self.samples.is_empty() {
334 self.samples.push_back(ProcessSample {
338 timestamp: now,
339 tx_bytes: self.cumulative_tx_bytes,
340 rx_bytes: self.cumulative_rx_bytes,
341 });
342 }
343
344 self.cumulative_tx_bytes = self.cumulative_tx_bytes.saturating_add(tx_delta);
345 self.cumulative_rx_bytes = self.cumulative_rx_bytes.saturating_add(rx_delta);
346 let tx_bytes = self.cumulative_tx_bytes;
347 let rx_bytes = self.cumulative_rx_bytes;
348
349 let unchanged = self
350 .samples
351 .back()
352 .is_some_and(|sample| sample.tx_bytes == tx_bytes && sample.rx_bytes == rx_bytes);
353 if !active && unchanged {
354 self.current_tx_bps = 0.0;
355 self.current_rx_bps = 0.0;
356 self.prune(now, window, false);
357 return;
358 }
359
360 if let Some(previous) = self.samples.back().copied() {
361 let elapsed = now
362 .duration_since(previous.timestamp)
363 .unwrap_or_default()
364 .as_secs_f64();
365 if elapsed > 0.0 {
366 self.current_tx_bps = tx_bytes.saturating_sub(previous.tx_bytes) as f64 / elapsed;
367 self.current_rx_bps = rx_bytes.saturating_sub(previous.rx_bytes) as f64 / elapsed;
368 self.peak_tx_bps = self.peak_tx_bps.max(self.current_tx_bps);
369 self.peak_rx_bps = self.peak_rx_bps.max(self.current_rx_bps);
370 }
371 }
372
373 self.samples.push_back(ProcessSample {
374 timestamp: now,
375 tx_bytes,
376 rx_bytes,
377 });
378
379 self.prune(now, window, active);
380 }
381
382 fn prune(&mut self, now: SystemTime, window: Duration, active: bool) {
383 while self.samples.len() > 2
384 && self.samples.get(1).is_some_and(|sample| {
385 now.duration_since(sample.timestamp).unwrap_or_default() >= window
386 })
387 {
388 self.samples.pop_front();
389 }
390
391 if !active
392 && self.samples.back().is_some_and(|sample| {
393 now.duration_since(sample.timestamp).unwrap_or_default() >= window
394 })
395 && let Some(last) = self.samples.back().copied()
396 {
397 self.samples.clear();
398 self.samples.push_back(last);
399 }
400 }
401
402 fn window_bytes(&self, now: SystemTime, window: Duration) -> (u64, u64) {
403 if self.samples.back().is_some_and(|sample| {
404 now.duration_since(sample.timestamp).unwrap_or_default() >= window
405 }) {
406 return (0, 0);
407 }
408 match (self.samples.front(), self.samples.back()) {
409 (Some(first), Some(last)) => (
410 last.tx_bytes.saturating_sub(first.tx_bytes),
411 last.rx_bytes.saturating_sub(first.rx_bytes),
412 ),
413 _ => (0, 0),
414 }
415 }
416}
417
418fn observe_flow_delta(
419 flow_counters: &mut HashMap<FlowIdentity, FlowCounters>,
420 conn: &Connection,
421 generation: u64,
422) -> TrafficDelta {
423 let identity = FlowIdentity::from_connection(conn);
424 match flow_counters.entry(identity) {
425 std::collections::hash_map::Entry::Occupied(mut entry) => {
426 let counters = entry.get_mut();
427 let delta = TrafficDelta {
428 tx_bytes: conn.bytes_sent.saturating_sub(counters.tx_bytes),
429 rx_bytes: conn.bytes_received.saturating_sub(counters.rx_bytes),
430 };
431 counters.tx_bytes = conn.bytes_sent;
432 counters.rx_bytes = conn.bytes_received;
433 counters.observed_generation = generation;
434 delta
435 }
436 std::collections::hash_map::Entry::Vacant(entry) => {
437 entry.insert(FlowCounters {
438 tx_bytes: conn.bytes_sent,
439 rx_bytes: conn.bytes_received,
440 observed_generation: generation,
441 });
442 TrafficDelta {
443 tx_bytes: conn.bytes_sent,
444 rx_bytes: conn.bytes_received,
445 }
446 }
447 }
448}
449
450pub struct ProcessActivityTracker {
452 config: ProcessActivityConfig,
453 sample: HashMap<ProcessIdentity, ProcessAccumulator>,
454 flow_counters: HashMap<FlowIdentity, FlowCounters>,
455 sample_generation: u64,
456 histories: HashMap<ProcessIdentity, ProcessHistory>,
457 snapshot: ProcessActivitySnapshot,
458}
459
460impl ProcessActivityTracker {
461 pub fn new() -> Self {
462 Self::with_config(ProcessActivityConfig::default())
463 }
464
465 pub fn with_config(config: ProcessActivityConfig) -> Self {
466 Self {
467 config,
468 sample: HashMap::new(),
469 flow_counters: HashMap::new(),
470 sample_generation: 0,
471 histories: HashMap::new(),
472 snapshot: ProcessActivitySnapshot::default(),
473 }
474 }
475
476 pub fn observe_connections(&mut self, connections: &[Connection], now: SystemTime) {
480 self.observe_sources(
481 now,
482 |observe| {
483 for connection in connections.iter().filter(|conn| !conn.is_historic) {
484 observe(connection);
485 }
486 },
487 |observe| {
488 for connection in connections.iter().filter(|conn| conn.is_historic) {
489 observe(connection);
490 }
491 },
492 );
493 }
494
495 pub fn observe_sources<A, H>(
503 &mut self,
504 now: SystemTime,
505 mut feed_active: A,
506 mut feed_historic: H,
507 ) where
508 A: FnMut(&mut dyn FnMut(&Connection)),
509 H: FnMut(&mut dyn FnMut(&Connection)),
510 {
511 self.sample.clear();
512 self.sample_generation = self.sample_generation.wrapping_add(1);
513 let generation = self.sample_generation;
514 let max_processes = self.config.max_completed_processes;
515 let mut traffic_deltas: HashMap<ProcessIdentity, TrafficDelta> = HashMap::new();
516
517 {
518 let sample = &mut self.sample;
519 let flow_counters = &mut self.flow_counters;
520 let mut observe = |conn: &Connection| {
521 let flow = FlowActivity::from_connection(conn);
522 let identity = flow.identity.clone();
523 sample
524 .entry(identity.clone())
525 .or_insert_with(|| ProcessAccumulator::new(flow.created_at))
526 .add_flow(&flow, false);
527 traffic_deltas
528 .entry(identity)
529 .or_default()
530 .add(observe_flow_delta(flow_counters, conn, generation));
531 };
532 feed_active(&mut observe);
533 }
534
535 let mut completed_identities = BTreeSet::new();
536 {
537 let active = &self.sample;
538 let mut select = |conn: &Connection| {
539 let identity = ProcessIdentity::from_connection(conn);
540 if active.contains_key(&identity) {
541 return;
542 }
543 completed_identities.insert(identity);
544 if completed_identities.len() > max_processes {
545 completed_identities.pop_last();
546 }
547 };
548 feed_historic(&mut select);
549 }
550
551 {
552 let sample = &mut self.sample;
553 let flow_counters = &mut self.flow_counters;
554 let mut observe = |conn: &Connection| {
555 let flow = FlowActivity::from_connection(conn);
556 let identity = if sample.contains_key(&flow.identity)
557 || completed_identities.contains(&flow.identity)
558 {
559 flow.identity.clone()
560 } else {
561 ProcessIdentity::overflow(flow.identity.attributed)
562 };
563 sample
564 .entry(identity.clone())
565 .or_insert_with(|| ProcessAccumulator::new(flow.created_at))
566 .add_flow(&flow, true);
567 traffic_deltas
568 .entry(identity)
569 .or_default()
570 .add(observe_flow_delta(flow_counters, conn, generation));
571 };
572 feed_historic(&mut observe);
573 }
574
575 self.flow_counters
576 .retain(|_, counters| counters.observed_generation == generation);
577 self.rebuild_snapshot(now, &traffic_deltas);
578 self.sample.clear();
579 }
580
581 pub fn snapshot(&self) -> ProcessActivitySnapshot {
582 self.snapshot.clone()
583 }
584
585 pub fn clear(&mut self) {
586 self.sample.clear();
587 self.flow_counters.clear();
588 self.sample_generation = 0;
589 self.histories.clear();
590 self.snapshot = ProcessActivitySnapshot::default();
591 }
592
593 fn rebuild_snapshot(
594 &mut self,
595 now: SystemTime,
596 traffic_deltas: &HashMap<ProcessIdentity, TrafficDelta>,
597 ) {
598 self.histories
599 .retain(|identity, _| self.sample.contains_key(identity));
600
601 for (identity, aggregate) in &self.sample {
602 let is_new = !self.histories.contains_key(identity);
603 let delta = traffic_deltas.get(identity).copied().unwrap_or_default();
604 let (tx_delta, rx_delta) = if is_new {
605 (aggregate.tx_bytes, aggregate.rx_bytes)
606 } else {
607 (delta.tx_bytes, delta.rx_bytes)
608 };
609 self.histories.entry(identity.clone()).or_default().sample(
610 now,
611 tx_delta,
612 rx_delta,
613 self.config.window,
614 aggregate.active_connections > 0,
615 );
616 }
617
618 let mut processes = Vec::with_capacity(self.sample.len());
619 for (identity, aggregate) in &self.sample {
620 let history = self.histories.entry(identity.clone()).or_default();
621 let (window_tx_bytes, window_rx_bytes) = history.window_bytes(now, self.config.window);
622 let destination_count = aggregate.destinations.len();
623 let top_tx_destination = aggregate
624 .destinations
625 .values()
626 .max_by(|a, b| {
627 a.tx_bytes
628 .cmp(&b.tx_bytes)
629 .then_with(|| a.remote_addr.cmp(&b.remote_addr))
630 })
631 .cloned();
632 let top_rx_destination = aggregate
633 .destinations
634 .values()
635 .max_by(|a, b| {
636 a.rx_bytes
637 .cmp(&b.rx_bytes)
638 .then_with(|| a.remote_addr.cmp(&b.remote_addr))
639 })
640 .cloned();
641 processes.push(ProcessActivity {
642 identity: identity.clone(),
643 current_tx_bps: history.current_tx_bps,
644 current_rx_bps: history.current_rx_bps,
645 window_tx_bytes,
646 window_rx_bytes,
647 peak_tx_bps: history.peak_tx_bps,
648 peak_rx_bps: history.peak_rx_bps,
649 retained_tx_bytes: aggregate.tx_bytes,
650 retained_rx_bytes: aggregate.rx_bytes,
651 active_connections: aggregate.active_connections,
652 total_connections: aggregate
653 .completed_connections
654 .saturating_add(aggregate.active_connections as u64),
655 unique_destinations: destination_count
656 .min(self.config.max_destinations_per_process),
657 destinations_truncated: destination_count
658 > self.config.max_destinations_per_process,
659 top_tx_destination,
660 top_rx_destination,
661 current_tx_share: 0.0,
662 current_rx_share: 0.0,
663 window_tx_share: 0.0,
664 window_rx_share: 0.0,
665 retained_tx_share: 0.0,
666 retained_rx_share: 0.0,
667 first_seen: aggregate.first_seen,
668 last_seen: aggregate.last_seen,
669 });
670 }
671
672 let current_tx_bps = processes.iter().map(|p| p.current_tx_bps).sum();
673 let current_rx_bps = processes.iter().map(|p| p.current_rx_bps).sum();
674 let window_tx_bytes = processes.iter().map(|p| p.window_tx_bytes).sum();
675 let window_rx_bytes = processes.iter().map(|p| p.window_rx_bytes).sum();
676 let retained_tx_bytes = processes.iter().map(|p| p.retained_tx_bytes).sum();
677 let retained_rx_bytes = processes.iter().map(|p| p.retained_rx_bytes).sum();
678 let attributed_tx_bytes = processes
679 .iter()
680 .filter(|p| p.identity.attributed)
681 .map(|p| p.retained_tx_bytes)
682 .sum();
683 let attributed_rx_bytes = processes
684 .iter()
685 .filter(|p| p.identity.attributed)
686 .map(|p| p.retained_rx_bytes)
687 .sum();
688
689 for process in &mut processes {
690 process.current_tx_share = percentage(process.current_tx_bps, current_tx_bps);
691 process.current_rx_share = percentage(process.current_rx_bps, current_rx_bps);
692 process.window_tx_share =
693 percentage(process.window_tx_bytes as f64, window_tx_bytes as f64);
694 process.window_rx_share =
695 percentage(process.window_rx_bytes as f64, window_rx_bytes as f64);
696 process.retained_tx_share =
697 percentage(process.retained_tx_bytes as f64, retained_tx_bytes as f64);
698 process.retained_rx_share =
699 percentage(process.retained_rx_bytes as f64, retained_rx_bytes as f64);
700 }
701 processes.sort_by(|a, b| {
702 b.retained_tx_bytes
703 .cmp(&a.retained_tx_bytes)
704 .then_with(|| a.identity.name.cmp(&b.identity.name))
705 .then_with(|| a.identity.pid.cmp(&b.identity.pid))
706 });
707
708 self.snapshot = ProcessActivitySnapshot {
709 generated_at: now,
710 processes,
711 current_tx_bps,
712 current_rx_bps,
713 window_tx_bytes,
714 window_rx_bytes,
715 retained_tx_bytes,
716 retained_rx_bytes,
717 attributed_tx_bytes,
718 attributed_rx_bytes,
719 };
720 }
721}
722
723impl Default for ProcessActivityTracker {
724 fn default() -> Self {
725 Self::new()
726 }
727}
728
729fn percentage(part: f64, total: f64) -> f64 {
730 if total > 0.0 {
731 (part / total * 100.0).clamp(0.0, 100.0)
732 } else {
733 0.0
734 }
735}
736
737fn destination_label(conn: &Connection) -> Option<String> {
738 let dpi = conn.dpi_info.as_ref()?;
739 match &dpi.application {
740 ApplicationProtocol::Https(info) => info
741 .tls_info
742 .as_ref()
743 .and_then(|tls| tls.sni.as_ref())
744 .cloned(),
745 ApplicationProtocol::Quic(info) => info
746 .tls_info
747 .as_ref()
748 .and_then(|tls| tls.sni.as_ref())
749 .cloned(),
750 ApplicationProtocol::Http(info) => info.host.clone(),
751 _ => None,
752 }
753}
754
755#[cfg(test)]
756mod tests {
757 use super::*;
758 use crate::network::types::{Protocol, ProtocolState, TcpState};
759 use std::net::{IpAddr, Ipv4Addr};
760
761 fn connection(pid: Option<u32>, name: Option<&str>, remote_port: u16) -> Connection {
762 let mut conn = Connection::new(
763 Protocol::Tcp,
764 SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 40_000 + remote_port),
765 SocketAddr::new(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 10)), remote_port),
766 ProtocolState::Tcp(TcpState::Established),
767 );
768 conn.pid = pid;
769 conn.process_name = name.map(str::to_string);
770 conn
771 }
772
773 #[test]
774 fn aggregates_active_and_retained_historic_flows() {
775 let start = SystemTime::UNIX_EPOCH + Duration::from_secs(100);
776 let mut a = connection(Some(7), Some("uploader"), 443);
777 let mut b = connection(Some(7), Some("uploader"), 8443);
778 a.created_at = start;
779 b.created_at = start + Duration::from_millis(1);
780 a.bytes_sent = 800;
781 b.bytes_sent = 200;
782
783 let mut tracker = ProcessActivityTracker::new();
784 tracker.observe_connections(&[a.clone(), b.clone()], start + Duration::from_secs(1));
785 a.is_historic = true;
786 tracker.observe_connections(&[a, b], start + Duration::from_secs(2));
787
788 let snapshot = tracker.snapshot();
789 let process = &snapshot.processes[0];
790 assert_eq!(process.retained_tx_bytes, 1000);
791 assert_eq!(process.active_connections, 1);
792 assert_eq!(process.total_connections, 2);
793 assert_eq!(process.unique_destinations, 2);
794 }
795
796 #[test]
797 fn late_attribution_moves_active_bytes_out_of_unknown() {
798 let now = SystemTime::UNIX_EPOCH + Duration::from_secs(200);
799 let mut conn = connection(None, None, 443);
800 conn.created_at = now;
801 conn.bytes_sent = 4096;
802
803 let mut tracker = ProcessActivityTracker::new();
804 tracker.observe_connections(&[conn.clone()], now + Duration::from_secs(1));
805 assert!(!tracker.snapshot().processes[0].identity.attributed);
806
807 conn.pid = Some(42);
808 conn.process_name = Some("agent-helper".to_string());
809 tracker.observe_connections(&[conn], now + Duration::from_secs(2));
810 let snapshot = tracker.snapshot();
811 assert_eq!(snapshot.processes.len(), 1);
812 assert_eq!(snapshot.processes[0].identity.pid, Some(42));
813 assert_eq!(snapshot.attributed_tx_bytes, 4096);
814 }
815
816 #[test]
817 fn repeated_historic_observations_are_idempotent() {
818 let now = SystemTime::UNIX_EPOCH + Duration::from_secs(300);
819 let mut conn = connection(Some(9), Some("once"), 443);
820 conn.created_at = now;
821 conn.bytes_sent = 1234;
822 conn.is_historic = true;
823
824 let mut tracker = ProcessActivityTracker::new();
825 tracker.observe_connections(&[conn.clone()], now + Duration::from_secs(1));
826 tracker.observe_connections(&[conn], now + Duration::from_secs(2));
827
828 let process = &tracker.snapshot().processes[0];
829 assert_eq!(process.retained_tx_bytes, 1234);
830 assert_eq!(process.active_connections, 0);
831 assert_eq!(process.total_connections, 1);
832 }
833
834 #[test]
835 fn calculates_current_window_and_retained_shares() {
836 let now = SystemTime::UNIX_EPOCH + Duration::from_secs(400);
837 let mut first = connection(Some(1), Some("first"), 443);
838 let mut second = connection(Some(2), Some("second"), 443);
839 first.created_at = now;
840 second.created_at = now + Duration::from_millis(1);
841
842 let mut tracker = ProcessActivityTracker::new();
843 tracker.observe_connections(
844 &[first.clone(), second.clone()],
845 now + Duration::from_secs(1),
846 );
847 first.bytes_sent = 750;
848 second.bytes_sent = 250;
849 first.bytes_received = 250;
850 second.bytes_received = 750;
851 tracker.observe_connections(&[first, second], now + Duration::from_secs(2));
852
853 let snapshot = tracker.snapshot();
854 let first = snapshot
855 .processes
856 .iter()
857 .find(|process| process.identity.name == "first")
858 .unwrap();
859 assert_eq!(snapshot.window_tx_bytes, 1000);
860 assert_eq!(snapshot.window_rx_bytes, 1000);
861 assert_eq!(first.current_tx_bps, 750.0);
862 assert_eq!(first.current_rx_bps, 250.0);
863 assert_eq!(first.peak_rx_bps, 250.0);
864 assert_eq!(first.window_tx_share, 75.0);
865 assert_eq!(first.window_rx_share, 25.0);
866 assert_eq!(first.retained_tx_share, 75.0);
867 assert_eq!(first.retained_rx_share, 25.0);
868 }
869
870 #[test]
871 fn first_observation_is_visible_in_the_window_without_a_fake_rate() {
872 let now = SystemTime::UNIX_EPOCH + Duration::from_secs(500);
873 let mut conn = connection(Some(12), Some("quick-uploader"), 443);
874 conn.created_at = now;
875 conn.bytes_sent = 64 * 1024 * 1024;
876
877 let mut tracker = ProcessActivityTracker::new();
878 tracker.observe_connections(&[conn], now + Duration::from_secs(1));
879
880 let process = &tracker.snapshot().processes[0];
881 assert_eq!(process.window_tx_bytes, 64 * 1024 * 1024);
882 assert_eq!(process.current_tx_bps, 0.0);
883 assert_eq!(process.window_tx_share, 100.0);
884 }
885
886 #[test]
887 fn historic_eviction_preserves_new_traffic_in_both_directions() {
888 let now = SystemTime::UNIX_EPOCH + Duration::from_secs(600);
889 let mut old = connection(Some(21), Some("worker"), 443);
890 let mut live = connection(Some(21), Some("worker"), 8443);
891 old.created_at = now;
892 live.created_at = now + Duration::from_millis(1);
893 old.bytes_sent = 10_000;
894 old.bytes_received = 100;
895 old.is_historic = true;
896
897 let mut tracker = ProcessActivityTracker::new();
898 tracker.observe_connections(&[old, live.clone()], now + Duration::from_secs(1));
899
900 live.bytes_sent = 100;
901 live.bytes_received = 300;
902 tracker.observe_connections(&[live.clone()], now + Duration::from_secs(2));
903 let after_eviction = &tracker.snapshot().processes[0];
904 assert_eq!(after_eviction.retained_tx_bytes, 100);
905 assert_eq!(after_eviction.retained_rx_bytes, 300);
906 assert_eq!(after_eviction.current_tx_bps, 100.0);
907 assert_eq!(after_eviction.current_rx_bps, 300.0);
908 assert_eq!(after_eviction.window_tx_bytes, 10_100);
909 assert_eq!(after_eviction.window_rx_bytes, 400);
910
911 live.bytes_sent = 300;
912 live.bytes_received = 500;
913 tracker.observe_connections(&[live], now + Duration::from_secs(3));
914 let resumed = &tracker.snapshot().processes[0];
915 assert_eq!(resumed.current_tx_bps, 200.0);
916 assert_eq!(resumed.current_rx_bps, 200.0);
917 assert_eq!(resumed.window_tx_bytes, 10_300);
918 assert_eq!(resumed.window_rx_bytes, 600);
919 }
920
921 #[test]
922 fn historic_eviction_does_not_reduce_a_larger_new_delta() {
923 let now = SystemTime::UNIX_EPOCH + Duration::from_secs(650);
924 let mut old = connection(Some(22), Some("worker"), 443);
925 let mut live = connection(Some(22), Some("worker"), 8443);
926 old.created_at = now;
927 live.created_at = now + Duration::from_millis(1);
928 old.bytes_sent = 1_000;
929 old.is_historic = true;
930
931 let mut tracker = ProcessActivityTracker::new();
932 tracker.observe_connections(&[old, live.clone()], now + Duration::from_secs(1));
933
934 live.bytes_sent = 1_500;
935 tracker.observe_connections(&[live], now + Duration::from_secs(2));
936 let process = &tracker.snapshot().processes[0];
937 assert_eq!(process.retained_tx_bytes, 1_500);
938 assert_eq!(process.current_tx_bps, 1_500.0);
939 assert_eq!(process.window_tx_bytes, 2_500);
940 }
941
942 #[test]
943 fn full_connection_aggregation_is_transient() {
944 let now = SystemTime::UNIX_EPOCH + Duration::from_secs(700);
945 let conn = connection(Some(33), Some("short-lived"), 443);
946 let mut tracker = ProcessActivityTracker::new();
947
948 tracker.observe_connections(&[conn], now);
949
950 assert!(tracker.sample.is_empty());
951 assert_eq!(tracker.snapshot().processes.len(), 1);
952 assert_eq!(tracker.histories.len(), 1);
953 }
954
955 #[test]
956 fn active_to_historic_transition_does_not_create_a_rate_spike() {
957 let now = SystemTime::UNIX_EPOCH + Duration::from_secs(800);
958 let mut idle = connection(Some(44), Some("long-session"), 443);
959 let mut live = connection(Some(44), Some("long-session"), 8443);
960 idle.bytes_sent = 500 * 1024 * 1024;
961
962 let mut tracker = ProcessActivityTracker::new();
963 tracker.observe_connections(&[idle.clone(), live.clone()], now);
964
965 live.bytes_sent = 1024;
966 tracker.observe_connections(&[idle.clone(), live.clone()], now + Duration::from_secs(1));
967
968 idle.is_historic = true;
969 live.bytes_sent = 2048;
970 tracker.observe_connections(&[idle, live], now + Duration::from_secs(2));
971
972 let process = &tracker.snapshot().processes[0];
973 assert_eq!(process.current_tx_bps, 1024.0);
974 assert_eq!(process.peak_tx_bps, 1024.0);
975 assert_eq!(process.window_tx_bytes, 500 * 1024 * 1024 + 2048);
976 }
977
978 #[test]
979 fn completed_process_overflow_is_independent_of_source_order() {
980 let now = SystemTime::UNIX_EPOCH + Duration::from_secs(900);
981 let mut connections: Vec<_> = (1..=4)
982 .map(|pid| {
983 let mut conn = connection(Some(pid), Some(&format!("process-{pid}")), 443);
984 conn.bytes_sent = u64::from(pid) * 100;
985 conn.is_historic = true;
986 conn
987 })
988 .collect();
989 let config = ProcessActivityConfig {
990 max_completed_processes: 2,
991 ..ProcessActivityConfig::default()
992 };
993 let mut tracker = ProcessActivityTracker::with_config(config);
994
995 tracker.observe_connections(&connections, now);
996 let first: Vec<_> = tracker
997 .snapshot()
998 .processes
999 .iter()
1000 .map(|process| (process.identity.display_name(), process.retained_tx_bytes))
1001 .collect();
1002
1003 connections.reverse();
1004 tracker.observe_connections(&connections, now + Duration::from_secs(1));
1005 let second: Vec<_> = tracker
1006 .snapshot()
1007 .processes
1008 .iter()
1009 .map(|process| (process.identity.display_name(), process.retained_tx_bytes))
1010 .collect();
1011
1012 assert_eq!(second, first);
1013 assert!(second.contains(&("process-1 (1)".to_string(), 100)));
1014 assert!(second.contains(&("process-2 (2)".to_string(), 200)));
1015 assert!(second.contains(&(OTHER_NAME.to_string(), 700)));
1016 assert!(
1017 tracker
1018 .snapshot()
1019 .processes
1020 .iter()
1021 .all(|process| process.current_tx_bps == 0.0)
1022 );
1023 }
1024
1025 #[test]
1026 fn destination_cap_does_not_hide_the_highest_volume_peer() {
1027 let now = SystemTime::UNIX_EPOCH + Duration::from_secs(1_000);
1028 let mut low = connection(Some(55), Some("scanner"), 80);
1029 let mut medium = connection(Some(55), Some("scanner"), 443);
1030 let mut highest = connection(Some(55), Some("scanner"), 8443);
1031 low.bytes_sent = 10;
1032 medium.bytes_sent = 20;
1033 highest.bytes_sent = 10_000;
1034 low.bytes_received = 30;
1035 medium.bytes_received = 40;
1036 highest.bytes_received = 20_000;
1037
1038 let config = ProcessActivityConfig {
1039 max_destinations_per_process: 2,
1040 ..ProcessActivityConfig::default()
1041 };
1042 let mut tracker = ProcessActivityTracker::with_config(config);
1043 tracker.observe_connections(&[low, medium, highest], now);
1044
1045 let process = &tracker.snapshot().processes[0];
1046 assert_eq!(process.unique_destinations, 2);
1047 assert!(process.destinations_truncated);
1048 assert_eq!(
1049 process
1050 .top_tx_destination
1051 .as_ref()
1052 .map(|destination| destination.remote_addr.port()),
1053 Some(8443)
1054 );
1055 assert_eq!(
1056 process
1057 .top_rx_destination
1058 .as_ref()
1059 .map(|destination| destination.remote_addr.port()),
1060 Some(8443)
1061 );
1062 }
1063}