Skip to content

Commit bfad9ef

Browse files
committed
Added better logging and multithreading
1 parent 0327550 commit bfad9ef

File tree

4 files changed

+174
-5
lines changed

4 files changed

+174
-5
lines changed

Cargo.lock

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ edition = "2024"
66
[dependencies]
77
chrono = { version = "0.4.41", features = ["serde"] }
88
clap = { version = "4.5.38", features = ["derive"] }
9+
console = "0.15.11"
910
curl = "0.4.47"
11+
indicatif = "0.17.11"
1012
mockito = "1.7.0"
1113
onig = "6.5.1"
1214
serde = { version = "1.0.219", features = ["derive"] }

src/handlers.rs

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
use crate::build_order::{BuildOrder, BuildOrderError};
22
use crate::build_parser::fetch_build_order;
33
use crate::index_manager::{LOWEST_INDEX, get_st_highest_index};
4+
use console::style;
5+
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
6+
use std::thread;
7+
use std::time::Duration;
48

59
pub fn fetch_latest(count: u32) -> Vec<BuildOrder> {
10+
let spinner_style = ProgressStyle::with_template(
11+
"[{percent:.bold.dim}%] {elapsed:.dim} {spinner} {bar}\t{wide_msg}",
12+
)
13+
.unwrap()
14+
.tick_chars("⡇⣆⣤⣰⢸⠹⠛⠏ ");
15+
let pb = ProgressBar::new(count as u64);
16+
pb.set_style(spinner_style);
17+
pb.enable_steady_tick(Duration::from_millis(100));
18+
619
let highest_index = get_st_highest_index();
720
let mut end_index = if count > highest_index - LOWEST_INDEX {
821
LOWEST_INDEX
@@ -21,7 +34,7 @@ pub fn fetch_latest(count: u32) -> Vec<BuildOrder> {
2134
}
2235
Err(e) => {
2336
if e.eq(&BuildOrderError::Cloaked) {
24-
eprintln!("Build order {} is cloaked, skipping.", current_id);
37+
pb.set_message(format!("Build order {} is cloaked, skipping.", current_id));
2538
end_index = if end_index > LOWEST_INDEX {
2639
end_index - 1
2740
} else {
@@ -35,10 +48,23 @@ pub fn fetch_latest(count: u32) -> Vec<BuildOrder> {
3548
}
3649
}
3750
}
51+
pb.finish_with_message(format!(
52+
"{} {} build orders fetched.",
53+
style("✔").green(),
54+
build_orders.len()
55+
));
3856
build_orders
3957
}
4058

4159
pub fn fetch_segment(start: u32, end: u32) -> Vec<BuildOrder> {
60+
let spinner_style = ProgressStyle::with_template(
61+
"[{percent:.bold.dim}%] {elapsed:.dim} {spinner} {bar}\t{wide_msg}",
62+
)
63+
.unwrap()
64+
.tick_chars("⡇⣆⣤⣰⢸⠹⠛⠏ ");
65+
let pb = ProgressBar::new((end - start + 1) as u64);
66+
pb.set_style(spinner_style);
67+
4268
let highest_index = get_st_highest_index();
4369
let start = if start < LOWEST_INDEX {
4470
LOWEST_INDEX
@@ -59,12 +85,52 @@ pub fn fetch_segment(start: u32, end: u32) -> Vec<BuildOrder> {
5985
return Vec::new();
6086
}
6187

88+
let m = MultiProgress::new();
89+
6290
let mut build_orders = Vec::new();
63-
for id in (start..=end).rev() {
64-
match fetch_build_order(id) {
65-
Ok(build_order) => build_orders.push(build_order),
66-
Err(e) => eprintln!("Error fetching build order {}: {}", id, e),
91+
let mut handles = Vec::new();
92+
let count = (end - start + 1) as usize;
93+
94+
for id in start..=end {
95+
let pb_clone = m.add(ProgressBar::new(1));
96+
let handle = thread::spawn(move || {
97+
let spinner_style =
98+
ProgressStyle::with_template("[{prefix:.bold.dim}] {spinner} \t{wide_msg}")
99+
.unwrap()
100+
.tick_chars("⡇⣆⣤⣰⢸⠹⠛⠏ ");
101+
pb_clone.set_message(format!("Fetching build order {}", id));
102+
pb_clone.set_style(spinner_style.clone());
103+
pb_clone.enable_steady_tick(Duration::from_millis(100));
104+
pb_clone.set_prefix(format!("{}/{}", id - start + 1, count));
105+
match fetch_build_order(id) {
106+
Ok(build_order) => {
107+
pb_clone.inc(1);
108+
Some(build_order)
109+
}
110+
Err(e) => {
111+
if e.eq(&BuildOrderError::Cloaked) {
112+
pb_clone.set_message(format!("Build order {} is cloaked, skipping.", id));
113+
} else {
114+
eprintln!("Error fetching build order {}: {}", id, e);
115+
}
116+
None
117+
}
118+
}
119+
});
120+
handles.push(handle);
121+
}
122+
123+
for handle in handles {
124+
if let Ok(Some(build_order)) = handle.join() {
125+
build_orders.push(build_order);
67126
}
68127
}
128+
129+
pb.finish_with_message(format!(
130+
"{} {} build orders fetched.",
131+
style("✔").green(),
132+
build_orders.len()
133+
));
134+
69135
build_orders
70136
}

src/main.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use build_warren::build_parser::fetch_build_order;
22
use build_warren::handlers::{fetch_latest, fetch_segment};
33
use build_warren::index_manager::get_st_highest_index;
44
use clap::{Parser, Subcommand};
5+
use console::{Emoji, style};
56
use serde_json;
67
use std::fs;
78

@@ -42,15 +43,31 @@ enum Commands {
4243
},
4344
}
4445

46+
static CLIPBOARD_EMOJI: Emoji = Emoji("📋 ", "");
47+
static OUTPUT_EMOJI: Emoji = Emoji("📂 ", "");
48+
4549
fn main() {
4650
let cli = Cli::parse();
4751

52+
println!(
53+
"{} {} {}",
54+
CLIPBOARD_EMOJI,
55+
style("Build Warren CLI").bold().magenta(),
56+
style("v0.1.0").dim()
57+
);
58+
4859
match &cli.command {
4960
Some(Commands::BuildCount) => {
5061
let highest_index = get_st_highest_index();
5162
if let Some(output_file) = &cli.output {
5263
fs::write(output_file, highest_index.to_string())
5364
.expect("Failed to write to output file");
65+
println!(
66+
"{} {}Highest build index written to {}",
67+
OUTPUT_EMOJI,
68+
style("Success : ").green(),
69+
output_file
70+
);
5471
} else {
5572
println!("Highest build index: {}", highest_index);
5673
}
@@ -62,6 +79,13 @@ fn main() {
6279
if let Some(output_file) = &cli.output {
6380
fs::write(output_file, json_output)
6481
.expect("Failed to write build order to output file");
82+
println!(
83+
"{} {}Build order {} written to {}",
84+
OUTPUT_EMOJI,
85+
style("Success : ").green(),
86+
id,
87+
output_file
88+
);
6589
} else {
6690
println!("{}", json_output);
6791
}
@@ -75,6 +99,13 @@ fn main() {
7599
if let Some(output_file) = &cli.output {
76100
fs::write(output_file, json_output)
77101
.expect("Failed to write build orders to output file");
102+
println!(
103+
"{} {}Latest {} build orders written to {}",
104+
OUTPUT_EMOJI,
105+
style("Success : ").green(),
106+
count,
107+
output_file
108+
);
78109
} else {
79110
println!("{}", json_output);
80111
}
@@ -89,6 +120,14 @@ fn main() {
89120
if let Some(output_file) = &cli.output {
90121
fs::write(output_file, json_output)
91122
.expect("Failed to write build orders to output file");
123+
println!(
124+
"{} {}Build orders from {} to {} written to {}",
125+
OUTPUT_EMOJI,
126+
style("Success : ").green(),
127+
start,
128+
end,
129+
output_file
130+
);
92131
} else {
93132
println!("{}", json_output);
94133
}

0 commit comments

Comments
 (0)