use crossbeam::channel::{bounded, Receiver};
use std::sync::atomic::Ordering::Relaxed;
use std::{
sync::atomic::AtomicBool,
thread::{self, sleep},
time::Duration,
};
use crate::cli_pretty_printing::{countdown_until_program_ends, display_top_results};
use crate::config::get_config;
use crate::storage::wait_athena_storage;
static PAUSED: AtomicBool = AtomicBool::new(false);
pub fn start(duration: u32) -> Receiver<()> {
let (sender, recv) = bounded(1);
thread::spawn(move || {
let mut time_spent = 0;
while time_spent < duration {
if !PAUSED.load(Relaxed) {
sleep(Duration::from_secs(1));
time_spent += 1;
countdown_until_program_ends(time_spent, duration);
}
}
let config = get_config();
log::trace!("Timer expired. top_results mode: {}", config.top_results);
if config.top_results {
log::info!("Displaying all collected plaintext results");
filter_and_display_results();
} else {
log::info!("Not in top_results mode, skipping display_wait_athena_results()");
}
sender.send(()).expect("Timer should send succesfully");
});
recv
}
fn filter_and_display_results() {
let results = wait_athena_storage::get_plaintext_results();
log::trace!(
"Retrieved {} results from wait_athena_storage",
results.len()
);
display_top_results(&results);
}
pub fn pause() {
PAUSED.store(true, Relaxed);
}
pub fn resume() {
PAUSED.store(false, Relaxed);
}