agentmux_srv/server/remote_ops/
live_facts_ops.rs1use std::collections::HashMap;
10
11use serde_json::{json, Value};
12
13use super::{AppState, RemoteCaller, RemoteRefusal, RemoteResult};
14use crate::server::live_facts::handler::{answer, is_status_view};
15
16const DEFAULT_LIMIT: u64 = 200;
18
19fn bad(message: impl Into<String>) -> RemoteRefusal {
20 RemoteRefusal::new("bad_params", format!("agents.status: {}", message.into()))
21}
22
23fn query_of(params: &Value) -> Result<HashMap<String, String>, String> {
26 let mut out = HashMap::new();
27 let map = match params {
28 Value::Null => return Ok(out),
29 Value::Object(map) => map,
30 _ => return Err("the params are an object of the query's names".to_string()),
31 };
32 for (key, value) in map.iter().filter(|(k, _)| k.as_str() != "host") {
33 let text = match value {
34 Value::String(s) => s.clone(),
35 Value::Number(n) => n.to_string(),
36 Value::Null => continue,
37 _ => return Err(format!("{key} is text or a number")),
38 };
39 out.insert(key.clone(), text);
40 }
41 Ok(out)
42}
43
44fn without_local_places(row: &mut Value) {
46 let Some(fields) = row.as_object_mut() else { return };
47 fields.remove("working_directory");
48 fields.remove("block_id");
49 if let Some(runtime) = fields.get_mut("runtime").and_then(Value::as_object_mut) {
50 runtime.remove("block_id");
51 runtime.remove("waiting_in");
52 }
53}
54
55pub(super) async fn status(state: &AppState, _caller: &RemoteCaller, params: Value) -> RemoteResult {
58 let mut query = query_of(¶ms).map_err(bad)?;
59 query.entry("limit".to_string()).or_insert_with(|| DEFAULT_LIMIT.to_string());
60 let compact = is_status_view(&query).map_err(bad)?;
61 let body = answer(state, &query).await.map_err(bad)?;
62 let answer = if compact {
63 body
64 } else {
65 let mut agents = body.pointer("/host/agents").cloned().unwrap_or_else(|| json!([]));
66 agents.as_array_mut().into_iter().flatten().for_each(without_local_places);
67 json!({ "agents": agents, "page": body.pointer("/host/page").cloned().unwrap_or(Value::Null) })
68 };
69 Ok(json!({
70 "host": state.hostname,
71 "version": state.version,
72 "as_of_ms": agentmux_common::time::now_ms_u64(),
73 "view": if compact { "status" } else { "list" },
74 "answer": answer,
75 }))
76}
77
78#[cfg(test)]
79#[path = "live_facts_ops_tests.rs"]
80mod tests;