Hourly error stats
This commit is contained in:
parent
f2a92a5ef9
commit
633a493ab1
1 changed files with 41 additions and 16 deletions
57
src/main.rs
57
src/main.rs
|
|
@ -171,7 +171,7 @@ fn init_logger(
|
|||
))
|
||||
})
|
||||
.level(log_level)
|
||||
.chain(fern::log_file(path).unwrap())
|
||||
.chain(fern::log_file(path).expect("Failed to setup app_dispatch!"))
|
||||
});
|
||||
|
||||
let stat_dispatch = stat_log_path.map(|path| {
|
||||
|
|
@ -188,7 +188,7 @@ fn init_logger(
|
|||
.filter(|metadata| {
|
||||
metadata.level() == Level::Info && metadata.target().contains("stats10min")
|
||||
})
|
||||
.chain(fern::log_file(path).unwrap())
|
||||
.chain(fern::log_file(path).expect("Failed to setup stat_dispatch!"))
|
||||
});
|
||||
|
||||
let stdout_dispatch = if app_dispatch.is_none() || force_stdout {
|
||||
|
|
@ -347,30 +347,37 @@ fn main() {
|
|||
info!("Ctrl-C pressed! Initiating shutdown...");
|
||||
r.store(false, Ordering::SeqCst);
|
||||
})
|
||||
.unwrap();
|
||||
.expect("Failed to setup Ctrl-C handler!");
|
||||
debug!("Press Ctrl-C to stop");
|
||||
|
||||
let mut prev_stats: Option<StatsOnDetect> = None;
|
||||
let mut err_counter: u32 = 0;
|
||||
let mut err_cnt: u32 = 0;
|
||||
let mut success_cnt: u32 = 0;
|
||||
let samples_needed = args.wavs_path.is_some() || args.plot_data;
|
||||
let mut needed_retry = false;
|
||||
let mut last_hour_value = Local::now().hour();
|
||||
|
||||
while running.load(Ordering::SeqCst) {
|
||||
// update hailsens datetime once in an hour
|
||||
// update hailsens datetime once in an hour and print debug stats
|
||||
if last_hour_value != Local::now().hour() {
|
||||
sync_datetime(&mut port, args.device_id);
|
||||
last_hour_value = Local::now().hour();
|
||||
print_error_stats(err_cnt, success_cnt);
|
||||
err_cnt = 0;
|
||||
success_cnt = 0;
|
||||
}
|
||||
match get_stats(&mut port, args.device_id) {
|
||||
StatsOptions::None => {
|
||||
error!("Couldn't read adcstats!");
|
||||
handle_error_with_timeout(&mut err_counter);
|
||||
handle_error_with_timeout(&mut err_cnt, args.request_rate);
|
||||
continue;
|
||||
}
|
||||
StatsOptions::Stats(val) => info!(target: "stats10min", "{val}"),
|
||||
StatsOptions::Stats(val) => {
|
||||
success_cnt = success_cnt.saturating_add(1);
|
||||
info!(target: "stats10min", "{val}");
|
||||
},
|
||||
StatsOptions::DetectedNew(cur_stats) => {
|
||||
err_counter = err_counter.saturating_sub(1);
|
||||
success_cnt = success_cnt.saturating_add(1);
|
||||
if prev_stats.map_or(true, |v| v != cur_stats) {
|
||||
match prev_stats {
|
||||
Some(prev_stats) => {
|
||||
|
|
@ -445,12 +452,12 @@ fn main() {
|
|||
);
|
||||
}
|
||||
|
||||
err_counter = err_counter.saturating_sub(1);
|
||||
success_cnt = success_cnt.saturating_add(1);
|
||||
needed_retry = false;
|
||||
}
|
||||
_ => {
|
||||
error!("Couldn't receive new data!");
|
||||
handle_error_with_timeout(&mut err_counter);
|
||||
handle_error_with_timeout(&mut err_cnt, args.request_rate);
|
||||
needed_retry = true;
|
||||
}
|
||||
}
|
||||
|
|
@ -466,13 +473,31 @@ fn main() {
|
|||
}
|
||||
}
|
||||
|
||||
fn handle_error_with_timeout(err_counter: &mut u32) {
|
||||
*err_counter += 5;
|
||||
if *err_counter > 100 {
|
||||
panic!("Too many error! Exiting...")
|
||||
fn print_error_stats(error_counter: u32, success_counter: u32) {
|
||||
info!("{error_counter} errors and {success_counter} successful transfers in the last hour");
|
||||
match (success_counter == 0, error_counter == 0) {
|
||||
(true, true) => {
|
||||
warn!("No transfers in the last hour!");
|
||||
},
|
||||
(true, false) => {
|
||||
warn!("No successful transfers in the last hour!");
|
||||
},
|
||||
(false, true) => {
|
||||
info!("No errors in the last hour!");
|
||||
},
|
||||
(false, false) => {
|
||||
let transfers = success_counter + error_counter;
|
||||
let err_ratio = (error_counter as f64 / transfers as f64) * 100.0;
|
||||
warn!("Error ratio in the last hour is: {:.2}%", err_ratio);
|
||||
}
|
||||
}
|
||||
error!("Trying again in 100 ms...");
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
}
|
||||
|
||||
fn handle_error_with_timeout(err_counter: &mut u32, request_rate: u32) {
|
||||
*err_counter = err_counter.saturating_add(1);
|
||||
let timeout = (1000 / request_rate) + 200;
|
||||
error!("Trying again in {timeout} ms...");
|
||||
std::thread::sleep(Duration::from_millis(timeout as u64));
|
||||
}
|
||||
|
||||
fn get_stats(port: &mut Box<dyn SerialPort>, id: u32) -> StatsOptions {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue