Skip to content

Commit

Permalink
Merge pull request #7 from rafaelrcamargo/refactor/instant-based
Browse files Browse the repository at this point in the history
Refactor: instant-based container purging
  • Loading branch information
rafaelrcamargo authored Oct 2, 2023
2 parents d2fe88c + e0c741f commit 28d8ff8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 44 deletions.
32 changes: 0 additions & 32 deletions a.sh

This file was deleted.

Empty file removed docs/notes.md
Empty file.
61 changes: 49 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ use colored::Colorize;
use data::DockerStats;
use std::{
io::{BufRead, BufReader},
process::{Command, Stdio}
process::{Command, Stdio},
sync::{
mpsc::{self, Receiver},
Arc, Mutex
},
thread,
time::Instant
};
use utils::*;

Expand All @@ -19,9 +25,14 @@ fn main() {
cli::has_arg(&matches, "full")
);

let mut containers: Vec<DockerStats> = Vec::new();
let containers: Arc<Mutex<Vec<DockerStats>>> = Arc::new(Mutex::new(Vec::new()));
let width = get_terminal_width();

let (sender, receiver) = mpsc::channel::<()>();

let t_containers = containers.clone();
let purger = thread::spawn(move || purge(receiver, t_containers, compact, full, width));

let mut cmd = Command::new("docker")
.args(build_command(matches))
.stdout(Stdio::piped())
Expand All @@ -40,30 +51,56 @@ fn main() {
println!("Fetching container stats...");

for line in stdout_lines {
print!("\x1B[2J\x1B[1;1H"); // Clear screen
let mut line = line.unwrap();
// dbg!(line.clone());

let line = line.unwrap();
dbg!(line.clone());
sender.send(()).unwrap();

if line.starts_with("\u{1b}[2J\u{1b}[H") && !containers.is_empty() {
let containers = containers.clone();
if line.starts_with("\u{1b}[2J\u{1b}[H") {
// println!("{}", "\x1B[u"); // Restore cursor position
print(&containers, compact, full, width); // Print the charts
containers.clear(); // Reset the containers
}
print(containers.clone(), compact, full, width); // Print the charts
containers.lock().unwrap().clear(); // Reset the containers

let line = line.replace("\u{1b}[2J\u{1b}[H", "");
line = line.replace("\u{1b}[2J\u{1b}[H", "")
}

let stats: DockerStats = serde_json::from_str(&line).unwrap();
containers.push(stats);
containers.lock().unwrap().push(stats);
}

purger.join().unwrap();
let status = cmd.wait();
println!("Exited with status {:?}", status);
}

fn print(containers: &Vec<DockerStats>, compact: bool, full: bool, width: usize) {
fn purge(receiver: Receiver<()>, containers: Arc<Mutex<Vec<DockerStats>>>, compact: bool, full: bool, width: usize) {
let mut last_message: Instant = Instant::now();

loop {
if receiver.try_recv().is_ok() {
last_message = Instant::now()
}

if last_message.elapsed().as_secs() > 2 {
containers.lock().unwrap().clear(); // Clear the containers list
print(containers.clone(), compact, full, width); // Print the charts
last_message = Instant::now()
}
}
}

fn print(containers: Arc<Mutex<Vec<DockerStats>>>, compact: bool, full: bool, width: usize) {
print!("\x1B[2J\x1B[1;1H"); // Clear screen

let containers = containers.lock().unwrap();
let mut max = 100f32;

if containers.len() == 0 {
println!("Waiting for container stats...");
return;
}

for (i, stats) in containers.iter().enumerate() {
// LAYOUT
{
Expand Down

0 comments on commit 28d8ff8

Please sign in to comment.