Compare commits
4 changed files with 34 additions and 97 deletions
|
|
@ -30,8 +30,6 @@ impl Graph {
|
|||
if self.timeline.len() > 500 {
|
||||
self.timeline.pop_front();
|
||||
}
|
||||
|
||||
self.cache.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,28 +0,0 @@
|
|||
use iced::{
|
||||
futures::{self, SinkExt},
|
||||
stream,
|
||||
};
|
||||
use smol::Timer;
|
||||
|
||||
use crate::sysinfo_snapshot::SysinfoSnapshot;
|
||||
|
||||
pub fn create() -> impl futures::Stream<Item = SysinfoSnapshot> {
|
||||
stream::channel(
|
||||
1,
|
||||
async |mut sender: futures::channel::mpsc::Sender<SysinfoSnapshot>| {
|
||||
let mut sys = sysinfo::System::new();
|
||||
let refreshes = sysinfo::RefreshKind::nothing()
|
||||
.with_memory(sysinfo::MemoryRefreshKind::everything())
|
||||
.with_cpu(sysinfo::CpuRefreshKind::nothing().with_cpu_usage());
|
||||
|
||||
sys.refresh_specifics(refreshes);
|
||||
Timer::after(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL).await;
|
||||
|
||||
loop {
|
||||
sys.refresh_specifics(refreshes);
|
||||
sender.send(SysinfoSnapshot::from(&sys)).await.unwrap();
|
||||
Timer::after(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL).await;
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
@ -1,11 +1,12 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use iced::{Color, Element, Length, Renderer, Task, Theme, application, theme, widget};
|
||||
use iced::{
|
||||
Color, Element, Length, Renderer, Task, Theme, application, futures::SinkExt, stream, theme,
|
||||
widget,
|
||||
};
|
||||
use smol::Timer;
|
||||
|
||||
use crate::{app::graph::Graph, sysinfo_snapshot::SysinfoSnapshot};
|
||||
|
||||
mod graph;
|
||||
mod info_stream;
|
||||
|
||||
enum Message {
|
||||
Info(SysinfoSnapshot),
|
||||
|
|
@ -13,33 +14,38 @@ enum Message {
|
|||
|
||||
#[derive(Default)]
|
||||
struct State {
|
||||
uptime: Duration,
|
||||
|
||||
total_cpu_graph: Graph,
|
||||
cpu_graphs: Vec<Graph>,
|
||||
memory_graph: Graph,
|
||||
swap_graph: Graph,
|
||||
}
|
||||
|
||||
impl State {
|
||||
fn new() -> (Self, Task<Message>) {
|
||||
(
|
||||
Self::default(),
|
||||
Task::run(info_stream::create(), Message::Info),
|
||||
Task::run(
|
||||
stream::channel(1, async |mut sender| {
|
||||
let mut sys = sysinfo::System::new();
|
||||
let refreshes = sysinfo::RefreshKind::nothing()
|
||||
.with_cpu(sysinfo::CpuRefreshKind::nothing().with_cpu_usage());
|
||||
|
||||
sys.refresh_specifics(refreshes);
|
||||
Timer::after(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL).await;
|
||||
|
||||
loop {
|
||||
sys.refresh_specifics(refreshes);
|
||||
Timer::after(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL).await;
|
||||
sender.send(SysinfoSnapshot::from(&sys)).await.unwrap();
|
||||
}
|
||||
}),
|
||||
Message::Info,
|
||||
),
|
||||
)
|
||||
}
|
||||
fn update(&mut self, message: Message) -> Task<Message> {
|
||||
match message {
|
||||
Message::Info(snapshot) => {
|
||||
self.uptime = snapshot.uptime();
|
||||
|
||||
self.total_cpu_graph.update(snapshot.cpu_total);
|
||||
|
||||
self.memory_graph
|
||||
.update(snapshot.memory as f32 / snapshot.memory_total as f32 * 100.);
|
||||
|
||||
self.swap_graph
|
||||
.update(snapshot.swap as f32 / snapshot.swap_total as f32 * 100.);
|
||||
self.total_cpu_graph.cache.clear();
|
||||
|
||||
if self.cpu_graphs.len() != snapshot.cpus.len() {
|
||||
self.cpu_graphs = snapshot
|
||||
|
|
@ -51,6 +57,7 @@ impl State {
|
|||
for (graph, value) in self.cpu_graphs.iter_mut().zip(snapshot.cpus.into_iter())
|
||||
{
|
||||
graph.update(value);
|
||||
graph.cache.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -59,9 +66,8 @@ impl State {
|
|||
Task::none()
|
||||
}
|
||||
fn view(&self) -> Element<'_, Message, Theme, Renderer> {
|
||||
widget::column([
|
||||
widget::row([
|
||||
widget::canvas(&self.total_cpu_graph)
|
||||
widget::Canvas::new(&self.total_cpu_graph)
|
||||
.width(Length::Fill)
|
||||
.height(Length::Fill)
|
||||
.into(),
|
||||
|
|
@ -74,30 +80,12 @@ impl State {
|
|||
.height(Length::Fill)
|
||||
.into(),
|
||||
])
|
||||
.into(),
|
||||
widget::row([
|
||||
widget::canvas(&self.memory_graph)
|
||||
.width(Length::Fill)
|
||||
.height(Length::Fill)
|
||||
.into(),
|
||||
widget::canvas(&self.swap_graph)
|
||||
.width(Length::Fill)
|
||||
.height(Length::Fill)
|
||||
.into(),
|
||||
])
|
||||
.into(),
|
||||
])
|
||||
.into()
|
||||
}
|
||||
|
||||
fn title(&self) -> String {
|
||||
format!("itop {:?}", self.uptime)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run() -> Result<(), impl std::error::Error> {
|
||||
application(State::new, State::update, State::view)
|
||||
.title(State::title)
|
||||
.theme(theme::Theme::custom(
|
||||
"high-contrast-dark",
|
||||
theme::Palette {
|
||||
|
|
|
|||
|
|
@ -1,35 +1,14 @@
|
|||
use std::time::Duration;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SysinfoSnapshot {
|
||||
uptime: u64,
|
||||
|
||||
pub cpu_total: f32,
|
||||
pub cpus: Vec<f32>,
|
||||
|
||||
pub memory: u64,
|
||||
pub memory_total: u64,
|
||||
pub swap: u64,
|
||||
pub swap_total: u64,
|
||||
}
|
||||
impl SysinfoSnapshot {
|
||||
pub fn uptime(&self) -> Duration {
|
||||
Duration::from_secs(self.uptime)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&sysinfo::System> for SysinfoSnapshot {
|
||||
fn from(value: &sysinfo::System) -> Self {
|
||||
Self {
|
||||
uptime: sysinfo::System::uptime(),
|
||||
|
||||
cpu_total: value.global_cpu_usage(),
|
||||
cpus: value.cpus().iter().map(|cpu| cpu.cpu_usage()).collect(),
|
||||
|
||||
memory: value.used_memory(),
|
||||
memory_total: value.total_memory(),
|
||||
swap: value.used_swap(),
|
||||
swap_total: value.total_swap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue