Skip to content

Commit 9d4b90b

Browse files
authored
Merge pull request #9 from c-git/develop
0.3.6
2 parents 0645cd8 + 97a54eb commit 9d4b90b

27 files changed

+342
-259
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "log_viewer"
3-
version = "0.3.5"
3+
version = "0.3.6"
44
edition = "2021"
55

66
[dependencies]
@@ -16,6 +16,7 @@ egui = "0.30"
1616
egui_extras = "0.30"
1717
futures = "0.3.31"
1818
rfd = { version = "0.15.2", default-features = false, features = ["gtk3", "tokio"] }
19+
ron = "0.8.1"
1920
serde = { version = "1.0.217", features = ["derive"] }
2021
serde_json = "1.0.135"
2122
tracing = "0.1.41"

src/app.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::{
1313
path::PathBuf,
1414
sync::{Arc, LazyLock, Mutex},
1515
};
16-
use tracing::info;
16+
use tracing::{debug, error, info};
1717

1818
mod data;
1919
mod data_display_options;
@@ -39,6 +39,8 @@ pub struct LogViewerApp {
3939
should_scroll: bool,
4040
#[serde(skip)]
4141
loading_status: LoadingStatus,
42+
#[serde(skip)]
43+
last_save_hash: Option<u64>,
4244
}
4345

4446
impl Default for LogViewerApp {
@@ -56,6 +58,7 @@ impl Default for LogViewerApp {
5658
should_focus_search: Default::default(),
5759
should_scroll: Default::default(),
5860
show_last_filename: true,
61+
last_save_hash: Default::default(),
5962
}
6063
}
6164
}
@@ -678,6 +681,7 @@ impl LogViewerApp {
678681
(false, _, total_len) => as_string_with_separators(total_len),
679682
};
680683
ui.label(format!("# Rows: {row_count_text}"));
684+
ui.label(format!("Size: {}", data.file_size));
681685
}
682686
});
683687
}
@@ -701,6 +705,26 @@ impl LogViewerApp {
701705
}
702706
}
703707
}
708+
709+
fn is_changed_since_last_save(&mut self) -> bool {
710+
let as_ron = match ron::to_string(&self) {
711+
Ok(s) => s,
712+
Err(err_msg) => {
713+
error!(?err_msg, "failed to serialize app data");
714+
return true;
715+
}
716+
};
717+
let mut hasher = DefaultHasher::new();
718+
as_ron.hash(&mut hasher);
719+
let new_hash = hasher.finish();
720+
if let Some(&old_hash) = self.last_save_hash.as_ref() {
721+
if old_hash == new_hash {
722+
return false;
723+
}
724+
}
725+
self.last_save_hash = Some(new_hash);
726+
true
727+
}
704728
}
705729

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

755783
/// Called each time the UI needs repainting, which may be many times per second.

src/app/data.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{
22
calculate_hash,
3-
data_display_options::{DataDisplayOptions, LevelConversion, RowParseErrorHandling},
3+
data_display_options::{DataDisplayOptions, LevelConversion, RowParseErrorHandling, SizeUnits},
44
};
55
use anyhow::Context;
66
use data_iter::DataIter;
@@ -25,6 +25,7 @@ pub struct Data {
2525
rows: Vec<LogRow>,
2626
filtered_rows: Option<Vec<usize>>,
2727
applied_filter: Option<FilterConfig>,
28+
pub file_size: String,
2829
}
2930

