Skip to main content

aios_collector/
collection_stats.rs

1//! 采集观测统计
2//!
3//! 只记录原始事件类型计数, 不读取或保留事件内容。
4
5use aios_spec::RawEvent;
6
7/// 原始事件类型。
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum RawEventKind {
10    /// UsageStatsManager 应用前后台切换。
11    AppTransition,
12    /// Binder 事务。
13    BinderTransaction,
14    /// /proc 进程状态变化。
15    ProcStateChange,
16    /// 文件系统访问。
17    FileSystemAccess,
18    /// 通知到达。
19    NotificationPosted,
20    /// 通知交互。
21    NotificationInteraction,
22    /// 屏幕状态。
23    ScreenState,
24    /// 系统状态。
25    SystemState,
26}
27
28/// 窗口内的原始采集统计。
29#[derive(Debug, Clone, Default)]
30pub struct RawEventStats {
31    app_transition: u64,
32    binder_transaction: u64,
33    proc_state_change: u64,
34    file_system_access: u64,
35    notification_posted: u64,
36    notification_interaction: u64,
37    screen_state: u64,
38    system_state: u64,
39}
40
41impl RawEventStats {
42    /// 记录一个原始事件。
43    pub fn record(&mut self, event: &RawEvent) {
44        match event {
45            RawEvent::AppTransition(_) => self.app_transition += 1,
46            RawEvent::BinderTransaction(_) => self.binder_transaction += 1,
47            RawEvent::ProcStateChange(_) => self.proc_state_change += 1,
48            RawEvent::FileSystemAccess(_) => self.file_system_access += 1,
49            RawEvent::NotificationPosted(_) => self.notification_posted += 1,
50            RawEvent::NotificationInteraction(_) => self.notification_interaction += 1,
51            RawEvent::ScreenState(_) => self.screen_state += 1,
52            RawEvent::SystemState(_) => self.system_state += 1,
53        }
54    }
55
56    /// 返回指定事件类型的计数。
57    pub fn count(&self, kind: RawEventKind) -> u64 {
58        match kind {
59            RawEventKind::AppTransition => self.app_transition,
60            RawEventKind::BinderTransaction => self.binder_transaction,
61            RawEventKind::ProcStateChange => self.proc_state_change,
62            RawEventKind::FileSystemAccess => self.file_system_access,
63            RawEventKind::NotificationPosted => self.notification_posted,
64            RawEventKind::NotificationInteraction => self.notification_interaction,
65            RawEventKind::ScreenState => self.screen_state,
66            RawEventKind::SystemState => self.system_state,
67        }
68    }
69
70    /// 返回窗口内原始事件总数。
71    pub fn total(&self) -> u64 {
72        self.summary_fields()
73            .into_iter()
74            .map(|(_, count)| count)
75            .sum()
76    }
77
78    /// 返回适合日志输出的稳定字段列表。
79    pub fn summary_fields(&self) -> Vec<(&'static str, u64)> {
80        vec![
81            ("app_transition", self.app_transition),
82            ("binder_transaction", self.binder_transaction),
83            ("proc_state_change", self.proc_state_change),
84            ("file_system_access", self.file_system_access),
85            ("notification_posted", self.notification_posted),
86            ("notification_interaction", self.notification_interaction),
87            ("screen_state", self.screen_state),
88            ("system_state", self.system_state),
89        ]
90    }
91
92    /// 返回适合单行日志输出的非零统计。
93    pub fn summary_line(&self) -> String {
94        let parts: Vec<String> = self
95            .summary_fields()
96            .into_iter()
97            .filter(|(_, count)| *count > 0)
98            .map(|(kind, count)| format!("{kind}={count}"))
99            .collect();
100
101        if parts.is_empty() {
102            "none".to_string()
103        } else {
104            parts.join(" ")
105        }
106    }
107}