Skip to content

Commit

Permalink
refactor: Use comft-table for autowrap
Browse files Browse the repository at this point in the history
  • Loading branch information
jopemachine committed Oct 6, 2024
1 parent 4e65266 commit f1da1f3
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 73 deletions.
96 changes: 54 additions & 42 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions raftify-cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion raftify-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ built = "0.5"
clap = { version = "4.5.18", features = ["derive"] }
raftify = { version = "=0.1.81", features = ["heed_storage", "inmemory_storage", "rocksdb_storage"] }
rocksdb = "0.19.0"
prettytable-rs = "^0.10"
comfy-table = "7.1.1"

[lib]
name = "raftify_cli"
Expand Down
63 changes: 45 additions & 18 deletions raftify-cli/src/commands/debug.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use comfy_table::{Cell, CellAlignment, ContentArrangement, Table};
use core::panic;
use prettytable::*;
use serde_json::Value;
use std::{collections::HashMap, fs, path::Path, sync::Arc};

Expand All @@ -14,7 +14,11 @@ use raftify::{
raft_service, ConfigBuilder, HeedStorage, Result, StableStorage, StorageType,
};

pub fn debug_persisted<LogStorage: StableStorage>(path: &str, logger: slog::Logger) -> Result<()> {
pub fn debug_persisted<LogStorage: StableStorage>(
path: &str,
logger: slog::Logger,
print_as_table: bool,
) -> Result<()> {
let config = ConfigBuilder::new().log_dir(path.to_string()).build();

let storage = match LogStorage::STORAGE_TYPE {
Expand Down Expand Up @@ -43,22 +47,40 @@ pub fn debug_persisted<LogStorage: StableStorage>(path: &str, logger: slog::Logg
}

println!("---- Persisted entries ----");
let mut table = Table::new();
let formatter = CUSTOM_FORMATTER.read().unwrap();

table.add_row(row!["index", "type", "context", "data", "term"]);

for entry in entries.iter() {
println!("Key: {}, {:?}", entry.get_index(), format_entry(entry));
table.add_row(row![
entry.get_index(),
format!("{:?}", entry.get_entry_type()).replace("Entry", ""),
formatter.format_entry_context(&entry.context.clone().into()),
formatter.format_entry_data(&entry.data.clone().into()),
entry.get_term()
]);

if print_as_table {
let mut table = Table::new();
let formatter = CUSTOM_FORMATTER.read().unwrap();

table
.set_header(vec![
Cell::new("index").set_alignment(CellAlignment::Center),
Cell::new("type").set_alignment(CellAlignment::Center),
Cell::new("data").set_alignment(CellAlignment::Center),
Cell::new("term").set_alignment(CellAlignment::Center),
])
.set_content_arrangement(ContentArrangement::Dynamic);

for entry in entries.iter() {
table.add_row(vec![
Cell::new(entry.get_index()).set_alignment(CellAlignment::Center),
Cell::new(
format!("{:?}", entry.get_entry_type())
.replace("Entry", "")
.replace("ConfChangeV2", "ConfChange"),
)
.set_alignment(CellAlignment::Center),
Cell::new(formatter.format_entry_data(&entry.data.clone().into()))
.set_alignment(CellAlignment::Left),
Cell::new(entry.get_term()).set_alignment(CellAlignment::Center),
]);
}
println!("{}", table);
} else {
for entry in entries.iter() {
println!("Key: {}, {:?}", entry.get_index(), format_entry(entry));
}
}
table.printstd();

println!();

Expand All @@ -73,6 +95,7 @@ pub fn debug_persisted<LogStorage: StableStorage>(path: &str, logger: slog::Logg
pub fn debug_persisted_all<LogStorage: StableStorage>(
path_str: &str,
logger: slog::Logger,
print_as_table: bool,
) -> Result<()> {
let path = match fs::canonicalize(Path::new(&path_str)) {
Ok(absolute_path) => absolute_path,
Expand Down Expand Up @@ -104,7 +127,11 @@ pub fn debug_persisted_all<LogStorage: StableStorage>(

for name in dir_entries {
println!("*----- {name} -----*");
debug_persisted::<LogStorage>(&format!("{}/{}", path_str, name), logger.clone())?;
debug_persisted::<LogStorage>(
&format!("{}/{}", path_str, name),
logger.clone(),
print_as_table,
)?;
println!();
}
} else {
Expand Down
Loading

0 comments on commit f1da1f3

Please sign in to comment.