github_analytics/print_data/
mod.rs

1use std::collections::HashMap;
2
3use crate::models::Interaction;
4
5/// Display the summary of the interactions
6///
7/// # Arguments
8///
9/// * `repo_info` - A HashMap containing the interactions
10///
11/// # Example
12///
13/// ```
14/// use std::collections::HashMap;
15///
16/// use github_analytics::models::Interaction;
17/// use github_analytics::print_data::display_summary;
18///
19/// let mut repo_info: HashMap<String, Vec<Interaction>> = HashMap::new();
20/// repo_info.insert("repo".to_owned(), vec![]);
21/// display_summary(&repo_info);
22/// ```
23pub fn display_summary(repo_info: &HashMap<String, Vec<Interaction>>) {
24    // Create the header of the table.
25    let mut lines: Vec<Vec<String>> = vec![vec![
26        "Repo".to_owned(),
27        "PRs opened".to_owned(),
28        "PRs merged".to_owned(),
29        "Issues opened".to_owned(),
30        "Issues closed".to_owned(),
31    ]];
32    // Count the different interactions for each repo.
33    repo_info.into_iter().for_each(|(repo, interactions)| {
34        let mut issues_opened = 0;
35        let mut issues_closed = 0;
36        let mut prs_opened = 0;
37        let mut prs_closed = 0;
38
39        interactions.into_iter().for_each(|interaction| {
40            match interaction.interaction_type.as_str() {
41                "pr created" => prs_opened += 1,
42                "pr ended" => prs_closed += 1,
43                "issue created" => issues_opened += 1,
44                "issue ended" => issues_closed += 1,
45                _ => panic!("Unknown interaction type"),
46            }
47        });
48        lines.push(
49            [
50                repo.to_owned(),
51                prs_opened.to_string(),
52                prs_closed.to_string(),
53                issues_opened.to_string(),
54                issues_closed.to_string(),
55            ]
56            .to_vec(),
57        );
58    });
59    let mut out = Vec::new();
60    // Format the table.
61    text_tables::render(&mut out, lines).unwrap();
62    // Print the table.
63    println!("{}", std::str::from_utf8(&out).unwrap());
64}