3031
#[derive(serde::Deserialize, serde::Serialize, Default, Debug, PartialEq, Eq, Clone)]
@@ -451,7 +452,17 @@ impl TryFrom<(&DataDisplayOptions, &str)> for Data {
451452
fn try_from(
452453
(data_display_options, value): (&DataDisplayOptions, &str),
453454
) -> Result<Self, Self::Error> {
454-
let mut result = Data::default();
455+
let file_size = SizeUnits::Auto.convert(value.len());
456+
let file_size = file_size
457+
.as_str()
458+
.map(|x| x.to_string())
459+
.unwrap_or_else(|| file_size.to_string())
460+
.trim_matches('0')
461+
.to_string();
462+
let mut result = Data {
463+
file_size,
464+
..Default::default()
465+
};
455466
for (i, line) in value.lines().enumerate() {
456467
let row = LogRow::try_from((data_display_options, i, line))
457468
.with_context(|| format!("failed to parse line {}", i + 1))?;

src/app/data_display_options.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,13 @@ pub struct RowSizeConfig {
6868
#[derive(Default, serde::Deserialize, serde::Serialize, Debug, PartialEq, Eq)]
6969
pub enum SizeUnits {
7070
Bytes,
71-
#[default]
7271
KB,
7372
MB,
7473
GB,
7574
TB,
76-
// TODO 5: Add an auto option to use smallest non fractional (would need to use str instead of f32)
75+
#[default]
76+
/// Is output as a String because it includes the unit and is the largest unit where the output value is >= 1
77+
Auto,
7778
}
7879
impl SizeUnits {
7980
pub(crate) fn convert(&self, row_size_in_bytes: usize) -> serde_json::Value {
@@ -83,6 +84,26 @@ impl SizeUnits {
8384
SizeUnits::MB => 1024.0 * 1024.0,
8485
SizeUnits::GB => 1024.0 * 1024.0 * 1024.0,
8586
SizeUnits::TB => 1024.0 * 1024.0 * 1024.0 * 1024.0,
87+
SizeUnits::Auto => {
88+
// TODO 5: Rewrite with shifts for performance
89+
// Special handling see doc string for explanation
90+
let units = ["Bytes", "KB", "MB", "GB", "TB"];
91+
let mut last_index = 0;
92+
let mut scalar = 1.0f64;
93+
let row_size_in_bytes = row_size_in_bytes as f64;
94+
for i in 1..units.len() {
95+
let new_scalar = scalar * 1024.0;
96+
if (row_size_in_bytes / new_scalar) >= 1.0 {
97+
last_index = i;
98+
scalar = new_scalar;
99+
} else {
100+
// Last was as correct unit
101+
break;
102+
}
103+
}
104+
let result = row_size_in_bytes / scalar;
105+
return format!("{result:0>9.4} {}", units[last_index]).into();
106+
}
86107
};
87108
let result = row_size_in_bytes as f64 / scalar;
88109
result.into()
@@ -147,7 +168,7 @@ impl Default for DataDisplayOptions {
147168
row_idx_field_name: Some("row#".to_string()),
148169
row_size_config: Some(RowSizeConfig {
149170
field_name: "row_size".to_string(),
150-
units: SizeUnits::Bytes,
171+
units: SizeUnits::Auto,
151172
}),
152173
row_parse_error_handling: Default::default(),
153174
level_conversion: Some(Default::default()),

tests/snapshots/log_viewer__app__data__tests__comparisons_any-2.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ applied_filter:
2222
filter_on: Any
2323
is_case_sensitive: false
2424
comparator: LessThanEqual
25+
file_size: ""

tests/snapshots/log_viewer__app__data__tests__comparisons_any-3.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ applied_filter:
2222
filter_on: Any
2323
is_case_sensitive: false
2424
comparator: Equal
25+
file_size: ""

tests/snapshots/log_viewer__app__data__tests__comparisons_any-4.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ applied_filter:
2424
filter_on: Any
2525
is_case_sensitive: false
2626
comparator: GreaterThan
27+
file_size: ""

tests/snapshots/log_viewer__app__data__tests__comparisons_any-5.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ applied_filter:
2424
filter_on: Any
2525
is_case_sensitive: false
2626
comparator: GreaterThanEqual
27+
file_size: ""

tests/snapshots/log_viewer__app__data__tests__comparisons_any-6.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ applied_filter:
2424
filter_on: Any
2525
is_case_sensitive: false
2626
comparator: NotEqual
27+
file_size: ""

0 commit comments

Comments
 (0)