Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commands: add source statistics to table output #225

Merged
merged 3 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
6 changes: 3 additions & 3 deletions api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),]
);
}
}
39 changes: 36 additions & 3 deletions cli/src/commands/get/sources.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -10,10 +11,18 @@ pub struct GetSourcesArgs {
#[structopt(name = "source")]
/// If specified, only list this source (name or id)
source: Option<SourceIdentifier>,

#[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())
Expand All @@ -35,6 +44,24 @@ pub fn get(client: &Client, args: &GetSourcesArgs, printer: &Printer) -> Result<
.map(|bucket| (bucket.id.clone(), bucket))
.collect();

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<PrintableSource> = sources
.into_iter()
.map(|source| {
Expand All @@ -43,7 +70,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();

Expand Down
14 changes: 11 additions & 3 deletions cli/src/printer.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -144,6 +146,7 @@ impl DisplayTable for Source {
pub struct PrintableSource {
pub source: Source,
pub bucket: Option<Bucket>,
pub stats: Option<Statistics>,
}

impl Serialize for PrintableSource {
Expand All @@ -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 {
Expand All @@ -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().as_str().into()
} else {
"none".dimmed()
}
]
}
}
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/test_comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<&str>>(),
comments_str.as_bytes(),
);
Expand Down
Loading