refactor: sort_containers
This commit is contained in:
+75
-82
@@ -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 => {
|
.cpu_stats
|
||||||
a.status.cmp(&b.status).then_with(|| a.name.cmp(&b.name))
|
.back()
|
||||||
}
|
.cmp(&item_ord.1.cpu_stats.back())
|
||||||
SortedOrder::Desc => {
|
.then_with(|| item_ord.0.name.get().cmp(&item_ord.1.name.get())),
|
||||||
b.status.cmp(&a.status).then_with(|| b.name.cmp(&a.name))
|
Header::Memory => item_ord
|
||||||
}
|
.0
|
||||||
},
|
.mem_stats
|
||||||
Header::Cpu => match ord {
|
.back()
|
||||||
SortedOrder::Asc => a
|
.cmp(&item_ord.1.mem_stats.back())
|
||||||
.cpu_stats
|
.then_with(|| item_ord.0.name.get().cmp(&item_ord.1.name.get())),
|
||||||
.back()
|
|
||||||
.cmp(&b.cpu_stats.back())
|
Header::Id => item_ord
|
||||||
.then_with(|| a.name.cmp(&b.name)),
|
.0
|
||||||
SortedOrder::Desc => b
|
.id
|
||||||
.cpu_stats
|
.cmp(&item_ord.1.id)
|
||||||
.back()
|
.then_with(|| item_ord.0.name.get().cmp(&item_ord.1.name.get())),
|
||||||
.cmp(&a.cpu_stats.back())
|
Header::Image => item_ord
|
||||||
.then_with(|| b.name.cmp(&a.name)),
|
.0
|
||||||
},
|
.image
|
||||||
Header::Memory => match ord {
|
.get()
|
||||||
SortedOrder::Asc => a
|
.cmp(&item_ord.1.image.get())
|
||||||
.mem_stats
|
.then_with(|| item_ord.0.name.get().cmp(&item_ord.1.name.get())),
|
||||||
.back()
|
Header::Rx => item_ord
|
||||||
.cmp(&b.mem_stats.back())
|
.0
|
||||||
.then_with(|| a.name.cmp(&b.name)),
|
.rx
|
||||||
SortedOrder::Desc => b
|
.cmp(&item_ord.1.rx)
|
||||||
.mem_stats
|
.then_with(|| item_ord.0.name.get().cmp(&item_ord.1.name.get())),
|
||||||
.back()
|
Header::Tx => item_ord
|
||||||
.cmp(&a.mem_stats.back())
|
.0
|
||||||
.then_with(|| b.name.cmp(&a.name)),
|
.tx
|
||||||
},
|
.cmp(&item_ord.1.tx)
|
||||||
Header::Id => match ord {
|
.then_with(|| item_ord.0.name.get().cmp(&item_ord.1.name.get())),
|
||||||
SortedOrder::Asc => a.id.cmp(&b.id).then_with(|| a.name.cmp(&b.name)),
|
|
||||||
SortedOrder::Desc => b.id.cmp(&a.id).then_with(|| b.name.cmp(&a.name)),
|
Header::Name => item_ord
|
||||||
},
|
.0
|
||||||
Header::Image => match ord {
|
.name
|
||||||
SortedOrder::Asc => a.image.cmp(&b.image).then_with(|| a.name.cmp(&b.name)),
|
.get()
|
||||||
SortedOrder::Desc => {
|
.cmp(&item_ord.1.name.get())
|
||||||
b.image.cmp(&a.image).then_with(|| b.name.cmp(&a.name))
|
.then_with(|| item_ord.0.id.cmp(&item_ord.1.id)),
|
||||||
}
|
|
||||||
},
|
|
||||||
Header::Name => match ord {
|
|
||||||
SortedOrder::Asc => a.name.cmp(&b.name).then_with(|| a.id.cmp(&b.id)),
|
|
||||||
SortedOrder::Desc => b.name.cmp(&a.name).then_with(|| b.id.cmp(&a.id)),
|
|
||||||
},
|
|
||||||
Header::Rx => match ord {
|
|
||||||
SortedOrder::Asc => a.rx.cmp(&b.rx).then_with(|| a.name.cmp(&b.name)),
|
|
||||||
SortedOrder::Desc => b.rx.cmp(&a.rx).then_with(|| b.name.cmp(&a.name)),
|
|
||||||
},
|
|
||||||
Header::Tx => match ord {
|
|
||||||
SortedOrder::Asc => a.tx.cmp(&b.tx).then_with(|| a.name.cmp(&b.name)),
|
|
||||||
SortedOrder::Desc => b.tx.cmp(&a.tx).then_with(|| b.name.cmp(&a.name)),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user