Skip to content

Commit

Permalink
fix: Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
veryard committed Nov 4, 2024
1 parent dedf549 commit 2073bce
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 60 deletions.
16 changes: 8 additions & 8 deletions packages/client-api/src/providers/disk/disk-provider-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ export interface DiskProviderConfig {

export type DiskProvider = Provider<DiskProviderConfig, DiskOutput>;

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[];
}
119 changes: 74 additions & 45 deletions packages/desktop/src/providers/disk/disk_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DiskInner>,
pub disks: Vec<DiskInner>,
}


#[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<Mutex<Disks>>,
config: DiskProviderConfig,
system: Arc<Mutex<Disks>>,
}

impl DiskProvider {
pub fn new(config: DiskProviderConfig, system: Arc<Mutex<Disks>>) -> DiskProvider {
DiskProvider { config, system }
}

fn refresh_interval_ms(&self) -> u64 {
self.config.refresh_interval
}

async fn run_interval(&self) -> anyhow::Result<ProviderOutput> {
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<Mutex<Disks>>,
) -> DiskProvider {
DiskProvider { config, system }
}

fn refresh_interval_ms(&self) -> u64 {
self.config.refresh_interval
}

async fn run_interval(&self) -> anyhow::Result<ProviderOutput> {
let mut disks = self.system.lock().await;
disks.refresh();

let list: Vec<DiskInner> = disks
.iter()
.map(|disk| -> anyhow::Result<DiskInner> {
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::<anyhow::Result<Vec<DiskInner>>>()?;

let output = DiskOutput { disks: list };
Ok(ProviderOutput::Disk(output))
}

fn to_disk_size_measure(bytes: u64) -> anyhow::Result<DiskSizeMeasure> {
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);
2 changes: 0 additions & 2 deletions packages/desktop/src/providers/disk/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
mod disk_provider;

pub use disk_provider::*;


2 changes: 1 addition & 1 deletion packages/desktop/src/providers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
mod battery;
mod cpu;
mod disk;
mod host;
mod ip;
#[cfg(windows)]
mod keyboard;
#[cfg(windows)]
mod komorebi;
mod memory;
mod disk;
mod network;
mod provider;
mod provider_config;
Expand Down
5 changes: 4 additions & 1 deletion packages/desktop/src/providers/provider_config.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/src/providers/provider_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use super::{ProviderConfig, ProviderRef};
pub struct SharedProviderState {
pub sysinfo: Arc<Mutex<System>>,
pub netinfo: Arc<Mutex<Networks>>,
pub diskinfo: Arc<Mutex<Disks>>
pub diskinfo: Arc<Mutex<Disks>>,
}

/// Manages the creation and cleanup of providers.
Expand Down
4 changes: 3 additions & 1 deletion packages/desktop/src/providers/provider_output.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
5 changes: 4 additions & 1 deletion packages/desktop/src/providers/provider_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit 2073bce

Please sign in to comment.