From 3e2e607f78c895b5c918fa388672fa164af86ff9 Mon Sep 17 00:00:00 2001 From: Joe Prosser Date: Mon, 11 Sep 2023 10:59:52 +0100 Subject: [PATCH 1/3] commands: add source statistics to table output --- CHANGELOG.md | 3 +++ cli/src/commands/get/sources.rs | 41 ++++++++++++++++++++++++++++++--- cli/src/printer.rs | 14 ++++++++--- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46e58f07..ca439cb1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## Unreleased +- Show source statistics in table when getting sources + ## v0.18.2 -Add ability to filter on user properties when getting comments diff --git a/cli/src/commands/get/sources.rs b/cli/src/commands/get/sources.rs index 973d34b1..59c05ce1 100644 --- a/cli/src/commands/get/sources.rs +++ b/cli/src/commands/get/sources.rs @@ -1,5 +1,6 @@ use anyhow::{Context, Result}; -use reinfer_client::{Client, SourceIdentifier}; +use log::info; +use reinfer_client::{resources::source::StatisticsRequestParams, Client, SourceIdentifier}; use std::collections::HashMap; use structopt::StructOpt; @@ -10,10 +11,18 @@ pub struct GetSourcesArgs { #[structopt(name = "source")] /// If specified, only list this source (name or id) source: Option, + + #[structopt(long = "stats")] + /// Whether to include source statistics in response + include_stats: bool, } pub fn get(client: &Client, args: &GetSourcesArgs, printer: &Printer) -> Result<()> { - let GetSourcesArgs { source } = args; + let GetSourcesArgs { + source, + include_stats, + } = args; + let sources = if let Some(source) = source { vec![client .get_source(source.clone()) @@ -35,6 +44,26 @@ pub fn get(client: &Client, args: &GetSourcesArgs, printer: &Printer) -> Result< .map(|bucket| (bucket.id.clone(), bucket)) .collect(); + let source_stats: HashMap<_, _> = if *include_stats { + sources + .iter() + .map(|source| { + info!("Getting statistics for source {}", source.full_name().0); + let stats = client + .get_source_statistics( + &source.full_name(), + &StatisticsRequestParams { + comment_filter: Default::default(), + }, + ) + .expect("Could not get statistics for source"); + (source.id.clone(), stats) + }) + .collect() + } else { + HashMap::new() + }; + let printable_sources: Vec = sources .into_iter() .map(|source| { @@ -43,7 +72,13 @@ pub fn get(client: &Client, args: &GetSourcesArgs, printer: &Printer) -> Result< .as_ref() .and_then(|id| buckets.get(id)) .cloned(); - PrintableSource { source, bucket } + + let stats = source_stats.get(&source.id).cloned(); + PrintableSource { + source, + bucket, + stats, + } }) .collect(); diff --git a/cli/src/printer.rs b/cli/src/printer.rs index a558db6f..4ea7052b 100644 --- a/cli/src/printer.rs +++ b/cli/src/printer.rs @@ -1,7 +1,9 @@ use super::thousands::Thousands; use colored::Colorize; use prettytable::{format, row, Row, Table}; -use reinfer_client::{resources::quota::Quota, Bucket, Dataset, Project, Source, Stream, User}; +use reinfer_client::{ + resources::quota::Quota, Bucket, Dataset, Project, Source, Statistics, Stream, User, +}; use serde::{Serialize, Serializer}; use anyhow::{anyhow, Context, Error, Result}; @@ -144,6 +146,7 @@ impl DisplayTable for Source { pub struct PrintableSource { pub source: Source, pub bucket: Option, + pub stats: Option, } impl Serialize for PrintableSource { @@ -157,7 +160,7 @@ impl Serialize for PrintableSource { impl DisplayTable for PrintableSource { fn to_table_headers() -> Row { - row![bFg => "Name", "ID", "Updated (UTC)", "Transform Tag", "Bucket", "Title"] + row![bFg => "Name", "ID", "Updated (UTC)", "Transform Tag", "Bucket", "Title", "Num Comments"] } fn to_table_row(&self) -> Row { @@ -182,7 +185,12 @@ impl DisplayTable for PrintableSource { None => "none".dimmed(), }, }, - self.source.title + self.source.title, + if let Some(stats) = &self.stats { + stats.num_comments.to_string() + } else { + "N/A".to_string() + } ] } } From 721588bab97ee39d3bd2e726fd02935350cc0d89 Mon Sep 17 00:00:00 2001 From: Joe Prosser Date: Mon, 11 Sep 2023 11:30:09 +0100 Subject: [PATCH 2/3] post review fixup --- cli/src/commands/get/sources.rs | 34 ++++++++++++++++----------------- cli/src/printer.rs | 4 ++-- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/cli/src/commands/get/sources.rs b/cli/src/commands/get/sources.rs index 59c05ce1..e98e2112 100644 --- a/cli/src/commands/get/sources.rs +++ b/cli/src/commands/get/sources.rs @@ -44,24 +44,22 @@ pub fn get(client: &Client, args: &GetSourcesArgs, printer: &Printer) -> Result< .map(|bucket| (bucket.id.clone(), bucket)) .collect(); - let source_stats: HashMap<_, _> = if *include_stats { - sources - .iter() - .map(|source| { - info!("Getting statistics for source {}", source.full_name().0); - let stats = client - .get_source_statistics( - &source.full_name(), - &StatisticsRequestParams { - comment_filter: Default::default(), - }, - ) - .expect("Could not get statistics for source"); - (source.id.clone(), stats) - }) - .collect() - } else { - HashMap::new() + let mut source_stats: HashMap<_, _> = HashMap::new(); + if *include_stats { + sources.iter().try_for_each(|source| -> Result<()> { + info!("Getting statistics for source {}", source.full_name().0); + let stats = client + .get_source_statistics( + &source.full_name(), + &StatisticsRequestParams { + comment_filter: Default::default(), + }, + ) + .context("Could not get statistics for source")?; + + source_stats.insert(source.id.clone(), stats); + Ok(()) + })?; }; let printable_sources: Vec = sources diff --git a/cli/src/printer.rs b/cli/src/printer.rs index 4ea7052b..edc9d07f 100644 --- a/cli/src/printer.rs +++ b/cli/src/printer.rs @@ -187,9 +187,9 @@ impl DisplayTable for PrintableSource { }, self.source.title, if let Some(stats) = &self.stats { - stats.num_comments.to_string() + stats.num_comments.to_string().as_str().into() } else { - "N/A".to_string() + "none".dimmed() } ] } From d6e23d3de8c42089c6152ca67da137441d2b9f5d Mon Sep 17 00:00:00 2001 From: Joe Prosser Date: Mon, 11 Sep 2023 11:36:33 +0100 Subject: [PATCH 3/3] fix clippy --- api/src/lib.rs | 6 +++--- cli/tests/test_comments.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/api/src/lib.rs b/api/src/lib.rs index 9c7ee193..4b9d177c 100644 --- a/api/src/lib.rs +++ b/api/src/lib.rs @@ -1609,19 +1609,19 @@ mod tests { fn test_id_list_query() { assert_eq!(id_list_query(Vec::new().iter()), Vec::new()); assert_eq!( - id_list_query(vec!["foo".to_owned()].iter()), + id_list_query(["foo".to_owned()].iter()), vec![("id", "foo")] ); assert_eq!( id_list_query( - vec![ + [ "Stream".to_owned(), "River".to_owned(), "Waterfall".to_owned() ] .iter() ), - vec![("id", "Stream"), ("id", "River"), ("id", "Waterfall"),] + [("id", "Stream"), ("id", "River"), ("id", "Waterfall"),] ); } } diff --git a/cli/tests/test_comments.rs b/cli/tests/test_comments.rs index 4bc6cddd..3c1971ab 100644 --- a/cli/tests/test_comments.rs +++ b/cli/tests/test_comments.rs @@ -59,7 +59,7 @@ fn check_comments_lifecycle(comments_str: &str, args: Vec<&str>) { &format!("--source={}", source.identifier()), ] .into_iter() - .chain(args.into_iter())) + .chain(args)) .collect::>(), comments_str.as_bytes(), );