aios_cli/benchmark_next_app/
context_loader.rs1use std::collections::{BTreeMap, HashSet};
4use std::fs::File;
5use std::io::{BufRead, BufReader};
6use std::path::Path;
7
8use aios_core::collector_ingress::RustCollectorIngress;
9use aios_core::context_builder::WindowAggregator;
10use aios_core::privacy_airgap::DefaultPrivacyAirGap;
11use aios_spec::traits::PrivacySanitizer;
12use aios_spec::{CollectorEnvelope, RawEvent, SanitizedEvent, SourceTier, StructuredContext};
13use anyhow::{Context, Result};
14use serde_json::Value;
15
16use super::types::NextAppLabel;
17
18pub fn load_labels(path: &Path) -> Result<Vec<NextAppLabel>> {
20 let file = File::open(path).with_context(|| format!("opening labels {}", path.display()))?;
21 let reader = BufReader::new(file);
22 let mut labels = Vec::new();
23 for (line_no, line) in reader.lines().enumerate() {
24 let line =
25 line.with_context(|| format!("reading line {} of {}", line_no + 1, path.display()))?;
26 if line.trim().is_empty() {
27 continue;
28 }
29 let label: NextAppLabel = serde_json::from_str(&line)
30 .with_context(|| format!("parsing label line {} of {}", line_no + 1, path.display()))?;
31 labels.push(label);
32 }
33 Ok(labels)
34}
35
36pub fn extract_observable_candidates(ctx: &StructuredContext, current_app: &str) -> Vec<String> {
38 let mut seen = HashSet::new();
39 let mut out = Vec::new();
40 for pkg in ctx
41 .summary
42 .foreground_apps
43 .iter()
44 .chain(ctx.summary.notified_apps.iter())
45 {
46 if pkg == current_app || !seen.insert(pkg.clone()) {
47 continue;
48 }
49 out.push(pkg.clone());
50 }
51 out
52}
53
54pub fn load_contexts_by_label(
61 inputs: &[(String, &Path)],
62 labels: &[NextAppLabel],
63 window_secs: u64,
64) -> Result<BTreeMap<(String, i64, i64), StructuredContext>> {
65 let mut contexts = BTreeMap::new();
66
67 for (scenario_name, trace_path) in inputs {
68 let sanitized = load_and_sanitize_trace(trace_path)
69 .with_context(|| format!("loading trace {}", trace_path.display()))?;
70
71 for label in labels.iter().filter(|l| &l.scenario == scenario_name) {
72 let ctx = build_context_for_label(
73 &sanitized,
74 window_secs,
75 label.window_start_ms,
76 label.window_end_ms,
77 )
78 .with_context(|| {
79 format!(
80 "building context for scenario {} window {}-{}",
81 scenario_name, label.window_start_ms, label.window_end_ms
82 )
83 })?;
84 contexts.insert(
85 (
86 scenario_name.clone(),
87 label.window_start_ms,
88 label.window_end_ms,
89 ),
90 ctx,
91 );
92 }
93 }
94
95 Ok(contexts)
96}
97
98fn load_and_sanitize_trace(path: &Path) -> Result<Vec<SanitizedEvent>> {
99 let file = File::open(path).with_context(|| format!("opening trace {}", path.display()))?;
100 let reader = BufReader::new(file);
101 let ingress = RustCollectorIngress;
102 let sanitizer = DefaultPrivacyAirGap;
103 let mut events = Vec::new();
104
105 for (line_no, line) in reader.lines().enumerate() {
106 let line =
107 line.with_context(|| format!("reading line {} of {}", line_no + 1, path.display()))?;
108 if line.trim().is_empty() {
109 continue;
110 }
111 let value: Value = serde_json::from_str(&line)
112 .with_context(|| format!("parsing JSON line {} of {}", line_no + 1, path.display()))?;
113
114 let raw_event_value = value.get("rawEvent").cloned().unwrap_or(Value::Null);
115 if raw_event_value.is_null() {
116 continue;
117 }
118 let raw_event: RawEvent = serde_json::from_value(raw_event_value).with_context(|| {
119 format!(
120 "parsing rawEvent line {} of {}",
121 line_no + 1,
122 path.display()
123 )
124 })?;
125
126 let envelope = CollectorEnvelope {
127 schema_version: "dipecs.collector.v1".to_string(),
128 source: value
129 .get("source")
130 .and_then(Value::as_str)
131 .unwrap_or("apps.android-collector.replay")
132 .to_string(),
133 source_tier: SourceTier::PublicApi,
134 device_trace_id: value
135 .get("eventId")
136 .and_then(Value::as_str)
137 .map(String::from),
138 captured_at_ms: value
139 .get("timestampMs")
140 .and_then(Value::as_i64)
141 .unwrap_or(0),
142 received_at_ms: None,
143 raw_event,
144 };
145
146 let ingested = ingress
147 .accept(envelope)
148 .with_context(|| format!("ingress line {} of {}", line_no + 1, path.display()))?;
149 let sanitized = sanitizer.sanitize_with_tier(ingested.raw_event, ingested.source_tier);
150 events.push(sanitized);
151 }
152
153 Ok(events)
154}
155
156fn build_context_for_label(
157 sanitized_events: &[SanitizedEvent],
158 window_secs: u64,
159 window_start_ms: i64,
160 window_end_ms: i64,
161) -> Result<StructuredContext> {
162 let mut aggregator = WindowAggregator::new(window_secs, window_start_ms);
163 for event in sanitized_events {
164 if event.timestamp_ms >= window_start_ms && event.timestamp_ms < window_end_ms {
165 aggregator.push(event.clone());
166 }
167 }
168 aggregator
169 .close(window_end_ms)
170 .context("no events in window")
171}