2020-05-15 11:16:09 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
|
|
|
use crate::prelude::*;
|
|
|
|
use nu_errors::ShellError;
|
2020-06-17 19:33:50 +02:00
|
|
|
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
|
2020-05-15 11:16:09 +02:00
|
|
|
use nu_source::Tagged;
|
|
|
|
|
|
|
|
pub struct GroupByDate;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct GroupByDateArgs {
|
|
|
|
column_name: Option<Tagged<String>>,
|
|
|
|
format: Option<Tagged<String>>,
|
|
|
|
}
|
|
|
|
|
2020-05-29 10:22:52 +02:00
|
|
|
#[async_trait]
|
2020-05-15 11:16:09 +02:00
|
|
|
impl WholeStreamCommand for GroupByDate {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"group-by date"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("group-by date")
|
|
|
|
.optional(
|
|
|
|
"column_name",
|
|
|
|
SyntaxShape::String,
|
|
|
|
"the name of the column to group by",
|
|
|
|
)
|
|
|
|
.named(
|
|
|
|
"format",
|
|
|
|
SyntaxShape::String,
|
|
|
|
"Specify date and time formatting",
|
|
|
|
Some('f'),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Creates a new table with the data from the table rows grouped by the column given."
|
|
|
|
}
|
|
|
|
|
2020-05-29 10:22:52 +02:00
|
|
|
async fn run(
|
2020-05-15 11:16:09 +02:00
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2020-06-08 06:48:10 +02:00
|
|
|
group_by_date(args, registry).await
|
2020-05-15 11:16:09 +02:00
|
|
|
}
|
|
|
|
|
2020-05-18 14:56:01 +02:00
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![Example {
|
2020-05-15 11:16:09 +02:00
|
|
|
description: "Group files by type",
|
2020-05-18 14:56:01 +02:00
|
|
|
example: "ls | group-by date --format '%d/%m/%Y'",
|
|
|
|
result: None,
|
2020-05-15 11:16:09 +02:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Grouper {
|
2020-06-17 19:33:50 +02:00
|
|
|
ByDate(Option<Tagged<String>>),
|
|
|
|
}
|
|
|
|
|
|
|
|
enum GroupByColumn {
|
|
|
|
Name(Option<Tagged<String>>),
|
2020-05-15 11:16:09 +02:00
|
|
|
}
|
|
|
|
|
2020-06-08 06:48:10 +02:00
|
|
|
pub async fn group_by_date(
|
2020-05-16 05:18:24 +02:00
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
2020-05-15 11:16:09 +02:00
|
|
|
) -> Result<OutputStream, ShellError> {
|
2020-05-16 05:18:24 +02:00
|
|
|
let registry = registry.clone();
|
|
|
|
let name = args.call_info.name_tag.clone();
|
2020-06-08 06:48:10 +02:00
|
|
|
let (
|
|
|
|
GroupByDateArgs {
|
|
|
|
column_name,
|
|
|
|
format,
|
|
|
|
},
|
|
|
|
input,
|
|
|
|
) = args.process(®istry).await?;
|
|
|
|
let values: Vec<Value> = input.collect().await;
|
|
|
|
|
|
|
|
if values.is_empty() {
|
|
|
|
Err(ShellError::labeled_error(
|
|
|
|
"Expected table from pipeline",
|
|
|
|
"requires a table input",
|
|
|
|
name,
|
|
|
|
))
|
|
|
|
} else {
|
2020-06-17 19:33:50 +02:00
|
|
|
let values = UntaggedValue::table(&values).into_value(&name);
|
|
|
|
|
|
|
|
let grouper_column = if let Some(column_name) = column_name {
|
|
|
|
GroupByColumn::Name(Some(column_name))
|
|
|
|
} else {
|
|
|
|
GroupByColumn::Name(None)
|
|
|
|
};
|
|
|
|
|
|
|
|
let grouper_date = if let Some(date_format) = format {
|
|
|
|
Grouper::ByDate(Some(date_format))
|
2020-05-15 11:16:09 +02:00
|
|
|
} else {
|
2020-06-08 06:48:10 +02:00
|
|
|
Grouper::ByDate(None)
|
|
|
|
};
|
|
|
|
|
2020-06-17 19:33:50 +02:00
|
|
|
match (grouper_date, grouper_column) {
|
|
|
|
(Grouper::ByDate(None), GroupByColumn::Name(None)) => {
|
|
|
|
let block = Box::new(move |row: &Value| row.format("%Y-%b-%d"));
|
|
|
|
|
|
|
|
match crate::utils::data::group(&values, &Some(block), &name) {
|
2020-06-08 06:48:10 +02:00
|
|
|
Ok(grouped) => Ok(OutputStream::one(ReturnSuccess::value(grouped))),
|
|
|
|
Err(err) => Err(err),
|
2020-05-15 11:16:09 +02:00
|
|
|
}
|
2020-06-08 06:48:10 +02:00
|
|
|
}
|
2020-06-17 19:33:50 +02:00
|
|
|
(Grouper::ByDate(None), GroupByColumn::Name(Some(column_name))) => {
|
|
|
|
let block = Box::new(move |row: &Value| {
|
|
|
|
let group_key = match row.get_data_by_key(column_name.borrow_spanned()) {
|
|
|
|
Some(group_key) => Ok(group_key),
|
|
|
|
None => Err(suggestions(column_name.borrow_tagged(), &row)),
|
|
|
|
};
|
|
|
|
|
|
|
|
group_key?.format("%Y-%b-%d")
|
|
|
|
});
|
|
|
|
|
|
|
|
match crate::utils::data::group(&values, &Some(block), &name) {
|
|
|
|
Ok(grouped) => Ok(OutputStream::one(ReturnSuccess::value(grouped))),
|
|
|
|
Err(err) => Err(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(Grouper::ByDate(Some(fmt)), GroupByColumn::Name(None)) => {
|
|
|
|
let block = Box::new(move |row: &Value| row.format(&fmt));
|
|
|
|
|
|
|
|
match crate::utils::data::group(&values, &Some(block), &name) {
|
|
|
|
Ok(grouped) => Ok(OutputStream::one(ReturnSuccess::value(grouped))),
|
|
|
|
Err(err) => Err(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(Grouper::ByDate(Some(fmt)), GroupByColumn::Name(Some(column_name))) => {
|
|
|
|
let block = Box::new(move |row: &Value| {
|
|
|
|
let group_key = match row.get_data_by_key(column_name.borrow_spanned()) {
|
|
|
|
Some(group_key) => Ok(group_key),
|
|
|
|
None => Err(suggestions(column_name.borrow_tagged(), &row)),
|
|
|
|
};
|
|
|
|
|
|
|
|
group_key?.format(&fmt)
|
|
|
|
});
|
|
|
|
|
|
|
|
match crate::utils::data::group(&values, &Some(block), &name) {
|
2020-06-08 06:48:10 +02:00
|
|
|
Ok(grouped) => Ok(OutputStream::one(ReturnSuccess::value(grouped))),
|
|
|
|
Err(err) => Err(err),
|
2020-05-15 11:16:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-08 06:48:10 +02:00
|
|
|
}
|
2020-05-15 11:16:09 +02:00
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
|
2020-06-17 19:33:50 +02:00
|
|
|
pub fn suggestions(tried: Tagged<&str>, for_value: &Value) -> ShellError {
|
|
|
|
let possibilities = for_value.data_descriptors();
|
|
|
|
|
|
|
|
let mut possible_matches: Vec<_> = possibilities
|
|
|
|
.iter()
|
|
|
|
.map(|x| (natural::distance::levenshtein_distance(x, &tried), x))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
possible_matches.sort();
|
|
|
|
|
|
|
|
if !possible_matches.is_empty() {
|
|
|
|
ShellError::labeled_error(
|
|
|
|
"Unknown column",
|
|
|
|
format!("did you mean '{}'?", possible_matches[0].1),
|
|
|
|
tried.tag(),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
ShellError::labeled_error(
|
|
|
|
"Unknown column",
|
|
|
|
"row does not contain this column",
|
|
|
|
tried.tag(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 14:56:01 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::GroupByDate;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn examples_work_as_expected() {
|
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
|
|
|
test_examples(GroupByDate {})
|
|
|
|
}
|
|
|
|
}
|