Skip to main content

AirLibrary/Client/AirClient/
GetMetrics.rs

1//! `AirClient::GetMetrics` - fetches a metrics snapshot from the Air
2//! daemon. The wire response is a `HashMap<String, String>` so the daemon
3//! can ship arbitrary keys; this method extracts the canonical numeric
4//! fields ([`AirMetrics::Struct`]) by name and parses each as `f64`,
5//! defaulting to `0.0` on missing / unparseable entries.
6
7use tonic::Request;
8
9use crate::{
10	AirError,
11	Client::AirClient::{AirClient, AirMetrics},
12	Vine::Generated::air::MetricsRequest,
13	dev_log,
14};
15
16impl AirClient {
17	/// Gets metrics from the Air daemon.
18	///
19	/// # Arguments
20	///
21	/// - `request_id` - opaque correlation id.
22	/// - `metric_type` - optional filter (`"performance"` / `"resources"` /
23	///   `"requests"`). `None` requests the full metric set.
24	pub async fn GetMetrics(
25		&self,
26
27		request_id:String,
28
29		metric_type:Option<String>,
30	) -> Result<AirMetrics::Struct, AirError> {
31		dev_log!("grpc", "[AirClient] Getting metrics (type: {:?})", metric_type.as_deref());
32
33		let RequestPayload = MetricsRequest { request_id, metric_type:metric_type.unwrap_or_default() };
34
35		let Client = self
36			.Client()
37			.ok_or_else(|| AirError::Network("Air client not initialized".to_string()))?;
38
39		let mut ClientGuard = Client.lock().await;
40
41		match ClientGuard.get_metrics(Request::new(RequestPayload)).await {
42			Ok(Response) => {
43				let Response = Response.into_inner();
44
45				dev_log!("grpc", "[AirClient] Metrics retrieved");
46
47				let Metrics = AirMetrics::Struct {
48					memory_usage_mb:Response
49						.metrics
50						.get("memory_usage_mb")
51						.and_then(|S| S.parse::<f64>().ok())
52						.unwrap_or(0.0),
53
54					cpu_usage_percent:Response
55						.metrics
56						.get("cpu_usage_percent")
57						.and_then(|S| S.parse::<f64>().ok())
58						.unwrap_or(0.0),
59
60					network_usage_mbps:Response
61						.metrics
62						.get("network_usage_mbps")
63						.and_then(|S| S.parse::<f64>().ok())
64						.unwrap_or(0.0),
65
66					disk_usage_mb:Response
67						.metrics
68						.get("disk_usage_mb")
69						.and_then(|S| S.parse::<f64>().ok())
70						.unwrap_or(0.0),
71
72					average_response_time:Response
73						.metrics
74						.get("average_response_time")
75						.and_then(|S| S.parse::<f64>().ok())
76						.unwrap_or(0.0),
77				};
78
79				Ok(Metrics)
80			},
81
82			Err(Status) => {
83				dev_log!("grpc", "error: [AirClient] Get metrics RPC error: {}", Status);
84
85				Err(AirError::Network(format!("Get metrics RPC error: {}", Status)))
86			},
87		}
88	}
89}