agentmux_srv_core/backend/audit_query/
other_channels.rs1use std::time::Duration;
11
12use crate::backend::rpc_types::{AuditChannelRows, AuditQueryReq, AuditQueryResult};
13use crate::backend::swarm_remote::ChannelStatusTarget;
14
15pub const OWN_ROWS_PATH: &str = "/agentmux/audit/own";
17
18const TIMEOUT: Duration = Duration::from_millis(2500);
20const MAX_RESPONSE_BYTES: usize = 4 * 1024 * 1024;
22
23pub fn request_for_others(req: &AuditQueryReq) -> AuditQueryReq {
26 AuditQueryReq { cursor: None, totals: None, scope: Some("channel".to_string()), ..req.clone() }
27}
28
29pub async fn ask(http: &reqwest::Client, targets: &[ChannelStatusTarget], req: &AuditQueryReq) -> Vec<AuditChannelRows> {
31 let req = request_for_others(req);
32 let answers = futures_util::future::join_all(targets.iter().map(|t| {
33 let req = &req;
34 async move {
35 let resp = http
36 .post(format!("{}{OWN_ROWS_PATH}", t.local_url))
37 .header(agentmux_common::AUTH_KEY_HEADER, &t.auth_key)
38 .json(req)
39 .timeout(TIMEOUT)
40 .send()
41 .await
42 .ok()
43 .filter(|r| r.status().is_success())?;
44 let body = crate::backend::lan::discovery::LanDiscovery::read_body_capped(resp, MAX_RESPONSE_BYTES).await.ok()?;
45 let result: AuditQueryResult = serde_json::from_slice(&body).ok()?;
46 Some(rows_of(&t.channel, result))
47 }
48 }))
49 .await;
50 answers.into_iter().flatten().filter(|c| !c.entries.is_empty()).collect()
51}
52
53pub fn rows_of(channel: &str, result: AuditQueryResult) -> AuditChannelRows {
56 let mut entries = result.entries;
57 for e in &mut entries {
58 e.id = format!("{channel}:{}", e.id);
59 e.links.pane = None;
60 for end in [e.from.as_mut(), e.to.as_mut()].into_iter().flatten() {
61 end.links.pane = None;
62 }
63 let provenance = e.provenance.get_or_insert_with(Default::default);
64 if provenance.channel.as_deref().is_none_or(str::is_empty) {
65 provenance.channel = Some(channel.to_string());
66 }
67 }
68 AuditChannelRows { channel: channel.to_string(), entries, more: result.next_cursor.is_some() }
69}
70
71#[cfg(test)]
72#[path = "other_channels_tests.rs"]
73mod tests;