goat_cli/progress/
mod.rs

1//!
2//! Module for progress bar addition to searches.
3//!
4//! Add with `--progress-bar` in `goat-cli search` and
5//! `goat-cli newick`.
6
7use futures::StreamExt;
8use indicatif;
9use serde_json::Value;
10use std::time::Duration;
11use tokio::time::sleep;
12
13use crate::client::GoatClient;
14use crate::error::Result;
15use crate::cli::SearchRequest;
16use crate::count;
17use crate::utils::cli_matches::{self, CliAction};
18use crate::{GOAT_URL, UPPER_CLI_SIZE_LIMIT};
19
20// a function to create and display a progress bar
21// for large requests. Currently limited to single large requests.
22
23/// Adds a progress bar to large requests: a `search` (`Some(request)`),
24/// or a `newick` report (`None`).
25pub async fn progress_bar(search: Option<&SearchRequest>, unique_ids: Vec<String>) -> Result<()> {
26    // nothing is fetched when only printing the URLs
27    if let Some(request) = search {
28        if request.output.url || request.output.goat_ui_url {
29            return Ok(());
30        }
31    }
32    // wait briefly before submitting
33    // so we are sure the API has recieved and set the queryId
34    sleep(Duration::from_secs(2)).await;
35    let (size_int, url_vector_api) = match search {
36        None => (0u64, vec!["init".to_string()]),
37        Some(request) => {
38            match cli_matches::process_cli_args(request, "search", unique_ids.clone())? {
39                CliAction::Continue { size, urls, .. } => (size, urls),
40                CliAction::PrintedAndExit => return Ok(()),
41            }
42        }
43    };
44
45    let concurrent_requests = url_vector_api.len();
46
47    // For search/count-style commands, use a count preflight to decide
48    // whether a progress bar is worthwhile. Newick does not share the same
49    // CLI shape, so do not route it through count::count/process_cli_args.
50    if let Some(request) = search {
51        let no_query_hits = count::count(request, false, false, unique_ids.clone())
52            .await?
53            .unwrap();
54
55        if no_query_hits < 10000 || size_int < 10000 {
56            return Ok(());
57        }
58    }
59
60    // add the query ID's to a vec
61    let mut query_id_vec = Vec::new();
62    for i in unique_ids.iter().take(concurrent_requests) {
63        let query_id = format!("{}progress?queryId=goat_cli_{}", *GOAT_URL, i);
64        query_id_vec.push(query_id);
65    }
66
67    // we want to wrap this in a loop
68    // and break when sum(progress_x) == sum(progress_total)
69    let bar = indicatif::ProgressBar::new(512);
70    let bar_style = ("█▓▓▒░░░ ", "magenta");
71    bar.set_style(
72        indicatif::ProgressStyle::default_bar()
73            .template(&format!(
74                "{{prefix:.bold}}▕{{bar:57.{}}}▏{{pos}}/{{len}} {{wide_msg}}",
75                bar_style.1
76            ))?
77            .progress_chars(bar_style.0),
78    );
79    bar.set_prefix("Fetching from GoaT: ");
80
81    let client = GoatClient::new();
82    loop {
83        // main body
84        let fetches = futures::stream::iter(query_id_vec.clone().into_iter().map(|path| {
85            let client = client.clone();
86            async move {
87                let v: Value = client.get_json(&path).await?;
88                match &v["progress"] {
89                    Value::Object(_o) => {
90                        let progress_total = v["progress"]["total"].as_u64();
91                        let progress_x = v["progress"]["x"].as_u64();
92                        Ok(Some((progress_x, progress_total)))
93                    }
94                    _ => Ok(None),
95                }
96            }
97        }))
98        .buffered(crate::client::concurrency(concurrent_requests))
99        // complicated. Each u64 can be an option, as some
100        // queries will finish before others
101        // entire tuple is an option, as other progress enums evaluate to None.
102        .collect::<Vec<Result<Option<(Option<u64>, Option<u64>)>>>>();
103
104        let awaited_fetches = fetches.await;
105        // what's going on here?
106        let progress_total: std::result::Result<Vec<_>, _> = awaited_fetches.into_iter().collect();
107
108        let mut progress_x_total = 0;
109        let mut progress_total_total = 0;
110        for el in progress_total? {
111            let x_tot_tup = match el {
112                Some(t) => t,
113                None => (None, None),
114            };
115            progress_x_total += x_tot_tup.0.unwrap_or(0);
116            progress_total_total += x_tot_tup.1.unwrap_or(0);
117        }
118
119        // special case newick
120        if search.is_some() && progress_total_total > *UPPER_CLI_SIZE_LIMIT as u64 {
121            bar.set_length(size_int);
122        } else {
123            bar.set_length(progress_total_total);
124        }
125
126        bar.set_position(progress_x_total);
127
128        if progress_x_total >= progress_total_total {
129            break;
130        }
131
132        sleep(Duration::from_millis(500)).await;
133    }
134
135    Ok(())
136}