sl4f_lib/system_metrics/
facade.rs1use crate::system_metrics::types::{
6 CpuLoadLoggerResult, StartLoggingForeverRequest, StartLoggingRequest,
7};
8use anyhow::Error;
9use fidl_fuchsia_power_metrics::{CpuLoad, Metric, RecorderMarker};
10use fuchsia_component::client::connect_to_protocol;
11use serde_json::{from_value, Value};
12
13const CLIENT_ID: &'static str = "sl4f_cpu_load";
14
15#[derive(Debug)]
16pub struct SystemMetricsFacade {}
17
18impl SystemMetricsFacade {
19 pub fn new() -> SystemMetricsFacade {
20 SystemMetricsFacade {}
21 }
22
23 pub async fn start_logging(&self, args: Value) -> Result<CpuLoadLoggerResult, Error> {
29 let params: StartLoggingRequest = from_value(args)?;
30 connect_to_protocol::<RecorderMarker>()?
31 .start_logging(
32 CLIENT_ID,
33 &[Metric::CpuLoad(CpuLoad { interval_ms: params.interval_ms })],
34 params.duration_ms,
35 false,
36 false,
37 )
38 .await?
39 .map_err(|e| format_err!("Received RecorderError: {:?}", e))?;
40 Ok(CpuLoadLoggerResult::Success)
41 }
42
43 pub async fn start_logging_forever(&self, args: Value) -> Result<CpuLoadLoggerResult, Error> {
45 let params: StartLoggingForeverRequest = from_value(args)?;
46 connect_to_protocol::<RecorderMarker>()?
47 .start_logging_forever(
48 CLIENT_ID,
49 &[Metric::CpuLoad(CpuLoad { interval_ms: params.interval_ms })],
50 false,
51 false,
52 )
53 .await?
54 .map_err(|e| format_err!("Received RecorderError: {:?}", e))?;
55 Ok(CpuLoadLoggerResult::Success)
56 }
57
58 pub async fn stop_logging(&self, _args: Value) -> Result<CpuLoadLoggerResult, Error> {
64 let logger = connect_to_protocol::<RecorderMarker>()?;
65 logger.stop_logging(CLIENT_ID).await?;
66 Ok(CpuLoadLoggerResult::Success)
67 }
68}