1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use anyhow::{bail, ensure, Result};
use async_std::task;
use futures::StreamExt;
use indicatif;
use reqwest;
use reqwest::header::ACCEPT;
use serde_json::Value;
use std::time::Duration;
use crate::utils::cli_matches;
use crate::{count, IndexType};
use crate::{GOAT_URL, UPPER_CLI_SIZE_LIMIT};
pub async fn progress_bar(
matches: &clap::ArgMatches,
api: &str,
unique_ids: Vec<String>,
index_type: IndexType,
) -> Result<()> {
task::sleep(Duration::from_secs(2)).await;
let (size_int, _url_vector, url_vector_api) = match api {
"newick" => (0u64, vec!["init".to_string()], vec!["init".to_string()]),
other => cli_matches::process_cli_args(matches, other, unique_ids.clone(), index_type)?,
};
ensure!(
unique_ids.len() == url_vector_api.len(),
"No reason these lengths should be different."
);
let concurrent_requests = url_vector_api.len();
let no_query_hits = count::count(matches, false, false, unique_ids.clone(), index_type)
.await?
.unwrap();
if api != "newick" {
if no_query_hits < 10000 || size_int < 10000 {
return Ok(());
}
}
let mut query_id_vec = Vec::new();
for i in unique_ids.iter().take(concurrent_requests) {
let query_id = format!("{}progress?queryId=goat_cli_{}", *GOAT_URL, i);
query_id_vec.push(query_id);
}
let bar = indicatif::ProgressBar::new(512);
let bar_style = ("█▓▓▒░░░ ", "magenta");
bar.set_style(
indicatif::ProgressStyle::default_bar()
.template(&format!(
"{{prefix:.bold}}▕{{bar:57.{}}}▏{{pos}}/{{len}} {{wide_msg}}",
bar_style.1
))?
.progress_chars(bar_style.0),
);
bar.set_prefix("Fetching from GoaT: ");
loop {
let fetches =
futures::stream::iter(query_id_vec.clone().into_iter().map(|path| async move {
let client = reqwest::Client::new();
match again::retry(|| client.get(&path).header(ACCEPT, "application/json").send())
.await
{
Ok(resp) => match resp.text().await {
Ok(body) => {
let v: Value = serde_json::from_str(&body)?;
match &v["progress"] {
Value::Object(_o) => {
let progress_total = v["progress"]["total"].as_u64();
let progress_x = v["progress"]["x"].as_u64();
Ok(Some((progress_x, progress_total)))
}
_ => Ok(None),
}
}
Err(_) => bail!("ERROR reading {}", path),
},
Err(_) => bail!("ERROR downloading {}", path),
}
}))
.buffered(concurrent_requests)
.collect::<Vec<Result<Option<(Option<u64>, Option<u64>)>>>>();
let awaited_fetches = fetches.await;
let progress_total: Result<Vec<_>, _> = awaited_fetches.into_iter().collect();
let mut progress_x_total = 0;
let mut progress_total_total = 0;
for el in progress_total.unwrap() {
let x_tot_tup = match el {
Some(t) => t,
None => (None, None),
};
progress_x_total += x_tot_tup.0.unwrap_or(0);
progress_total_total += x_tot_tup.1.unwrap_or(0);
}
match api {
"newick" => bar.set_length(progress_total_total),
_ => match progress_total_total > *UPPER_CLI_SIZE_LIMIT as u64 {
true => bar.set_length(size_int),
false => bar.set_length(progress_total_total),
},
}
bar.set_position(progress_x_total);
if progress_x_total >= progress_total_total {
break;
}
task::sleep(Duration::from_millis(1)).await;
}
Ok(())
}