Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "log_viewer"
version = "0.3.5"
version = "0.3.6"
edition = "2021"

[dependencies]
Expand All @@ -16,6 +16,7 @@ egui = "0.30"
egui_extras = "0.30"
futures = "0.3.31"
rfd = { version = "0.15.2", default-features = false, features = ["gtk3", "tokio"] }
ron = "0.8.1"
serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.135"
tracing = "0.1.41"
Expand Down
34 changes: 31 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{
path::PathBuf,
sync::{Arc, LazyLock, Mutex},
};
use tracing::info;
use tracing::{debug, error, info};

mod data;
mod data_display_options;
Expand All @@ -39,6 +39,8 @@ pub struct LogViewerApp {
should_scroll: bool,
#[serde(skip)]
loading_status: LoadingStatus,
#[serde(skip)]
last_save_hash: Option<u64>,
}

impl Default for LogViewerApp {
Expand All @@ -56,6 +58,7 @@ impl Default for LogViewerApp {
should_focus_search: Default::default(),
should_scroll: Default::default(),
show_last_filename: true,
last_save_hash: Default::default(),
}
}
}
Expand Down Expand Up @@ -678,6 +681,7 @@ impl LogViewerApp {
(false, _, total_len) => as_string_with_separators(total_len),
};
ui.label(format!("# Rows: {row_count_text}"));
ui.label(format!("Size: {}", data.file_size));
}
});
}
Expand All @@ -701,6 +705,26 @@ impl LogViewerApp {
}
}
}

fn is_changed_since_last_save(&mut self) -> bool {
let as_ron = match ron::to_string(&self) {
Ok(s) => s,
Err(err_msg) => {
error!(?err_msg, "failed to serialize app data");
return true;
}
};
let mut hasher = DefaultHasher::new();
as_ron.hash(&mut hasher);
let new_hash = hasher.finish();
if let Some(&old_hash) = self.last_save_hash.as_ref() {
if old_hash == new_hash {
return false;
}
}
self.last_save_hash = Some(new_hash);
true
}
}

#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -748,8 +772,12 @@ fn execute<F: std::future::Future<Output = Box<LoadingStatus>> + 'static>(
impl eframe::App for LogViewerApp {
/// Called by the frame work to save state before shutdown.
fn save(&mut self, storage: &mut dyn eframe::Storage) {
info!("Saving data");
eframe::set_value(storage, eframe::APP_KEY, self);
if self.is_changed_since_last_save() {
info!("Saving data");
eframe::set_value(storage, eframe::APP_KEY, self);
} else {
debug!("Save skipped, no change detected");
}
}

/// Called each time the UI needs repainting, which may be many times per second.
Expand Down
15 changes: 13 additions & 2 deletions src/app/data.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
calculate_hash,
data_display_options::{DataDisplayOptions, LevelConversion, RowParseErrorHandling},
data_display_options::{DataDisplayOptions, LevelConversion, RowParseErrorHandling, SizeUnits},
};
use anyhow::Context;
use data_iter::DataIter;
Expand All @@ -25,6 +25,7 @@ pub struct Data {
rows: Vec<LogRow>,
filtered_rows: Option<Vec<usize>>,
applied_filter: Option<FilterConfig>,
pub file_size: String,
}

