1use std::{sync::RwLock, time::Duration};
2
3use tokio_util::sync::CancellationToken;
4use tracing::instrument;
5use wayle_core::Property;
6
7use crate::{
8 polling,
9 service::SysinfoService,
10 types::{CpuData, DiskData, MemoryData, NetworkData},
11};
12
13const DEFAULT_CPU_INTERVAL: Duration = Duration::from_secs(2);
14const DEFAULT_MEMORY_INTERVAL: Duration = Duration::from_secs(5);
15const DEFAULT_DISK_INTERVAL: Duration = Duration::from_secs(30);
16const DEFAULT_NETWORK_INTERVAL: Duration = Duration::from_secs(2);
17
18pub struct SysinfoServiceBuilder {
20 cpu_interval: Duration,
21 memory_interval: Duration,
22 disk_interval: Duration,
23 network_interval: Duration,
24 cpu_temp_sensor: String,
25}
26
27impl SysinfoServiceBuilder {
28 pub fn new() -> Self {
30 Self {
31 cpu_interval: DEFAULT_CPU_INTERVAL,
32 memory_interval: DEFAULT_MEMORY_INTERVAL,
33 disk_interval: DEFAULT_DISK_INTERVAL,
34 network_interval: DEFAULT_NETWORK_INTERVAL,
35 cpu_temp_sensor: String::from("auto"),
36 }
37 }
38
39 pub fn cpu_interval(mut self, interval: Duration) -> Self {
41 self.cpu_interval = interval;
42 self
43 }
44
45 pub fn memory_interval(mut self, interval: Duration) -> Self {
47 self.memory_interval = interval;
48 self
49 }
50
51 pub fn disk_interval(mut self, interval: Duration) -> Self {
53 self.disk_interval = interval;
54 self
55 }
56
57 pub fn network_interval(mut self, interval: Duration) -> Self {
59 self.network_interval = interval;
60 self
61 }
62
63 pub fn cpu_temp_sensor(mut self, sensor: impl Into<String>) -> Self {
68 self.cpu_temp_sensor = sensor.into();
69 self
70 }
71
72 #[instrument(skip_all, name = "SysinfoService::build")]
74 pub fn build(self) -> SysinfoService {
75 let cancellation_token = CancellationToken::new();
76
77 let cpu = Property::new(CpuData::default());
78 let memory = Property::new(MemoryData::default());
79 let disks = Property::new(Vec::<DiskData>::new());
80 let network = Property::new(Vec::<NetworkData>::new());
81
82 let tokens = polling::spawn_polling_tasks(
83 &cancellation_token,
84 &cpu,
85 &memory,
86 &disks,
87 &network,
88 self.cpu_interval,
89 self.memory_interval,
90 self.disk_interval,
91 self.network_interval,
92 self.cpu_temp_sensor.clone(),
93 );
94
95 SysinfoService {
96 cancellation_token,
97 cpu_token: RwLock::new(tokens.cpu),
98 memory_token: RwLock::new(tokens.memory),
99 disk_token: RwLock::new(tokens.disk),
100 network_token: RwLock::new(tokens.network),
101 cpu_interval: RwLock::new(self.cpu_interval),
102 cpu_temp_sensor: RwLock::new(self.cpu_temp_sensor),
103 cpu,
104 memory,
105 disks,
106 network,
107 }
108 }
109}
110
111impl Default for SysinfoServiceBuilder {
112 fn default() -> Self {
113 Self::new()
114 }
115}