-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
188 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/client-api/src/providers/disk/create-disk-provider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { z } from 'zod'; | ||
|
||
import { createBaseProvider } from '../create-base-provider'; | ||
import { onProviderEmit } from '~/desktop'; | ||
import type { | ||
DiskOutput, | ||
DiskProvider, | ||
DiskProviderConfig, | ||
} from './disk-provider-types'; | ||
|
||
const diskProviderConfigSchema = z.object({ | ||
type: z.literal('disk'), | ||
refreshInterval: z.coerce.number().default(60 * 1000), | ||
}); | ||
|
||
export function createDiskProvider( | ||
config: DiskProviderConfig, | ||
): DiskProvider { | ||
const mergedConfig = diskProviderConfigSchema.parse(config); | ||
|
||
return createBaseProvider(mergedConfig, async queue => { | ||
return onProviderEmit<DiskOutput>(mergedConfig, ({ result }) => { | ||
if ('error' in result) { | ||
queue.error(result.error); | ||
} else { | ||
queue.output(result.output); | ||
} | ||
}); | ||
}); | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/client-api/src/providers/disk/disk-provider-types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { Provider } from '../create-base-provider'; | ||
|
||
export interface DiskProviderConfig { | ||
type: 'disk'; | ||
|
||
/** | ||
* How often this provider refreshes in milliseconds. | ||
*/ | ||
refreshInterval?: number; | ||
} | ||
|
||
export type DiskProvider = Provider<DiskProviderConfig, DiskOutput>; | ||
|
||
export interface Disk { | ||
name: string; | ||
fileSystem: string; | ||
mountPoint: string; | ||
totalSpace: number; | ||
availableSpace: number; | ||
isRemovable: boolean; | ||
diskType: string; | ||
} | ||
|
||
export interface DiskOutput { | ||
disks: Disk[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use std::{any::Any, sync::Arc}; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
use sysinfo::{Disk, Disks}; | ||
use tokio::sync::Mutex; | ||
|
||
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, | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct DiskOutput { | ||
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: DiskSizeMeasure, | ||
pub available_space: DiskSizeMeasure, | ||
pub is_removable: bool, | ||
pub disk_type: String, | ||
} | ||
|
||
pub struct DiskProvider { | ||
config: DiskProviderConfig, | ||
system: Arc<Mutex<Disks>>, | ||
} | ||
|
||
#[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, | ||
} | ||
|
||
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod disk_provider; | ||
|
||
pub use disk_provider::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod battery; | ||
mod cpu; | ||
mod disk; | ||
mod host; | ||
mod ip; | ||
#[cfg(windows)] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters