sl4f_lib/system_metrics/
facade.rsuse crate::system_metrics::types::{
CpuLoadLoggerResult, StartLoggingForeverRequest, StartLoggingRequest,
};
use anyhow::Error;
use fidl_fuchsia_power_metrics::{CpuLoad, Metric, RecorderMarker};
use fuchsia_component::client::connect_to_protocol;
use serde_json::{from_value, Value};
const CLIENT_ID: &'static str = "sl4f_cpu_load";
#[derive(Debug)]
pub struct SystemMetricsFacade {}
impl SystemMetricsFacade {
pub fn new() -> SystemMetricsFacade {
SystemMetricsFacade {}
}
pub async fn start_logging(&self, args: Value) -> Result<CpuLoadLoggerResult, Error> {
let params: StartLoggingRequest = from_value(args)?;
connect_to_protocol::<RecorderMarker>()?
.start_logging(
CLIENT_ID,
&[Metric::CpuLoad(CpuLoad { interval_ms: params.interval_ms })],
params.duration_ms,
false,
false,
)
.await?
.map_err(|e| format_err!("Received RecorderError: {:?}", e))?;
Ok(CpuLoadLoggerResult::Success)
}
pub async fn start_logging_forever(&self, args: Value) -> Result<CpuLoadLoggerResult, Error> {
let params: StartLoggingForeverRequest = from_value(args)?;
connect_to_protocol::<RecorderMarker>()?
.start_logging_forever(
CLIENT_ID,
&[Metric::CpuLoad(CpuLoad { interval_ms: params.interval_ms })],
false,
false,
)
.await?
.map_err(|e| format_err!("Received RecorderError: {:?}", e))?;
Ok(CpuLoadLoggerResult::Success)
}
pub async fn stop_logging(&self, _args: Value) -> Result<CpuLoadLoggerResult, Error> {
let logger = connect_to_protocol::<RecorderMarker>()?;
logger.stop_logging(CLIENT_ID).await?;
Ok(CpuLoadLoggerResult::Success)
}
}