fix: sort containers on every update_stats(), if a sort is set

This commit is contained in:
Jack Wills
2023-01-03 04:44:47 +00:00
parent 3cdc5fae02
commit cfdea77594
2 changed files with 56 additions and 28 deletions
+41 -16
View File
@@ -56,21 +56,6 @@ impl fmt::Display for Header {
} }
impl AppData { impl AppData {
pub const fn get_sorted(&self) -> Option<(Header, SortedOrder)> {
self.sorted_by
}
/// Change the sorted order, also set the selected container state to match new order
pub fn set_sorted(&mut self, x: Option<(Header, SortedOrder)>) {
self.sorted_by = x;
self.sort_containers();
self.containers
.state
.select(self.containers.items.iter().position(|i| {
self.get_selected_container_id()
.map_or(false, |id| i.id == id)
}));
}
/// Generate a default app_state /// Generate a default app_state
pub fn default(args: CliArgs) -> Self { pub fn default(args: CliArgs) -> Self {
Self { Self {
@@ -82,6 +67,41 @@ impl AppData {
} }
} }
pub const fn get_sorted(&self) -> Option<(Header, SortedOrder)> {
self.sorted_by
}
/// Remove the sorted header & order, and sort by default - created datetime
pub fn reset_sorted(&mut self) {
self.set_sorted(None);
}
/// Sort containers based on a given header, if headings match, and already ascending, remove sorting
pub fn set_sort_by_header(&mut self, selected_header: Header) {
let mut output = Some((selected_header, SortedOrder::Asc));
if let Some((current_header, order)) = self.get_sorted() {
if current_header == selected_header {
match order {
SortedOrder::Desc => output = None,
SortedOrder::Asc => output = Some((selected_header, SortedOrder::Desc)),
}
}
}
self.set_sorted(output);
}
/// Change the sorted order, also set the selected container state to match new order
fn set_sorted(&mut self, x: Option<(Header, SortedOrder)>) {
self.sorted_by = x;
self.sort_containers();
self.containers
.state
.select(self.containers.items.iter().position(|i| {
self.get_selected_container_id()
.map_or(false, |id| i.id == id)
}));
}
/// Current time as unix timestamp /// Current time as unix timestamp
#[allow(clippy::expect_used)] #[allow(clippy::expect_used)]
fn get_systemtime() -> u64 { fn get_systemtime() -> u64 {
@@ -185,7 +205,7 @@ impl AppData {
/// Sort the containers vec, based on a heading, either ascending or descending, /// Sort the containers vec, based on a heading, either ascending or descending,
/// If not sort set, then sort by created time /// If not sort set, then sort by created time
fn sort_containers(&mut self) { pub fn sort_containers(&mut self) {
if let Some((head, ord)) = self.sorted_by { if let Some((head, ord)) = self.sorted_by {
match head { match head {
Header::State => match ord { Header::State => match ord {
@@ -411,6 +431,7 @@ impl AppData {
} }
/// Update container mem, cpu, & network stats, in single function so only need to call .lock() once /// Update container mem, cpu, & network stats, in single function so only need to call .lock() once
/// Will also, if a sort is set, sort the containers
pub fn update_stats( pub fn update_stats(
&mut self, &mut self,
id: &ContainerId, id: &ContainerId,
@@ -439,6 +460,10 @@ impl AppData {
container.tx.update(tx); container.tx.update(tx);
container.mem_limit.update(mem_limit); container.mem_limit.update(mem_limit);
} }
// need to benchmark this?
if self.get_sorted().is_some() {
self.sort_containers();
}
} }
/// Update, or insert, containers /// Update, or insert, containers
+15 -12
View File
@@ -156,18 +156,20 @@ impl DockerData {
let docker = Arc::clone(&self.docker); let docker = Arc::clone(&self.docker);
let app_data = Arc::clone(&self.app_data); let app_data = Arc::clone(&self.app_data);
let spawns = Arc::clone(&self.spawns); let spawns = Arc::clone(&self.spawns);
let key = SpawnId::Stats((id.clone(), self.binate)); let spawn_key = SpawnId::Stats((id.clone(), self.binate));
let spawn_key = key.clone(); self.spawns
self.spawns.lock().entry(key).or_insert_with(|| { .lock()
tokio::spawn(Self::update_container_stat( .entry(spawn_key.clone())
docker, .or_insert_with(|| {
id.clone(), tokio::spawn(Self::update_container_stat(
app_data, docker,
*is_running, id.clone(),
spawns, app_data,
spawn_key, *is_running,
)) spawns,
}); spawn_key,
))
});
} }
self.binate = self.binate.toggle(); self.binate = self.binate.toggle();
} }
@@ -299,6 +301,7 @@ impl DockerData {
} }
}; };
self.update_all_container_stats(&all_ids); self.update_all_container_stats(&all_ids);
self.app_data.lock().sort_containers();
} }
/// Animate the loading icon /// Animate the loading icon