refactor: clone()'s removed, allow(precision_loss)

This commit is contained in:
Jack Wills
2022-10-02 01:14:42 +00:00
parent f5fc446295
commit 6bee4d007a
4 changed files with 38 additions and 36 deletions
+8 -2
View File
@@ -337,6 +337,8 @@ impl ByteStats {
self.value = value;
}
}
#[allow(clippy::cast_precision_loss)]
impl Stats for ByteStats {
fn get_value(&self) -> f64 {
self.value as f64
@@ -346,7 +348,7 @@ impl Stats for ByteStats {
/// convert from bytes to kB, MB, GB etc
impl fmt::Display for ByteStats {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let as_f64 = self.value as f64;
let as_f64 = self.get_value();
let p = match as_f64 {
x if x >= ONE_GB => format!("{y:.2} GB", y = as_f64 / ONE_GB),
x if x >= ONE_MB => format!("{y:.2} MB", y = as_f64 / ONE_MB),
@@ -383,13 +385,15 @@ impl ContainerItem {
pub fn new(id: ContainerId, status: String, image: String, state: State, name: String) -> Self {
let mut docker_controls = StatefulList::new(DockerControls::gen_vec(state));
docker_controls.start();
let mut logs = StatefulList::new(vec![]);
logs.end();
Self {
cpu_stats: VecDeque::with_capacity(60),
docker_controls,
id,
image,
last_updated: 0,
logs: StatefulList::new(vec![]),
logs,
mem_limit: ByteStats::default(),
mem_stats: VecDeque::with_capacity(60),
name,
@@ -417,6 +421,7 @@ impl ContainerItem {
}
/// Convert cpu stats into a vec for the charts function
#[allow(clippy::cast_precision_loss)]
fn get_cpu_dataset(&self) -> Vec<(f64, f64)> {
self.cpu_stats
.iter()
@@ -426,6 +431,7 @@ impl ContainerItem {
}
/// Convert mem stats into a Vec for the charts function
#[allow(clippy::cast_precision_loss)]
fn get_mem_dataset(&self) -> Vec<(f64, f64)> {
self.mem_stats
.iter()
+24 -28
View File
@@ -163,7 +163,7 @@ impl AppData {
}
/// Find the id of the currently selected container.
/// If any containers on system, will always return a container id
/// If any containers on system, will always return a ContainerId
/// Only returns None when no containers found.
pub fn get_selected_container_id(&self) -> Option<ContainerId> {
let mut output = None;
@@ -323,6 +323,7 @@ impl AppData {
let mut output = Columns::new();
let count = |x: &String| x.chars().count();
// Should probably find a refactor here somewhere
for container in &self.containers.items {
let cpu_count = count(
&container
@@ -418,18 +419,18 @@ impl AppData {
}
/// Update, or insert, containers
pub fn update_containers(&mut self, containers: &mut [ContainerSummary]) {
pub fn update_containers(&mut self, all_containers: &mut [ContainerSummary]) {
let all_ids = self.get_all_ids();
if !containers.is_empty() && self.containers.state.selected().is_none() {
if !all_containers.is_empty() && self.containers.state.selected().is_none() {
self.containers.start();
}
for (index, id) in all_ids.iter().enumerate() {
if !containers
if !all_containers
.iter()
.filter_map(|i| i.id.as_ref())
.any(|x| ContainerId::from(x) == id.clone())
.any(|x| &ContainerId::from(x) == id)
{
// If removed container is currently selected, then change selected to previous
// This will default to 0 in any edge cases
@@ -443,15 +444,14 @@ impl AppData {
}
}
for i in containers.iter_mut() {
for i in all_containers.iter_mut() {
if let Some(id) = i.id.as_ref() {
// maybe if no name then continue?
let name = i.names.as_mut().map_or("".to_owned(), |n| {
n.get_mut(0).map_or("".to_owned(), |f| {
if f.starts_with('/') {
f.remove(0);
}
f.clone()
(*f).to_string()
})
});
@@ -470,36 +470,32 @@ impl AppData {
.as_ref()
.map_or("".to_owned(), std::clone::Clone::clone);
let id = ContainerId::from(id.as_str());
if let Some(current_container) = self.get_container_by_id(&id) {
if current_container.name != name {
current_container.name = name;
let id = ContainerId::from(id);
// If container info already in containers Vec, then just update details
if let Some(item) = self.get_container_by_id(&id) {
if item.name != name {
item.name = name;
};
if current_container.status != status {
current_container.status = status;
if item.status != status {
item.status = status;
};
if current_container.state != state {
current_container.docker_controls.items = DockerControls::gen_vec(state);
if item.state != state {
item.docker_controls.items = DockerControls::gen_vec(state);
// Update the list state, needs to be None if the gen_vec returns an empty vec
match state {
State::Removing | State::Restarting | State::Unknown => {
current_container.docker_controls.state.select(None);
item.docker_controls.state.select(None);
}
_ => current_container.docker_controls.start(),
_ => item.docker_controls.start(),
};
current_container.state = state;
item.state = state;
};
if current_container.image != image {
// limit image name to 64 chars?
// current_container.image = image.chars().into_iter().take(64).collect();
current_container.image = image;
if item.image != image {
item.image = image;
};
// else container not known, so make new ContainerItem and push into containers Vec
} else {
// limit image name to 64 chars?
// let mut container = ContainerItem::new(id.clone(), status, image.chars().into_iter().take(64).collect(), state, name);
let mut container = ContainerItem::new(id.clone(), status, image, state, name);
container.logs.end();
let container = ContainerItem::new(id, status, image, state, name);
self.containers.items.push(container);
}
}