refactor: remove pointless clone()'s & variable declarations

This commit is contained in:
Jack Wills
2022-10-01 21:02:38 +00:00
parent 1dbb914387
commit 6731002ee4
7 changed files with 59 additions and 70 deletions
+2 -11
View File
@@ -318,7 +318,6 @@ impl fmt::Display for ByteStats {
}
}
pub type MemTuple = (Vec<(f64, f64)>, ByteStats, State);
pub type CpuTuple = (Vec<(f64, f64)>, CpuStats, State);
@@ -398,20 +397,12 @@ impl ContainerItem {
/// Get all cpu chart data
fn get_cpu_chart_data(&self) -> CpuTuple {
(
self.get_cpu_dataset(),
self.max_cpu_stats(),
self.state,
)
(self.get_cpu_dataset(), self.max_cpu_stats(), self.state)
}
/// Get all mem chart data
fn get_mem_chart_data(&self) -> MemTuple {
(
self.get_mem_dataset(),
self.max_mem_stats(),
self.state,
)
(self.get_mem_dataset(), self.max_mem_stats(), self.state)
}
/// Get chart info for cpu & memory in one function
+25 -24
View File
@@ -33,6 +33,7 @@ enum SpawnId {
/// Cpu & Mem stats take twice as long as the update interval to get a value, so will have two being executed at the same time
/// SpawnId::Stats takes container_id and binate value to enable both cycles of the same container_id to be inserted into the hashmap
/// Binate value is toggled when all join handles have been spawned off
/// Also effectively means that if the docker_update interval minimum will be 1000ms
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
enum Binate {
One,
@@ -113,22 +114,20 @@ impl DockerData {
let mem_stat = stats.memory_stats.usage.unwrap_or(0);
let mem_limit = stats.memory_stats.limit.unwrap_or(0);
let some_key = stats
let op_key = stats
.networks
.as_ref()
.and_then(|networks| networks.keys().next().cloned());
let cpu_stats = Self::calculate_usage(&stats);
let no_bytes = || (0, 0);
let (rx, tx) = if let Some(key) = some_key {
let (rx, tx) = if let Some(key) = op_key {
match stats.networks.unwrap_or_default().get(&key) {
Some(data) => (data.rx_bytes, data.tx_bytes),
None => no_bytes(),
None => (0, 0),
}
} else {
no_bytes()
(0, 0)
};
if is_running {
@@ -155,8 +154,6 @@ impl DockerData {
let docker = Arc::clone(&self.docker);
let app_data = Arc::clone(&self.app_data);
let spawns = Arc::clone(&self.spawns);
let id = id.clone();
let key = SpawnId::Stats((id.clone(), self.binate));
let spawn_key = key.clone();
@@ -228,19 +225,18 @@ impl DockerData {
docker: Arc<Docker>,
id: String,
timestamps: bool,
since: i64,
since: u64,
app_data: Arc<Mutex<AppData>>,
spawns: Arc<Mutex<HashMap<SpawnId, JoinHandle<()>>>>,
) {
let options = Some(LogsOptions::<String> {
stdout: true,
timestamps,
since,
since: since as i64,
..Default::default()
});
let mut logs = docker.logs(&id, options);
let mut output = vec![];
while let Some(value) = logs.next().await {
@@ -259,15 +255,18 @@ impl DockerData {
fn init_all_logs(&mut self, all_ids: &[(bool, String)]) {
for (_, id) in all_ids.iter() {
let docker = Arc::clone(&self.docker);
let timestamps = self.timestamps;
let id = id.clone();
let app_data = Arc::clone(&self.app_data);
let spawns = Arc::clone(&self.spawns);
let key = SpawnId::Log(id.clone());
self.spawns.lock().insert(
key,
tokio::spawn(Self::update_log(
docker, id, timestamps, 0, app_data, spawns,
docker,
id.clone(),
self.timestamps,
0,
app_data,
spawns,
)),
);
}
@@ -278,20 +277,24 @@ impl DockerData {
let all_ids = self.update_all_containers().await;
let optional_index = self.app_data.lock().get_selected_log_index();
if let Some(index) = optional_index {
// this could be neater
let id = self.app_data.lock().containers.items[index].id.clone();
let key = SpawnId::Log(id.clone());
self.spawns.lock().entry(key).or_insert_with(|| {
let since = self.app_data.lock().containers.items[index].last_updated as i64;
if let Some(container) = self.app_data.lock().containers.items.get(index) {
self.spawns
.lock()
.entry(SpawnId::Log(container.id.clone()))
.or_insert_with(|| {
let docker = Arc::clone(&self.docker);
let timestamps = self.timestamps;
let app_data = Arc::clone(&self.app_data);
let spawns = Arc::clone(&self.spawns);
tokio::spawn(Self::update_log(
docker, id, timestamps, since, app_data, spawns,
docker,
container.id.clone(),
self.timestamps,
container.last_updated,
app_data,
spawns,
))
});
}
};
self.update_all_container_stats(&all_ids);
}
@@ -391,8 +394,6 @@ impl DockerData {
.lock()
.set_error(AppError::DockerCommand(DockerControls::Unpause));
};
// loading sping take uuid to remove
// stop_loading_sping(uuid)
self.stop_loading_spin(&loading_spin, loading_uuid);
self.update_everything().await;
}
+2 -5
View File
@@ -46,8 +46,7 @@ async fn main() {
// Create docker daemon handler, and only spawn up the docker data handler if ping returns non-error
match Docker::connect_with_socket_defaults() {
Ok(docker) => {
match docker.ping().await {
Ok(docker) => match docker.ping().await {
Ok(_) => {
let docker = Arc::new(docker);
let is_running = Arc::clone(&is_running);
@@ -61,8 +60,7 @@ async fn main() {
));
}
Err(_) => app_data.lock().set_error(AppError::DockerConnect),
}
}
},
Err(_) => app_data.lock().set_error(AppError::DockerConnect),
}
let input_app_data = Arc::clone(&app_data);
@@ -82,7 +80,6 @@ async fn main() {
input_is_running,
));
if args.gui {
let update_duration = std::time::Duration::from_millis(u64::from(args.docker_interval));
create_ui(
+1 -1
View File
@@ -7,7 +7,7 @@ use tracing::error;
// #[command(help_template = FULL_TEMPLATE)]
#[command(version, about)]
pub struct CliArgs {
/// Docker update interval in ms, minimum 1, reccomended 500+
/// Docker update interval in ms, minimum effectively 1000
#[clap(short = 'd', value_name = "ms", default_value_t = 1000)]
pub docker_interval: u32,