diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json
index d4f7835..0efedc9 100644
--- a/.devcontainer/devcontainer.json
+++ b/.devcontainer/devcontainer.json
@@ -16,6 +16,8 @@
"seccomp=unconfined"
],
+ "postCreateCommand": "cargo install cross",
+
"mounts": [
"source=/etc/timezone,target=/etc/timezone,type=bind,readonly"
],
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5219847..bf03bcd 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,18 @@
+### Chores
++ dependencies updated, [8cd199db49186fad6ce432bb277e3a10f0a08d34], [d880b829c123dbe57deccadef97810e45c083737], [66d57c99558ca14d9593d6dbfd5b0e8e5d59055d], [33f9374908942f4a3b90be227fad94ca353cf351]
++ create_release.sh typos, [9a27d46a044452080144ee1367dc95886b10abf8]
++ dev container post create install cross, [2d253f034182741d434e4bac12317f24221d0d4a]
+
+### Features
++ Logs in own struct, [657ea2d751a71f05b17547b47c492d5676817336]
++ spawn docker exec commands into own thread, [9ec43e124a62a80f4e78acba85fc3af5980ce260]
++ align memory columns correctly, minimum byte display value now `0.00 kB`, rather than `0 B`closes #20, [bd7dfcd2c512a527d66a1388f90006988a487186], [51c580010a24de2427373795803936d498dc8cee]
+
+### Refactors
++ main.rs tidy up, [97b89349dc2de275ca514a1e6420255a63d775e8]
++ derive Default for GuiState, [9dcd0509efeb464f58fb53d813bd78de2447949d]
++ param reduction, AtomicBool to Relaxed, [0350293de3c00c6e5e5d787b7596bb3413d1cda1]
+
# v0.1.11
### 2023-01-03
diff --git a/Cargo.lock b/Cargo.lock
index fe0cded..8ec59a3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -667,9 +667,9 @@ dependencies = [
[[package]]
name = "proc-macro2"
-version = "1.0.49"
+version = "1.0.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "57a8eca9f9c4ffde41714334dee777596264c7825420f521abc92b5b5deb63a5"
+checksum = "6ef7d57beacfaf2d8aee5937dab7b7f28de3cb8b1828479bb5de2a7106f2bae2"
dependencies = [
"unicode-ident",
]
@@ -896,9 +896,9 @@ dependencies = [
[[package]]
name = "termcolor"
-version = "1.1.3"
+version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
+checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6"
dependencies = [
"winapi-util",
]
@@ -949,9 +949,9 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
[[package]]
name = "tokio"
-version = "1.24.1"
+version = "1.24.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1d9f76183f91ecfb55e1d7d5602bd1d979e38a3a522fe900241cf195624d67ae"
+checksum = "597a12a59981d9e3c38d216785b0c37399f6e415e8d0712047620f189371b0bb"
dependencies = [
"autocfg",
"bytes",
diff --git a/create_release.sh b/create_release.sh
index 64e5193..f04c721 100755
--- a/create_release.sh
+++ b/create_release.sh
@@ -105,11 +105,11 @@ update_release_body_and_changelog () {
# Update changelog to add links to commits [hex:8](url_with_full_commit)
# "[aaaaaaaaaabbbbbbbbbbccccccccccddddddddd]" -> "[aaaaaaaa](https:/www.../commit/aaaaaaaaaabbbbbbbbbbccccccccccddddddddd)"
- sed -i -E "s=(\s)\[([0-9a-f]{8})([0-9a-f]{32})\]= [\2](${GIT_REPO_URL}/commit/\2\3)=g" ./CHANGELOG.md
+ sed -i -E "s=(\s)\[([0-9a-f]{8})([0-9a-f]{32})\]= [\2](${GIT_REPO_URL}/commit/\2\3)=g" CHANGELOG.md
- # Update changelog to add links to closed issues - comma included!
- # "closes #1" -> "closes [#1](https:/www.../issues/1),""
- sed -i -r -E "s=closes \#([0-9]+)=closes [#\1](${GIT_REPO_URL}/issues/\1)=g" ./CHANGELOG.md
+ # Update changelog to add links to closed issues
+ # "closes #1" -> "closes [#1](https:/www.../issues/1)""
+ sed -i -r -E "s=closes \#([0-9]+)=closes [#\1](${GIT_REPO_URL}/issues/\1)=g" CHANGELOG.md
}
# update version in cargo.toml, to match selected current version
diff --git a/src/app_data/container_state.rs b/src/app_data/container_state.rs
index f759867..efa2d81 100644
--- a/src/app_data/container_state.rs
+++ b/src/app_data/container_state.rs
@@ -1,4 +1,8 @@
-use std::{cmp::Ordering, collections::VecDeque, fmt};
+use std::{
+ cmp::Ordering,
+ collections::{HashSet, VecDeque},
+ fmt,
+};
use tui::{
style::Color,
@@ -352,6 +356,87 @@ impl fmt::Display for ByteStats {
pub type MemTuple = (Vec<(f64, f64)>, ByteStats, State);
pub type CpuTuple = (Vec<(f64, f64)>, CpuStats, State);
+/// Used to make sure that each log entry, for each container, is unique,
+/// will only push a log entry into the logs vec if timetstamp of said log entry isn't in the hashset
+#[derive(Debug, Clone, Hash, PartialEq, Eq)]
+pub struct LogsTz(String);
+
+/// The docker log, which should always contain a timestamp, is in the format `2023-01-14T19:13:30.783138328Z Lorem ipsum dolor sit amet`
+/// So just split at the inclusive index of the first space, needs to be inclusive, hence the use of format to at the space, so that we can remove the whole thing when the `-t` flag is set
+/// Need to make sure that this isn't an empty string?!
+impl From<&String> for LogsTz {
+ fn from(value: &String) -> Self {
+ Self(value.split_inclusive(' ').take(1).collect::())
+ }
+}
+
+impl fmt::Display for LogsTz {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "{}", self.0)
+ }
+}
+
+/// Store the logs alongside a HashSet, each log *should* generate a unique timestamp,
+/// so if we store the timestamp seperately in a HashSet, we can then check if we should insert a log line into the
+/// stateful list dependant on whethere the timestamp is in the HashSet or not
+#[derive(Debug, Clone)]
+pub struct Logs {
+ logs: StatefulList>,
+ tz: HashSet,
+}
+
+impl Default for Logs {
+ fn default() -> Self {
+ let mut logs = StatefulList::new(vec![]);
+ logs.end();
+ Self {
+ logs,
+ tz: HashSet::new(),
+ }
+ }
+}
+
+impl Logs {
+ /// Only allow a new log line to be inserted if the log timestamp isn't in the tz HashSet
+ pub fn insert(&mut self, line: ListItem<'static>, tz: LogsTz) {
+ if self.tz.insert(tz) {
+ self.logs.items.push(line);
+ };
+ }
+
+ pub fn to_vec(&self) -> Vec> {
+ self.logs.items.clone()
+ }
+
+ /// The rest of the methods are basically forwarding from the underlying StatefulList
+ pub fn get_state_title(&self) -> String {
+ self.logs.get_state_title()
+ }
+
+ pub fn next(&mut self) {
+ self.logs.next();
+ }
+
+ pub fn previous(&mut self) {
+ self.logs.previous();
+ }
+
+ pub fn end(&mut self) {
+ self.logs.end();
+ }
+ pub fn start(&mut self) {
+ self.logs.start();
+ }
+
+ pub fn len(&self) -> usize {
+ self.logs.items.len()
+ }
+
+ pub fn state(&mut self) -> &mut ListState {
+ &mut self.logs.state
+ }
+}
+
/// Info for each container
#[derive(Debug, Clone)]
pub struct ContainerItem {
@@ -361,7 +446,7 @@ pub struct ContainerItem {
pub id: ContainerId,
pub image: String,
pub last_updated: u64,
- pub logs: StatefulList>,
+ pub logs: Logs,
pub mem_limit: ByteStats,
pub mem_stats: VecDeque,
pub name: String,
@@ -385,8 +470,6 @@ impl ContainerItem {
) -> Self {
let mut docker_controls = StatefulList::new(DockerControls::gen_vec(state));
docker_controls.start();
- let mut logs = StatefulList::new(vec![]);
- logs.end();
Self {
created,
cpu_stats: VecDeque::with_capacity(60),
@@ -395,7 +478,7 @@ impl ContainerItem {
image,
is_oxker,
last_updated: 0,
- logs,
+ logs: Logs::default(),
mem_limit: ByteStats::default(),
mem_stats: VecDeque::with_capacity(60),
name,
@@ -479,14 +562,13 @@ impl Columns {
Self {
state: (Header::State, 11),
status: (Header::Status, 16),
- // 7 to allow for "100.00%"
cpu: (Header::Cpu, 7),
- mem: (Header::Memory, 6, 6),
+ mem: (Header::Memory, 7, 7),
id: (Header::Id, 8),
name: (Header::Name, 4),
image: (Header::Image, 5),
- net_rx: (Header::Rx, 5),
- net_tx: (Header::Tx, 5),
+ net_rx: (Header::Rx, 7),
+ net_tx: (Header::Tx, 7),
}
}
}
diff --git a/src/app_data/mod.rs b/src/app_data/mod.rs
index 94e5850..fc106f4 100644
--- a/src/app_data/mod.rs
+++ b/src/app_data/mod.rs
@@ -12,7 +12,6 @@ pub use container_state::*;
#[derive(Debug, Clone)]
pub struct AppData {
error: Option,
- logs_parsed: bool,
sorted_by: Option<(Header, SortedOrder)>,
pub args: CliArgs,
pub containers: StatefulList,
@@ -62,7 +61,6 @@ impl AppData {
args,
containers: StatefulList::new(vec![]),
error: None,
- logs_parsed: false,
sorted_by: None,
}
}
@@ -193,7 +191,7 @@ impl AppData {
/// Check if the selected container is a dockerised version of oxker
/// So that can disallow commands to be send
- /// Is a poor way of implementing this
+ /// Is a shabby way of implementing this
pub fn selected_container_is_oxker(&self) -> bool {
if let Some(index) = self.containers.state.selected() {
if let Some(x) = self.containers.items.get(index) {
@@ -352,7 +350,7 @@ impl AppData {
.iter()
.filter(|i| !i.cpu_stats.is_empty())
.count();
- self.logs_parsed && count_is_running == number_with_cpu_status
+ count_is_running == number_with_cpu_status
}
/// Just get the total number of containers
@@ -382,8 +380,14 @@ impl AppData {
let name_count = count(&container.name);
let state_count = count(&container.state.to_string());
let status_count = count(&container.status);
- let mem_current_count = count(&container.mem_stats.back().unwrap_or(&ByteStats::default()).to_string());
- let mem_limit_count= count(&container.mem_limit.to_string());
+ let mem_current_count = count(
+ &container
+ .mem_stats
+ .back()
+ .unwrap_or(&ByteStats::default())
+ .to_string(),
+ );
+ let mem_limit_count = count(&container.mem_limit.to_string());
if cpu_count > output.cpu.1 {
output.cpu.1 = cpu_count;
@@ -394,7 +398,7 @@ impl AppData {
if mem_current_count > output.mem.1 {
output.mem.1 = mem_current_count;
};
- if mem_limit_count > output.mem.2 {
+ if mem_limit_count > output.mem.2 {
output.mem.2 = mem_limit_count;
};
if name_count > output.name.1 {
@@ -548,8 +552,8 @@ impl AppData {
if item.image != image {
item.image = image;
};
- // else container not known, so make new ContainerItem and push into containers Vec
} else {
+ // container not known, so make new ContainerItem and push into containers Vec
let container =
ContainerItem::new(created, id, image, is_oxker, name, state, status);
self.containers.items.push(container);
@@ -559,34 +563,39 @@ impl AppData {
}
/// update logs of a given container, based on id
- pub fn update_log_by_id(&mut self, output: &[String], id: &ContainerId) {
- let tz = Self::get_systemtime();
+ pub fn update_log_by_id(&mut self, output: Vec, id: &ContainerId) {
let color = self.args.color;
let raw = self.args.raw;
- if let Some(container) = self.get_container_by_id(id) {
- container.last_updated = tz;
- let current_len = container.logs.items.len();
+ let timestamp = self.args.timestamp;
- for i in output {
+ if let Some(container) = self.get_container_by_id(id) {
+ container.last_updated = Self::get_systemtime();
+ let current_len = container.logs.len();
+
+ for mut i in output {
+ let tz = LogsTz::from(&i);
+ // Strip the timestamp if `-t` flag set
+ if !timestamp {
+ i = i.replace(&tz.to_string(), "");
+ }
let lines = if color {
- log_sanitizer::colorize_logs(i)
+ log_sanitizer::colorize_logs(&i)
} else if raw {
- log_sanitizer::raw(i)
+ log_sanitizer::raw(&i)
} else {
- log_sanitizer::remove_ansi(i)
+ log_sanitizer::remove_ansi(&i)
};
- container.logs.items.push(ListItem::new(lines));
+ container.logs.insert(ListItem::new(lines), tz);
}
// Set the logs selected row for each container
// Either when no long currently selected, or currently selected (before updated) is already at end
- if container.logs.state.selected().is_none()
- || container.logs.state.selected().map_or(1, |f| f + 1) == current_len
+ if container.logs.state().selected().is_none()
+ || container.logs.state().selected().map_or(1, |f| f + 1) == current_len
{
container.logs.end();
}
}
- self.logs_parsed = true;
}
}
diff --git a/src/docker_data/mod.rs b/src/docker_data/mod.rs
index 5daa6c7..c700654 100644
--- a/src/docker_data/mod.rs
+++ b/src/docker_data/mod.rs
@@ -3,7 +3,7 @@ use bollard::{
service::ContainerSummary,
Docker,
};
-use futures_util::{Future, StreamExt};
+use futures_util::StreamExt;
use parking_lot::Mutex;
use std::{
collections::HashMap,
@@ -33,7 +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
+/// Binate value is toggled when all 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 {
@@ -56,7 +56,6 @@ pub struct DockerData {
binate: Binate,
docker: Arc,
gui_state: Arc>,
- initialised: bool,
is_running: Arc,
receiver: Receiver,
spawns: Arc>>>,
@@ -94,12 +93,12 @@ impl DockerData {
/// don't take &self, so that can tokio::spawn into it's own thread
/// remove if from spawns hashmap when complete
async fn update_container_stat(
+ app_data: Arc>,
docker: Arc,
id: ContainerId,
- app_data: Arc>,
is_running: bool,
- spawns: Arc>>>,
spawn_id: SpawnId,
+ spawns: Arc>>>,
) {
let mut stream = docker
.stats(
@@ -156,18 +155,18 @@ impl DockerData {
let docker = Arc::clone(&self.docker);
let app_data = Arc::clone(&self.app_data);
let spawns = Arc::clone(&self.spawns);
- let spawn_key = SpawnId::Stats((id.clone(), self.binate));
+ let spawn_id = SpawnId::Stats((id.clone(), self.binate));
self.spawns
.lock()
- .entry(spawn_key.clone())
+ .entry(spawn_id.clone())
.or_insert_with(|| {
tokio::spawn(Self::update_container_stat(
+ app_data,
docker,
id.clone(),
- app_data,
*is_running,
+ spawn_id,
spawns,
- spawn_key,
))
});
}
@@ -223,19 +222,17 @@ impl DockerData {
}
/// Update single container logs
- /// don't take &self, so that can tokio::spawn into it's own thread
- /// remove if from spawns hashmap when complete
+ /// remove it from spawns hashmap when complete
async fn update_log(
+ app_data: Arc>,
docker: Arc,
id: ContainerId,
- timestamps: bool,
since: u64,
- app_data: Arc>,
spawns: Arc>>>,
) {
let options = Some(LogsOptions:: {
stdout: true,
- timestamps,
+ timestamps: true,
since: i64::try_from(since).unwrap_or_default(),
..Default::default()
});
@@ -243,16 +240,14 @@ impl DockerData {
let mut logs = docker.logs(id.get(), options);
let mut output = vec![];
- while let Some(value) = logs.next().await {
- if let Ok(data) = value {
- let log_string = data.to_string();
- if !log_string.trim().is_empty() {
- output.push(log_string);
- }
+ while let Some(Ok(value)) = logs.next().await {
+ let data = value.to_string();
+ if !data.trim().is_empty() {
+ output.push(data);
}
}
spawns.lock().remove(&SpawnId::Log(id.clone()));
- app_data.lock().update_log_by_id(&output, &id);
+ app_data.lock().update_log_by_id(output, &id);
}
/// Update all logs, spawn each container into own tokio::spawn thread
@@ -264,14 +259,7 @@ impl DockerData {
let key = SpawnId::Log(id.clone());
self.spawns.lock().insert(
key,
- tokio::spawn(Self::update_log(
- docker,
- id.clone(),
- self.args.timestamp,
- 0,
- app_data,
- spawns,
- )),
+ tokio::spawn(Self::update_log(app_data, docker, id.clone(), 0, spawns)),
);
}
}
@@ -290,11 +278,10 @@ impl DockerData {
let app_data = Arc::clone(&self.app_data);
let spawns = Arc::clone(&self.spawns);
tokio::spawn(Self::update_log(
+ app_data,
docker,
container.id.clone(),
- self.args.timestamp,
container.last_updated,
- app_data,
spawns,
))
});
@@ -305,8 +292,8 @@ impl DockerData {
}
/// Animate the loading icon
- async fn loading_spin(&mut self, loading_uuid: Uuid) -> JoinHandle<()> {
- let gui_state = Arc::clone(&self.gui_state);
+ async fn loading_spin(loading_uuid: Uuid, gui_state: &Arc>) -> JoinHandle<()> {
+ let gui_state = Arc::clone(&gui_state);
tokio::spawn(async move {
loop {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
@@ -316,89 +303,106 @@ impl DockerData {
}
/// Stop the loading_spin function, and reset gui loading status
- fn stop_loading_spin(&mut self, handle: &JoinHandle<()>, loading_uuid: Uuid) {
+ fn stop_loading_spin(
+ gui_state: &Arc>,
+ handle: &JoinHandle<()>,
+ loading_uuid: Uuid,
+ ) {
handle.abort();
- self.gui_state.lock().remove_loading(loading_uuid);
+ gui_state.lock().remove_loading(loading_uuid);
}
/// Initialize docker container data, before any messages are received
async fn initialise_container_data(&mut self) {
self.gui_state.lock().status_push(Status::Init);
let loading_uuid = Uuid::new_v4();
- let loading_spin = self.loading_spin(loading_uuid).await;
+ let loading_spin = Self::loading_spin(loading_uuid, &Arc::clone(&self.gui_state)).await;
let all_ids = self.update_all_containers().await;
self.update_all_container_stats(&all_ids);
- // Maybe only do a single one at first?
self.init_all_logs(&all_ids);
- if all_ids.is_empty() {
- self.initialised = true;
- }
-
// wait until all logs have initialised
- while !self.initialised {
+ while !self.app_data.lock().initialised(&all_ids) {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
- self.initialised = self.app_data.lock().initialised(&all_ids);
}
self.gui_state.lock().status_del(Status::Init);
- self.stop_loading_spin(&loading_spin, loading_uuid);
+ Self::stop_loading_spin(&self.gui_state, &loading_spin, loading_uuid);
}
/// Set the global error as the docker error, and set gui_state to error
- fn set_error(&mut self, error: DockerControls) {
- self.app_data
- .lock()
- .set_error(AppError::DockerCommand(error));
- self.gui_state.lock().status_push(Status::Error);
- }
-
- /// Execute a docker command, will start and stop the loading spinner, and set correct error
- async fn exec_docker(
- &mut self,
- docker_fn: impl Future