From 203cff23f9afcb24b0c8ae9124060097f00c03c6 Mon Sep 17 00:00:00 2001 From: alemi Date: Wed, 8 Jun 2022 12:58:07 +0200 Subject: [PATCH] feat: show file size in footer (update every s) also made jq_rs use bundled jqlib because rust-analyzer won't work otherwise --- Cargo.toml | 4 ++-- src/app/data/mod.rs | 29 +++++++++++++---------------- src/app/mod.rs | 15 +++++---------- src/app/util.rs | 23 +++++++++++++++++++++++ src/app/worker.rs | 3 +++ 5 files changed, 46 insertions(+), 28 deletions(-) create mode 100644 src/app/util.rs diff --git a/Cargo.toml b/Cargo.toml index a09e711..153e5b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dashboard" -version = "0.1.0" +version = "0.1.1" edition = "2021" [[bin]] @@ -16,7 +16,7 @@ eframe = { version = "0.18", features = ["persistence"] } serde = { version = "1", features = ["derive"] } serde_json = "1" rusqlite = { version = "0.27" } -jq-rs = "0.4" +jq-rs = { version = "0.4", features = ["bundled"] } ureq = { version = "2", features = ["json"] } [env] diff --git a/src/app/data/mod.rs b/src/app/data/mod.rs index d6b53c3..056e84b 100644 --- a/src/app/data/mod.rs +++ b/src/app/data/mod.rs @@ -37,35 +37,39 @@ impl From:: for FetchError { pub struct ApplicationState { pub run: bool, + pub file_path: PathBuf, + pub file_size: RwLock, pub panels: RwLock>, pub storage: Mutex, } impl ApplicationState { pub fn new(path:PathBuf) -> Self { - let storage = SQLiteDataStore::new(path).unwrap(); + let storage = SQLiteDataStore::new(path.clone()).unwrap(); let panels = storage.load_panels().unwrap(); return ApplicationState{ run: true, + file_size: RwLock::new(std::fs::metadata(path.clone()).unwrap().len()), + file_path: path, panels: RwLock::new(panels), storage: Mutex::new(storage), }; } pub fn add_panel(&self, name:&str) -> Result<(), FetchError> { - let panel = self.storage.lock().unwrap().new_panel(name, 100, 200, 280)?; // TODO make values customizable and useful - self.panels.write().unwrap().push(panel); + let panel = self.storage.lock().expect("Storage Mutex poisoned").new_panel(name, 100, 200, 280)?; // TODO make values customizable and useful + self.panels.write().expect("Panels RwLock poisoned").push(panel); Ok(()) } pub fn add_source(&self, panel_id:i32, name:&str, url:&str, query_x:&str, query_y:&str) -> Result<(), FetchError> { - let source = self.storage.lock().unwrap().new_source(panel_id, name, url, query_x, query_y)?; - let panels = self.panels.read().unwrap(); + let source = self.storage.lock().expect("Storage Mutex poisoned").new_source(panel_id, name, url, query_x, query_y)?; + let panels = self.panels.read().expect("Panels RwLock poisoned"); for panel in &*panels { if panel.id == panel_id { - panel.sources.write().unwrap().push(source); + panel.sources.write().expect("Sources RwLock poisoned").push(source); return Ok(()); } } @@ -104,26 +108,19 @@ pub struct Source { impl Source { pub fn valid(&self) -> bool { - let last_fetch = self.last_fetch.read().unwrap(); + let last_fetch = self.last_fetch.read().expect("LastFetch RwLock poisoned"); return (Utc::now() - *last_fetch).num_seconds() < self.interval as i64; } pub fn values(&self) -> Values { - Values::from_values(self.data.read().unwrap().clone()) + Values::from_values(self.data.read().expect("Values RwLock poisoned").clone()) } pub fn values_filter(&self, min_x:f64) -> Values { - let mut values = self.data.read().unwrap().clone(); + let mut values = self.data.read().expect("Values RwLock poisoned").clone(); values.retain(|x| x.x > min_x); Values::from_values(values) } - - // Not really useful since different data has different fetch rates - // pub fn values_limit(&self, size:usize) -> Values { - // let values = self.data.read().unwrap().clone(); - // let min = if values.len() < size { 0 } else { values.len() - size }; - // Values::from_values(values[min..values.len()].to_vec()) - // } } pub fn fetch(url:&str, query_x:&str, query_y:&str) -> Result { diff --git a/src/app/mod.rs b/src/app/mod.rs index f966116..d933f57 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -3,20 +3,13 @@ pub mod worker; pub mod util; use std::sync::Arc; -use chrono::{DateTime, NaiveDateTime, Utc}; +use chrono::Utc; use eframe::egui; use eframe::egui::{plot::{Line, Plot}}; use self::data::ApplicationState; use self::worker::native_save; - -fn timestamp_to_str(t:i64) -> String { - format!( - "{}", - DateTime::::from_utc(NaiveDateTime::from_timestamp(t, 0), Utc) - .format("%Y/%m/%d %H:%M:%S") - ) -} +use self::util::{human_size, timestamp_to_str}; struct InputBuffer { panel_name: String, @@ -112,7 +105,9 @@ impl eframe::App for App { }); egui::TopBottomPanel::bottom("footer").show(ctx, |ui| { ui.horizontal(|ui|{ - ui.label(self.data.file.to_str().unwrap()); + ui.label(self.data.file_path.to_str().unwrap()); + ui.separator(); + ui.label(human_size(*self.data.file_size.read().unwrap())); ui.with_layout(egui::Layout::top_down(egui::Align::RIGHT), |ui| { ui.horizontal(|ui| { ui.label(format!("v{}-{}", env!("CARGO_PKG_VERSION"), git_version::git_version!())); diff --git a/src/app/util.rs b/src/app/util.rs new file mode 100644 index 0000000..4757afe --- /dev/null +++ b/src/app/util.rs @@ -0,0 +1,23 @@ +// if you're handling more than terabytes of data, it's the future and you ought to update this code! +use chrono::{DateTime, Utc, NaiveDateTime}; + +const PREFIXES: &'static [&'static str] = &["", "k", "M", "G", "T"]; + +pub fn human_size(size:u64) -> String { + let mut buf : f64 = size as f64; + let mut prefix : usize = 0; + while buf > 1024.0 && prefix < PREFIXES.len() -1 { + buf /= 1024.0; + prefix += 1; + } + + return format!("{:.3} {}B", buf, PREFIXES[prefix]); +} + +pub fn timestamp_to_str(t:i64) -> String { + format!( + "{}", + DateTime::::from_utc(NaiveDateTime::from_timestamp(t, 0), Utc) + .format("%Y/%m/%d %H:%M:%S") + ) +} \ No newline at end of file diff --git a/src/app/worker.rs b/src/app/worker.rs index dbed56f..c73af3d 100644 --- a/src/app/worker.rs +++ b/src/app/worker.rs @@ -79,6 +79,9 @@ impl BackgroundWorker for NativeBackgroundWorker { } } + let mut fsize = state.file_size.write().expect("File Size RwLock poisoned"); + *fsize = std::fs::metadata(state.file_path.clone()).unwrap().len(); + ctx.request_repaint(); } });