Skip to content

Commit

Permalink
commands: add get dataset stats
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-prosser committed Oct 9, 2023
1 parent a69fc08 commit 6663ecf
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Unreleased

- Add ability to get dataset stats
- Show Global Permissions in `get users`
- Upgrade `ordered-float` version, which is exposed in the public crate api.

Expand Down
11 changes: 6 additions & 5 deletions api/src/resources/dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::{
str::FromStr,
};

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct Dataset {
pub id: Id,
pub name: Name,
Expand All @@ -34,6 +34,7 @@ pub struct Dataset {
pub entity_defs: Vec<EntityDef>,
pub label_defs: Vec<LabelDef>,
pub label_groups: Vec<LabelGroup>,
pub num_reviewed: Option<f64>,
}

impl Dataset {
Expand Down Expand Up @@ -248,17 +249,17 @@ pub(crate) struct CreateRequest<'request> {
pub dataset: NewDataset<'request>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub(crate) struct CreateResponse {
pub dataset: Dataset,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub(crate) struct GetAvailableResponse {
pub datasets: Vec<Dataset>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub(crate) struct GetResponse {
pub dataset: Dataset,
}
Expand All @@ -280,7 +281,7 @@ pub(crate) struct UpdateRequest<'request> {
pub dataset: UpdateDataset<'request>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub(crate) struct UpdateResponse {
pub dataset: Dataset,
}
Expand Down
49 changes: 46 additions & 3 deletions cli/src/commands/get/datasets.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use std::collections::HashMap;

use anyhow::{Context, Result};
use reinfer_client::{Client, DatasetIdentifier};
use log::info;
use reinfer_client::{
resources::dataset::StatisticsRequestParams, Client, CommentFilter, DatasetIdentifier,
};
use structopt::StructOpt;

use crate::printer::Printer;
Expand All @@ -9,11 +14,18 @@ pub struct GetDatasetsArgs {
#[structopt(name = "dataset")]
/// If specified, only list this dataset (name or id)
dataset: Option<DatasetIdentifier>,

#[structopt(long = "stats")]
/// Whether to include source statistics in response
include_stats: bool,
}

pub fn get(client: &Client, args: &GetDatasetsArgs, printer: &Printer) -> Result<()> {
let GetDatasetsArgs { dataset } = args;
let datasets = if let Some(dataset) = dataset {
let GetDatasetsArgs {
dataset,
include_stats,
} = args;
let mut datasets = if let Some(dataset) = dataset {
vec![client
.get_dataset(dataset.clone())
.context("Operation to list datasets has failed.")?]
Expand All @@ -26,5 +38,36 @@ pub fn get(client: &Client, args: &GetDatasetsArgs, printer: &Printer) -> Result
});
datasets
};

let mut dataset_stats: HashMap<_, _> = HashMap::new();
if *include_stats {
datasets.iter().try_for_each(|dataset| -> Result<()> {
info!("Getting statistics for dataset {}", dataset.full_name().0);
let stats = client
.get_dataset_statistics(
&dataset.full_name(),
&StatisticsRequestParams {
comment_filter: CommentFilter {
reviewed:Some(reinfer_client::resources::comment::ReviewedFilterEnum::OnlyReviewed),
..Default::default()
},
..Default::default()
},
)
.context("Could not get statistics for dataset")?;

dataset_stats.insert(dataset.id.clone(), stats);
Ok(())
})?;

datasets.iter_mut().for_each(|dataset| {
dataset.num_reviewed = if let Some(stats) = dataset_stats.get(&dataset.id).cloned() {
Some(*stats.num_comments)
} else {
None
}
});
}

printer.print_resources(&datasets)
}
9 changes: 7 additions & 2 deletions cli/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl DisplayTable for Quota {

impl DisplayTable for Dataset {
fn to_table_headers() -> Row {
row![bFg => "Name", "ID", "Updated (UTC)", "Title"]
row![bFg => "Name", "ID", "Updated (UTC)", "Title", "Num Reviewed"]
}

fn to_table_row(&self) -> Row {
Expand All @@ -98,7 +98,12 @@ impl DisplayTable for Dataset {
full_name,
self.id.0,
self.updated_at.format("%Y-%m-%d %H:%M:%S"),
self.title
self.title,
if let Some(num_reviewed) = &self.num_reviewed {
num_reviewed.to_string().as_str().into()
} else {
"none".dimmed()
}
]
}
}
Expand Down

0 comments on commit 6663ecf

Please sign in to comment.