2020-05-15 11:16:09 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
|
|
|
use crate::prelude::*;
|
2020-09-25 05:44:24 +02:00
|
|
|
use crate::utils::suggestions::suggestions;
|
2020-05-15 11:16:09 +02:00
|
|
|
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 {
|
2020-07-14 15:45:19 +02:00
|
|
|
"creates a table grouped by date."
|
2020-05-15 11:16:09 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
group_by_date(args).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-12-18 08:53:49 +01:00
|
|
|
pub async fn group_by_date(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
2020-05-16 05:18:24 +02:00
|
|
|
let name = args.call_info.name_tag.clone();
|
2020-06-08 06:48:10 +02:00
|
|
|
let (
|
|
|
|
GroupByDateArgs {
|
|
|
|
column_name,
|
|
|
|
format,
|
|
|
|
},
|
|
|
|
input,
|
2020-12-18 08:53:49 +01:00
|
|
|
) = args.process().await?;
|
2020-06-08 06:48:10 +02:00
|
|
|
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-07-18 03:12:06 +02:00
|
|
|
let value_result = match (grouper_date, grouper_column) {
|
2020-06-17 19:33:50 +02:00
|
|
|
(Grouper::ByDate(None), GroupByColumn::Name(None)) => {
|
2020-08-04 00:47:19 +02:00
|
|
|
let block = Box::new(move |_, row: &Value| row.format("%Y-%m-%d"));
|
2020-06-17 19:33:50 +02:00
|
|
|
|
2020-08-18 09:00:02 +02:00
|
|
|
nu_data::utils::group(&values, &Some(block), &name)
|
2020-06-08 06:48:10 +02:00
|
|
|
}
|
2020-06-17 19:33:50 +02:00
|
|
|
(Grouper::ByDate(None), GroupByColumn::Name(Some(column_name))) => {
|
2020-07-14 15:45:19 +02:00
|
|
|
let block = Box::new(move |_, row: &Value| {
|
2020-08-03 00:34:33 +02:00
|
|
|
let group_key = row
|
|
|
|
.get_data_by_key(column_name.borrow_spanned())
|
|
|
|
.ok_or_else(|| suggestions(column_name.borrow_tagged(), &row));
|
2020-06-17 19:33:50 +02:00
|
|
|
|
2020-08-04 00:47:19 +02:00
|
|
|
group_key?.format("%Y-%m-%d")
|
2020-06-17 19:33:50 +02:00
|
|
|
});
|
|
|
|
|
2020-08-18 09:00:02 +02:00
|
|
|
nu_data::utils::group(&values, &Some(block), &name)
|
2020-06-17 19:33:50 +02:00
|
|
|
}
|
|
|
|
(Grouper::ByDate(Some(fmt)), GroupByColumn::Name(None)) => {
|
2020-07-14 15:45:19 +02:00
|
|
|
let block = Box::new(move |_, row: &Value| row.format(&fmt));
|
2020-06-17 19:33:50 +02:00
|
|
|
|
2020-08-18 09:00:02 +02:00
|
|
|
nu_data::utils::group(&values, &Some(block), &name)
|
2020-06-17 19:33:50 +02:00
|
|
|
}
|
|
|
|
(Grouper::ByDate(Some(fmt)), GroupByColumn::Name(Some(column_name))) => {
|
2020-07-14 15:45:19 +02:00
|
|
|
let block = Box::new(move |_, row: &Value| {
|
2020-08-03 00:34:33 +02:00
|
|
|
let group_key = row
|
|
|
|
.get_data_by_key(column_name.borrow_spanned())
|
|
|
|
.ok_or_else(|| suggestions(column_name.borrow_tagged(), &row));
|
2020-06-17 19:33:50 +02:00
|
|
|
|
|
|
|
group_key?.format(&fmt)
|
|
|
|
});
|
|
|
|
|
2020-08-18 09:00:02 +02:00
|
|
|
nu_data::utils::group(&values, &Some(block), &name)
|
2020-05-15 11:16:09 +02:00
|
|
|
}
|
2020-07-18 03:12:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(OutputStream::one(ReturnSuccess::value(value_result?)))
|
2020-06-08 06:48:10 +02:00
|
|
|
}
|
2020-05-15 11:16:09 +02:00
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::GroupByDate;
|
2020-10-03 16:06:02 +02:00
|
|
|
use super::ShellError;
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[test]
|
2020-10-03 16:06:02 +02:00
|
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
2020-05-18 14:56:01 +02:00
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
2020-10-03 16:06:02 +02:00
|
|
|
Ok(test_examples(GroupByDate {})?)
|
2020-05-18 14:56:01 +02:00
|
|
|
}
|
|
|
|
}
|