#[derive(serde::Deserialize, serde::Serialize, Default, Debug, PartialEq, Eq, Clone)]
Expand Down Expand Up @@ -451,7 +452,17 @@ impl TryFrom<(&DataDisplayOptions, &str)> for Data {
fn try_from(
(data_display_options, value): (&DataDisplayOptions, &str),
) -> Result<Self, Self::Error> {
let mut result = Data::default();
let file_size = SizeUnits::Auto.convert(value.len());
let file_size = file_size
.as_str()
.map(|x| x.to_string())
.unwrap_or_else(|| file_size.to_string())
.trim_matches('0')
.to_string();
let mut result = Data {
file_size,
..Default::default()
};
for (i, line) in value.lines().enumerate() {
let row = LogRow::try_from((data_display_options, i, line))
.with_context(|| format!("failed to parse line {}", i + 1))?;
Expand Down
27 changes: 24 additions & 3 deletions src/app/data_display_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ pub struct RowSizeConfig {
#[derive(Default, serde::Deserialize, serde::Serialize, Debug, PartialEq, Eq)]
pub enum SizeUnits {
Bytes,
#[default]
KB,
MB,
GB,
TB,
// TODO 5: Add an auto option to use smallest non fractional (would need to use str instead of f32)
#[default]
/// Is output as a String because it includes the unit and is the largest unit where the output value is >= 1
Auto,
}
impl SizeUnits {
pub(crate) fn convert(&self, row_size_in_bytes: usize) -> serde_json::Value {
Expand All @@ -83,6 +84,26 @@ impl SizeUnits {
SizeUnits::MB => 1024.0 * 1024.0,
SizeUnits::GB => 1024.0 * 1024.0 * 1024.0,
SizeUnits::TB => 1024.0 * 1024.0 * 1024.0 * 1024.0,
SizeUnits::Auto => {
// TODO 5: Rewrite with shifts for performance
// Special handling see doc string for explanation
let units = ["Bytes", "KB", "MB", "GB", "TB"];
let mut last_index = 0;
let mut scalar = 1.0f64;
let row_size_in_bytes = row_size_in_bytes as f64;
for i in 1..units.len() {
let new_scalar = scalar * 1024.0;
if (row_size_in_bytes / new_scalar) >= 1.0 {
last_index = i;
scalar = new_scalar;
} else {
// Last was as correct unit
break;
}
}
let result = row_size_in_bytes / scalar;
return format!("{result:0>9.4} {}", units[last_index]).into();
}
};
let result = row_size_in_bytes as f64 / scalar;
result.into()
Expand Down Expand Up @@ -147,7 +168,7 @@ impl Default for DataDisplayOptions {
row_idx_field_name: Some("row#".to_string()),
row_size_config: Some(RowSizeConfig {
field_name: "row_size".to_string(),
units: SizeUnits::Bytes,
units: SizeUnits::Auto,
}),
row_parse_error_handling: Default::default(),
level_conversion: Some(Default::default()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ applied_filter:
filter_on: Any
is_case_sensitive: false
comparator: LessThanEqual
file_size: ""
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ applied_filter:
filter_on: Any
is_case_sensitive: false
comparator: Equal
file_size: ""
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ applied_filter:
filter_on: Any
is_case_sensitive: false
comparator: GreaterThan
file_size: ""
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ applied_filter:
filter_on: Any
is_case_sensitive: false
comparator: GreaterThanEqual
file_size: ""
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ applied_filter:
filter_on: Any
is_case_sensitive: false
comparator: NotEqual
file_size: ""
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ applied_filter:
filter_on: Any
is_case_sensitive: false
comparator: Contains
file_size: ""
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ applied_filter:
filter_on: Any
is_case_sensitive: false
comparator: NotContains
file_size: ""
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ applied_filter:
filter_on: Any
is_case_sensitive: false
comparator: LessThan
file_size: ""
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ applied_filter:
name: http.status_code
is_case_sensitive: false
comparator: LessThanEqual
file_size: ""
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ applied_filter:
name: http.status_code
is_case_sensitive: false
comparator: Equal
file_size: ""
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ applied_filter:
name: http.status_code
is_case_sensitive: false
comparator: GreaterThan
file_size: ""
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ applied_filter:
name: http.status_code
is_case_sensitive: false
comparator: GreaterThanEqual
file_size: ""
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ applied_filter:
name: http.status_code
is_case_sensitive: false
comparator: NotEqual
file_size: ""
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ applied_filter:
name: http.status_code
is_case_sensitive: false
comparator: Contains
file_size: ""
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ applied_filter:
name: http.status_code
is_case_sensitive: false
comparator: NotContains
file_size: ""
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ applied_filter:
name: http.status_code
is_case_sensitive: false
comparator: LessThan
file_size: ""
Loading
Loading