From 2073bce671f23f8b83271dd2312b855f9bdec292 Mon Sep 17 00:00:00 2001 From: Brad Veryard Date: Mon, 4 Nov 2024 16:57:26 +0800 Subject: [PATCH] fix: Feedback --- .../src/providers/disk/disk-provider-types.ts | 16 +-- .../src/providers/disk/disk_provider.rs | 119 +++++++++++------- packages/desktop/src/providers/disk/mod.rs | 2 - packages/desktop/src/providers/mod.rs | 2 +- .../desktop/src/providers/provider_config.rs | 5 +- .../desktop/src/providers/provider_manager.rs | 2 +- .../desktop/src/providers/provider_output.rs | 4 +- .../desktop/src/providers/provider_ref.rs | 5 +- 8 files changed, 95 insertions(+), 60 deletions(-) diff --git a/packages/client-api/src/providers/disk/disk-provider-types.ts b/packages/client-api/src/providers/disk/disk-provider-types.ts index ecc656f7..a8b7bf7d 100644 --- a/packages/client-api/src/providers/disk/disk-provider-types.ts +++ b/packages/client-api/src/providers/disk/disk-provider-types.ts @@ -11,16 +11,16 @@ export interface DiskProviderConfig { export type DiskProvider = Provider; -export interface DiskInner { +export interface Disk { name: string; - file_system: string; - mount_point: string; - total_space: number; - available_space: number; - is_removable: boolean; - disk_type: string; + fileSystem: string; + mountPoint: string; + totalSpace: number; + availableSpace: number; + isRemovable: boolean; + diskType: string; } export interface DiskOutput { - disks: DiskInner[]; + disks: Disk[]; } diff --git a/packages/desktop/src/providers/disk/disk_provider.rs b/packages/desktop/src/providers/disk/disk_provider.rs index 2bbe0b7c..97b4890b 100644 --- a/packages/desktop/src/providers/disk/disk_provider.rs +++ b/packages/desktop/src/providers/disk/disk_provider.rs @@ -4,71 +4,100 @@ use serde::{Deserialize, Serialize}; use sysinfo::{Disk, Disks}; use tokio::sync::Mutex; -use crate::{impl_interval_provider, providers::ProviderOutput}; +use crate::{ + common::{to_iec_bytes, to_si_bytes}, + impl_interval_provider, + providers::ProviderOutput, +}; #[derive(Deserialize, Debug)] #[serde(rename_all = "camelCase")] pub struct DiskProviderConfig { - pub refresh_interval: u64, + pub refresh_interval: u64, } - #[derive(Debug, Clone, PartialEq, Serialize)] #[serde(rename_all = "camelCase")] pub struct DiskOutput { - pub disks: Vec, + pub disks: Vec, } - #[derive(Debug, Clone, PartialEq, Serialize)] #[serde(rename_all = "camelCase")] pub struct DiskInner { - pub name: String, - pub file_system: String, - pub mount_point: String, - pub total_space: u64, - pub available_space: u64, - pub is_removable: bool, - pub disk_type: String, + pub name: String, + pub file_system: String, + pub mount_point: String, + pub total_space: DiskSizeMeasure, + pub available_space: DiskSizeMeasure, + pub is_removable: bool, + pub disk_type: String, } pub struct DiskProvider { - config: DiskProviderConfig, - system: Arc>, + config: DiskProviderConfig, + system: Arc>, } -impl DiskProvider { - pub fn new(config: DiskProviderConfig, system: Arc>) -> DiskProvider { - DiskProvider { config, system } - } - - fn refresh_interval_ms(&self) -> u64 { - self.config.refresh_interval - } - - async fn run_interval(&self) -> anyhow::Result { - let mut disks = self.system.lock().await; - // Refresh disk information - disks.refresh(); - - let mut list = Vec::new(); - - for disk in disks.iter() { - list.push(DiskInner { - name: disk.name().to_string_lossy().into_owned(), - file_system: disk.file_system().to_string_lossy().into_owned(), - mount_point: disk.mount_point().to_string_lossy().into_owned(), - total_space: disk.total_space(), - available_space: disk.available_space(), - is_removable: disk.is_removable(), - disk_type: format!("{:?}", disk.kind()), - }); - } - - let output = DiskOutput { disks: list }; +#[derive(Debug, Clone, PartialEq, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct DiskSizeMeasure { + pub bytes: u64, + pub si_value: f64, + pub si_unit: String, + pub iec_value: f64, + pub iec_unit: String, +} - Ok(ProviderOutput::Disk(output)) - } +impl DiskProvider { + pub fn new( + config: DiskProviderConfig, + system: Arc>, + ) -> DiskProvider { + DiskProvider { config, system } + } + + fn refresh_interval_ms(&self) -> u64 { + self.config.refresh_interval + } + + async fn run_interval(&self) -> anyhow::Result { + let mut disks = self.system.lock().await; + disks.refresh(); + + let list: Vec = disks + .iter() + .map(|disk| -> anyhow::Result { + Ok(DiskInner { + name: disk.name().to_string_lossy().to_string(), + file_system: disk.file_system().to_string_lossy().to_string(), + mount_point: disk.mount_point().to_string_lossy().to_string(), + total_space: Self::to_disk_size_measure(disk.total_space())?, + available_space: Self::to_disk_size_measure( + disk.available_space(), + )?, + is_removable: disk.is_removable(), + disk_type: disk.kind().to_string(), + }) + }) + .collect::>>()?; + + let output = DiskOutput { disks: list }; + Ok(ProviderOutput::Disk(output)) + } + + fn to_disk_size_measure(bytes: u64) -> anyhow::Result { + let (si_value, si_unit) = to_si_bytes(bytes as f64); + let (iec_value, iec_unit) = to_iec_bytes(bytes as f64); + + Ok(DiskSizeMeasure { + bytes, + si_value, + si_unit, + iec_value, + iec_unit, + }) + } } impl_interval_provider!(DiskProvider, true); diff --git a/packages/desktop/src/providers/disk/mod.rs b/packages/desktop/src/providers/disk/mod.rs index 3502559f..b291bf96 100644 --- a/packages/desktop/src/providers/disk/mod.rs +++ b/packages/desktop/src/providers/disk/mod.rs @@ -1,5 +1,3 @@ mod disk_provider; pub use disk_provider::*; - - diff --git a/packages/desktop/src/providers/mod.rs b/packages/desktop/src/providers/mod.rs index e8837440..dce7a9cc 100644 --- a/packages/desktop/src/providers/mod.rs +++ b/packages/desktop/src/providers/mod.rs @@ -1,5 +1,6 @@ mod battery; mod cpu; +mod disk; mod host; mod ip; #[cfg(windows)] @@ -7,7 +8,6 @@ mod keyboard; #[cfg(windows)] mod komorebi; mod memory; -mod disk; mod network; mod provider; mod provider_config; diff --git a/packages/desktop/src/providers/provider_config.rs b/packages/desktop/src/providers/provider_config.rs index 89ae7557..4410bccc 100644 --- a/packages/desktop/src/providers/provider_config.rs +++ b/packages/desktop/src/providers/provider_config.rs @@ -1,7 +1,10 @@ use serde::Deserialize; use super::{ - battery::BatteryProviderConfig, cpu::CpuProviderConfig, disk::DiskProviderConfig, host::HostProviderConfig, ip::IpProviderConfig, memory::MemoryProviderConfig, network::NetworkProviderConfig, weather::WeatherProviderConfig + battery::BatteryProviderConfig, cpu::CpuProviderConfig, + disk::DiskProviderConfig, host::HostProviderConfig, + ip::IpProviderConfig, memory::MemoryProviderConfig, + network::NetworkProviderConfig, weather::WeatherProviderConfig, }; #[cfg(windows)] use super::{ diff --git a/packages/desktop/src/providers/provider_manager.rs b/packages/desktop/src/providers/provider_manager.rs index 97d533a9..73b61d35 100644 --- a/packages/desktop/src/providers/provider_manager.rs +++ b/packages/desktop/src/providers/provider_manager.rs @@ -12,7 +12,7 @@ use super::{ProviderConfig, ProviderRef}; pub struct SharedProviderState { pub sysinfo: Arc>, pub netinfo: Arc>, - pub diskinfo: Arc> + pub diskinfo: Arc>, } /// Manages the creation and cleanup of providers. diff --git a/packages/desktop/src/providers/provider_output.rs b/packages/desktop/src/providers/provider_output.rs index 39680771..4ef23ab7 100644 --- a/packages/desktop/src/providers/provider_output.rs +++ b/packages/desktop/src/providers/provider_output.rs @@ -1,7 +1,9 @@ use serde::Serialize; use super::{ - battery::BatteryOutput, cpu::CpuOutput, disk::DiskOutput, host::HostOutput, ip::IpOutput, memory::MemoryOutput, network::NetworkOutput, weather::WeatherOutput + battery::BatteryOutput, cpu::CpuOutput, disk::DiskOutput, + host::HostOutput, ip::IpOutput, memory::MemoryOutput, + network::NetworkOutput, weather::WeatherOutput, }; #[cfg(windows)] use super::{keyboard::KeyboardOutput, komorebi::KomorebiOutput}; diff --git a/packages/desktop/src/providers/provider_ref.rs b/packages/desktop/src/providers/provider_ref.rs index 863e9b32..9c72f062 100644 --- a/packages/desktop/src/providers/provider_ref.rs +++ b/packages/desktop/src/providers/provider_ref.rs @@ -11,7 +11,10 @@ use tokio::{ use tracing::{info, warn}; use super::{ - battery::BatteryProvider, cpu::CpuProvider, disk::DiskProvider, host::HostProvider, ip::IpProvider, memory::MemoryProvider, network::NetworkProvider, weather::WeatherProvider, Provider, ProviderConfig, ProviderOutput, SharedProviderState + battery::BatteryProvider, cpu::CpuProvider, disk::DiskProvider, + host::HostProvider, ip::IpProvider, memory::MemoryProvider, + network::NetworkProvider, weather::WeatherProvider, Provider, + ProviderConfig, ProviderOutput, SharedProviderState, }; #[cfg(windows)] use super::{keyboard::KeyboardProvider, komorebi::KomorebiProvider};