rework the stats routes

This commit is contained in:
mbecker20
2023-06-09 08:31:46 +00:00
parent 51ebee2910
commit 8ad84a8cec
10 changed files with 257 additions and 235 deletions

View File

@@ -3,10 +3,10 @@ use std::path::PathBuf;
use serde::{Serialize, Deserialize};
use typeshare::typeshare;
use crate::Timelength;
use crate::{I64, Timelength};
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct SystemInformation {
pub name: Option<String>,
pub os: Option<String>,
@@ -17,60 +17,47 @@ pub struct SystemInformation {
}
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct SystemStats {
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct AllSystemStats {
pub basic: BasicSystemStats,
pub cpu: CpuUsage,
pub disk: DiskUsage,
pub network: NetworkUsage,
#[serde(default)]
pub processes: Vec<SystemProcess>,
#[serde(default)]
pub componenets: Vec<SystemComponent>,
pub polling_rate: Timelength,
pub refresh_ts: I64,
pub refresh_list_ts: I64,
}
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct BasicSystemStats {
pub system_load: f64,
pub cpu_perc: f32,
pub cpu_freq_mhz: f64,
pub mem_used_gb: f64, // in GB
pub mem_total_gb: f64, // in GB
pub disk: DiskUsage,
pub mem_used_gb: f64,
pub mem_total_gb: f64,
pub disk_used_gb: f64,
pub disk_total_gb: f64,
}
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct CpuUsage {
pub cpu_perc: f32,
pub cpu_freq_mhz: f64,
#[serde(default)]
pub cpus: Vec<SingleCpuUsage>,
#[serde(default)]
pub networks: Vec<SystemNetwork>,
#[serde(default)]
pub components: Vec<SystemComponent>,
#[serde(default)]
pub processes: Vec<SystemProcess>,
pub polling_rate: Timelength,
pub refresh_ts: u128,
pub refresh_list_ts: u128,
}
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SystemProcess {
pub pid: u32,
pub struct SingleCpuUsage {
pub name: String,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub exe: String,
pub cmd: Vec<String>,
#[serde(default)]
pub start_time: f64,
pub cpu_perc: f32,
pub mem_mb: f64,
pub disk_read_kb: f64,
pub disk_write_kb: f64,
}
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SystemNetwork {
pub name: String,
pub recieved_kb: f64, // in kB
pub transmitted_kb: f64, // in kB
}
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SystemComponent {
pub label: String,
pub temp: f32,
pub max: f32,
#[serde(skip_serializing_if = "Option::is_none")]
pub critical: Option<f32>,
pub usage: f32,
}
#[typeshare]
@@ -92,9 +79,44 @@ pub struct SingleDiskUsage {
pub total_gb: f64, // in GB
}
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct NetworkUsage {
pub recieved_kb: f64,
pub transmitted_kb: f64,
pub networks: Vec<SystemNetwork>,
}
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SingleCpuUsage {
pub struct SystemNetwork {
pub name: String,
pub usage: f32,
pub recieved_kb: f64, // in kB
pub transmitted_kb: f64, // in kB
}
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SystemProcess {
pub pid: u32,
pub name: String,
#[serde(default)]
pub exe: String,
pub cmd: Vec<String>,
#[serde(default)]
pub start_time: f64,
pub cpu_perc: f32,
pub mem_mb: f64,
pub disk_read_kb: f64,
pub disk_write_kb: f64,
}
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SystemComponent {
pub label: String,
pub temp: f32,
pub max: f32,
#[serde(skip_serializing_if = "Option::is_none")]
pub critical: Option<f32>,
}

View File

@@ -7,6 +7,9 @@ pub mod core_api;
pub mod periphery_api;
pub mod entities;
#[typeshare(serialized_as = "number")]
pub type I64 = i64;
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq, Diff)]
#[diff(attr(#[derive(Debug, PartialEq, Serialize)]))]

View File

@@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
use crate::SystemCommand;
use self::requests::{GetHealth, GetVersion};
use self::requests::{GetHealth, GetVersion, GetSystemInformation, GetBasicSystemStats, GetDiskUsage, GetNetworkUsage, GetSystemProcesses, GetAllSystemStats, GetSystemComponents, GetCpuUsage};
pub mod requests;
@@ -12,8 +12,14 @@ pub enum PeripheryRequest {
// GET
GetHealth(GetHealth),
GetVersion(GetVersion),
GetSystemInformation {},
GetSystemStats {},
GetSystemInformation(GetSystemInformation),
GetAllSystemStats(GetAllSystemStats),
GetBasicSystemStats(GetBasicSystemStats),
GetCpuUsage(GetCpuUsage),
GetDiskUsage(GetDiskUsage),
GetNetworkUsage(GetNetworkUsage),
GetSystemProcesses(GetSystemProcesses),
GetSystemComponents(GetSystemComponents),
GetAccounts {},
GetSecrets {},
GetContainerList {},

View File

@@ -1,9 +1,12 @@
use serde::{Deserialize, Serialize};
use typeshare::typeshare;
use crate::impl_has_response;
use crate::{
entities::server::{BasicSystemStats, DiskUsage, SystemInformation, SystemProcess, NetworkUsage, AllSystemStats, SystemComponent, CpuUsage},
impl_has_response,
};
// GET HEALTH
//
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -15,7 +18,7 @@ pub struct GetHealthResponse {}
impl_has_response!(GetHealth, GetHealthResponse);
// GET VERSION
//
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -29,14 +32,66 @@ pub struct GetVersionResponse {
impl_has_response!(GetVersion, GetVersionResponse);
//
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GetSystemInformation {}
impl_has_response!(GetSystemInformation, SystemInformation);
//
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GetSystemInformationResponse {
pub version: String,
}
pub struct GetAllSystemStats {}
impl_has_response!(GetSystemInformation, GetSystemInformationResponse);
impl_has_response!(GetAllSystemStats, AllSystemStats);
//
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GetBasicSystemStats {}
impl_has_response!(GetBasicSystemStats, BasicSystemStats);
//
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GetCpuUsage {}
impl_has_response!(GetCpuUsage, CpuUsage);
//
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GetDiskUsage {}
impl_has_response!(GetDiskUsage, DiskUsage);
//
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GetNetworkUsage {}
impl_has_response!(GetNetworkUsage, NetworkUsage);
//
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GetSystemProcesses {}
impl_has_response!(GetSystemProcesses, Vec<SystemProcess>);
//
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GetSystemComponents {}
impl_has_response!(GetSystemComponents, Vec<SystemComponent>);