refactor: sort_containers

This commit is contained in:
Jack Wills
2024-01-02 19:02:36 +00:00
parent 333621f1a7
commit ccf8b55a74
+69 -76
View File
@@ -161,80 +161,72 @@ impl AppData {
pub 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 {
let sort_closure = |a: &ContainerItem, b: &ContainerItem| -> std::cmp::Ordering { let sort_closure = |a: &ContainerItem, b: &ContainerItem| -> std::cmp::Ordering {
let item_ord = match ord {
SortedOrder::Asc => (a, b),
SortedOrder::Desc => (b, a),
};
match head { match head {
Header::State => match ord { Header::State => item_ord
SortedOrder::Asc => a .0
.state .state
.order() .order()
.cmp(&b.state.order()) .cmp(&item_ord.1.state.order())
.then_with(|| a.name.cmp(&b.name)), .then_with(|| item_ord.0.name.get().cmp(&item_ord.1.name.get())),
SortedOrder::Desc => b Header::Status => item_ord
.state .0
.order() .status
.cmp(&a.state.order()) .cmp(&item_ord.1.status)
.then_with(|| b.name.cmp(&a.name)), .then_with(|| item_ord.0.name.get().cmp(&item_ord.1.name.get())),
}, Header::Cpu => item_ord
Header::Status => match ord { .0
SortedOrder::Asc => {
a.status.cmp(&b.status).then_with(|| a.name.cmp(&b.name))
}
SortedOrder::Desc => {
b.status.cmp(&a.status).then_with(|| b.name.cmp(&a.name))
}
},
Header::Cpu => match ord {
SortedOrder::Asc => a
.cpu_stats .cpu_stats
.back() .back()
.cmp(&b.cpu_stats.back()) .cmp(&item_ord.1.cpu_stats.back())
.then_with(|| a.name.cmp(&b.name)), .then_with(|| item_ord.0.name.get().cmp(&item_ord.1.name.get())),
SortedOrder::Desc => b Header::Memory => item_ord
.cpu_stats .0
.back()
.cmp(&a.cpu_stats.back())
.then_with(|| b.name.cmp(&a.name)),
},
Header::Memory => match ord {
SortedOrder::Asc => a
.mem_stats .mem_stats
.back() .back()
.cmp(&b.mem_stats.back()) .cmp(&item_ord.1.mem_stats.back())
.then_with(|| a.name.cmp(&b.name)), .then_with(|| item_ord.0.name.get().cmp(&item_ord.1.name.get())),
SortedOrder::Desc => b
.mem_stats Header::Id => item_ord
.back() .0
.cmp(&a.mem_stats.back()) .id
.then_with(|| b.name.cmp(&a.name)), .cmp(&item_ord.1.id)
}, .then_with(|| item_ord.0.name.get().cmp(&item_ord.1.name.get())),
Header::Id => match ord { Header::Image => item_ord
SortedOrder::Asc => a.id.cmp(&b.id).then_with(|| a.name.cmp(&b.name)), .0
SortedOrder::Desc => b.id.cmp(&a.id).then_with(|| b.name.cmp(&a.name)), .image
}, .get()
Header::Image => match ord { .cmp(&item_ord.1.image.get())
SortedOrder::Asc => a.image.cmp(&b.image).then_with(|| a.name.cmp(&b.name)), .then_with(|| item_ord.0.name.get().cmp(&item_ord.1.name.get())),
SortedOrder::Desc => { Header::Rx => item_ord
b.image.cmp(&a.image).then_with(|| b.name.cmp(&a.name)) .0
} .rx
}, .cmp(&item_ord.1.rx)
Header::Name => match ord { .then_with(|| item_ord.0.name.get().cmp(&item_ord.1.name.get())),
SortedOrder::Asc => a.name.cmp(&b.name).then_with(|| a.id.cmp(&b.id)), Header::Tx => item_ord
SortedOrder::Desc => b.name.cmp(&a.name).then_with(|| b.id.cmp(&a.id)), .0
}, .tx
Header::Rx => match ord { .cmp(&item_ord.1.tx)
SortedOrder::Asc => a.rx.cmp(&b.rx).then_with(|| a.name.cmp(&b.name)), .then_with(|| item_ord.0.name.get().cmp(&item_ord.1.name.get())),
SortedOrder::Desc => b.rx.cmp(&a.rx).then_with(|| b.name.cmp(&a.name)),
}, Header::Name => item_ord
Header::Tx => match ord { .0
SortedOrder::Asc => a.tx.cmp(&b.tx).then_with(|| a.name.cmp(&b.name)), .name
SortedOrder::Desc => b.tx.cmp(&a.tx).then_with(|| b.name.cmp(&a.name)), .get()
}, .cmp(&item_ord.1.name.get())
.then_with(|| item_ord.0.id.cmp(&item_ord.1.id)),
} }
}; };
self.containers.items.sort_by(sort_closure); self.containers.items.sort_by(sort_closure);
} else { } else {
self.containers self.containers.items.sort_by(|a, b| {
.items a.created
.sort_by(|a, b| a.created.cmp(&b.created).then_with(|| a.name.cmp(&b.name))); .cmp(&b.created)
.then_with(|| a.name.get().cmp(&b.name.get()))
});
} }
} }
@@ -359,12 +351,12 @@ impl AppData {
pub fn get_log_title(&self) -> String { pub fn get_log_title(&self) -> String {
self.get_selected_container().map_or_else(String::new, |y| { self.get_selected_container().map_or_else(String::new, |y| {
let logs_len = y.logs.get_state_title(); let logs_len = y.logs.get_state_title();
let mut name = y.name.clone(); // let mut name = y.name.clone();
name.truncate(32); // name.truncate(32);
if logs_len.is_empty() { if logs_len.is_empty() {
format!("- {name} ") format!("- {} ", y.name)
} else { } else {
format!("{logs_len} - {name}") format!("{logs_len} - {}", y.name.get())
} }
}) })
} }
@@ -477,11 +469,12 @@ impl AppData {
.to_string(), .to_string(),
); );
// Issue here!
columns.cpu.1 = columns.cpu.1.max(cpu_count); columns.cpu.1 = columns.cpu.1.max(cpu_count);
columns.image.1 = columns.image.1.max(count(&container.image)); columns.image.1 = columns.image.1.max(count(&container.image.to_string()));
columns.mem.1 = columns.mem.1.max(mem_current_count); columns.mem.1 = columns.mem.1.max(mem_current_count);
columns.mem.2 = columns.mem.2.max(count(&container.mem_limit.to_string())); columns.mem.2 = columns.mem.2.max(count(&container.mem_limit.to_string()));
columns.name.1 = columns.name.1.max(count(&container.name)); columns.name.1 = columns.name.1.max(count(&container.name.to_string()));
columns.net_rx.1 = columns.net_rx.1.max(count(&container.rx.to_string())); columns.net_rx.1 = columns.net_rx.1.max(count(&container.rx.to_string()));
columns.net_tx.1 = columns.net_tx.1.max(count(&container.tx.to_string())); columns.net_tx.1 = columns.net_tx.1.max(count(&container.tx.to_string()));
columns.state.1 = columns.state.1.max(count(&container.state.to_string())); columns.state.1 = columns.state.1.max(count(&container.state.to_string()));
@@ -497,8 +490,8 @@ impl AppData {
self.containers.items.iter_mut().find(|i| &i.id == id) self.containers.items.iter_mut().find(|i| &i.id == id)
} }
/// return a mutable container by given id /// Get the ContainerName of by ID
pub fn get_container_name_by_id(&mut self, id: &ContainerId) -> Option<String> { pub fn get_container_name_by_id(&mut self, id: &ContainerId) -> Option<ContainerName> {
self.containers self.containers
.items .items
.iter_mut() .iter_mut()
@@ -516,7 +509,7 @@ impl AppData {
/// Get the Id and State for the currently selected container - used by the exec check method /// Get the Id and State for the currently selected container - used by the exec check method
pub fn get_selected_container_id_state_name(&self) -> Option<(ContainerId, State, String)> { pub fn get_selected_container_id_state_name(&self) -> Option<(ContainerId, State, String)> {
self.get_selected_container() self.get_selected_container()
.map(|i| (i.id.clone(), i.state, i.name.clone())) .map(|i| (i.id.clone(), i.state, i.name.get()))
} }
/// 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
@@ -623,8 +616,8 @@ impl AppData {
.map_or(0, |i| u64::try_from(i).unwrap_or_default()); .map_or(0, |i| u64::try_from(i).unwrap_or_default());
// If container info already in containers Vec, then just update details // If container info already in containers Vec, then just update details
if let Some(item) = self.get_container_by_id(&id) { if let Some(item) = self.get_container_by_id(&id) {
if item.name != name { if item.name.get() != name {
item.name = name; item.name.set(name);
}; };
if item.status != status { if item.status != status {
item.status = status; item.status = status;
@@ -640,8 +633,8 @@ impl AppData {
}; };
item.state = state; item.state = state;
}; };
if item.image != image { if item.image.get() != image {
item.image = image; item.image.set(image);
}; };
} else { } else {
// container not known, so make new ContainerItem and push into containers Vec // container not known, so make new ContainerItem and push into containers Vec