mirror of
https://github.com/nushell/nushell
synced 2025-01-28 21:05:48 +00:00
Extracted grouping by date to it's own subcommand. (#1792)
This commit is contained in:
parent
5fbe5cf785
commit
fc8ee8e4b9
6 changed files with 129 additions and 62 deletions
|
@ -298,6 +298,7 @@ pub fn create_default_context(
|
||||||
whole_stream_command(Prepend),
|
whole_stream_command(Prepend),
|
||||||
whole_stream_command(SortBy),
|
whole_stream_command(SortBy),
|
||||||
whole_stream_command(GroupBy),
|
whole_stream_command(GroupBy),
|
||||||
|
whole_stream_command(GroupByDate),
|
||||||
whole_stream_command(First),
|
whole_stream_command(First),
|
||||||
whole_stream_command(Last),
|
whole_stream_command(Last),
|
||||||
whole_stream_command(Nth),
|
whole_stream_command(Nth),
|
||||||
|
|
|
@ -50,6 +50,7 @@ pub(crate) mod from_xml;
|
||||||
pub(crate) mod from_yaml;
|
pub(crate) mod from_yaml;
|
||||||
pub(crate) mod get;
|
pub(crate) mod get;
|
||||||
pub(crate) mod group_by;
|
pub(crate) mod group_by;
|
||||||
|
pub(crate) mod group_by_date;
|
||||||
pub(crate) mod headers;
|
pub(crate) mod headers;
|
||||||
pub(crate) mod help;
|
pub(crate) mod help;
|
||||||
pub(crate) mod histogram;
|
pub(crate) mod histogram;
|
||||||
|
@ -178,6 +179,7 @@ pub(crate) use from_yaml::FromYAML;
|
||||||
pub(crate) use from_yaml::FromYML;
|
pub(crate) use from_yaml::FromYML;
|
||||||
pub(crate) use get::Get;
|
pub(crate) use get::Get;
|
||||||
pub(crate) use group_by::GroupBy;
|
pub(crate) use group_by::GroupBy;
|
||||||
|
pub(crate) use group_by_date::GroupByDate;
|
||||||
pub(crate) use headers::Headers;
|
pub(crate) use headers::Headers;
|
||||||
pub(crate) use help::Help;
|
pub(crate) use help::Help;
|
||||||
pub(crate) use histogram::Histogram;
|
pub(crate) use histogram::Histogram;
|
||||||
|
|
|
@ -8,9 +8,7 @@ pub struct GroupBy;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct GroupByArgs {
|
pub struct GroupByArgs {
|
||||||
column_name: Tagged<String>,
|
column_name: Option<Tagged<String>>,
|
||||||
date: Tagged<bool>,
|
|
||||||
format: Option<Tagged<String>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WholeStreamCommand for GroupBy {
|
impl WholeStreamCommand for GroupBy {
|
||||||
|
@ -19,19 +17,11 @@ impl WholeStreamCommand for GroupBy {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build("group-by")
|
Signature::build("group-by").optional(
|
||||||
.required(
|
"column_name",
|
||||||
"column_name",
|
SyntaxShape::String,
|
||||||
SyntaxShape::String,
|
"the name of the column to group by",
|
||||||
"the name of the column to group by",
|
)
|
||||||
)
|
|
||||||
.named(
|
|
||||||
"format",
|
|
||||||
SyntaxShape::String,
|
|
||||||
"Specify date and time formatting",
|
|
||||||
Some('f'),
|
|
||||||
)
|
|
||||||
.switch("date", "by date", Some('d'))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
|
@ -54,17 +44,8 @@ impl WholeStreamCommand for GroupBy {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Grouper {
|
|
||||||
Default,
|
|
||||||
ByDate(Option<String>),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn group_by(
|
pub fn group_by(
|
||||||
GroupByArgs {
|
GroupByArgs { column_name }: GroupByArgs,
|
||||||
column_name,
|
|
||||||
date,
|
|
||||||
format,
|
|
||||||
}: GroupByArgs,
|
|
||||||
RunnableContext { input, name, .. }: RunnableContext,
|
RunnableContext { input, name, .. }: RunnableContext,
|
||||||
) -> Result<OutputStream, ShellError> {
|
) -> Result<OutputStream, ShellError> {
|
||||||
let stream = async_stream! {
|
let stream = async_stream! {
|
||||||
|
@ -74,42 +55,15 @@ pub fn group_by(
|
||||||
yield Err(ShellError::labeled_error(
|
yield Err(ShellError::labeled_error(
|
||||||
"Expected table from pipeline",
|
"Expected table from pipeline",
|
||||||
"requires a table input",
|
"requires a table input",
|
||||||
column_name.span()
|
name
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
let grouper = if let Tagged { item: true, tag } = date {
|
match crate::utils::data::group(column_name, &values, None, &name) {
|
||||||
if let Some(Tagged { item: fmt, tag }) = format {
|
Ok(grouped) => yield ReturnSuccess::value(grouped),
|
||||||
Grouper::ByDate(Some(fmt))
|
Err(err) => yield Err(err),
|
||||||
} else {
|
|
||||||
Grouper::ByDate(None)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Grouper::Default
|
|
||||||
};
|
|
||||||
|
|
||||||
match grouper {
|
|
||||||
Grouper::Default => {
|
|
||||||
match crate::utils::data::group(column_name, &values, None, &name) {
|
|
||||||
Ok(grouped) => yield ReturnSuccess::value(grouped),
|
|
||||||
Err(err) => yield Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Grouper::ByDate(None) => {
|
|
||||||
match crate::utils::data::group(column_name, &values, Some(Box::new(|row: &Value| row.format("%Y-%b-%d"))), &name) {
|
|
||||||
Ok(grouped) => yield ReturnSuccess::value(grouped),
|
|
||||||
Err(err) => yield Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Grouper::ByDate(Some(fmt)) => {
|
|
||||||
match crate::utils::data::group(column_name, &values, Some(Box::new(move |row: &Value| {
|
|
||||||
row.format(&fmt)
|
|
||||||
})), &name) {
|
|
||||||
Ok(grouped) => yield ReturnSuccess::value(grouped),
|
|
||||||
Err(err) => yield Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -121,7 +75,7 @@ pub fn group(
|
||||||
values: Vec<Value>,
|
values: Vec<Value>,
|
||||||
tag: impl Into<Tag>,
|
tag: impl Into<Tag>,
|
||||||
) -> Result<Value, ShellError> {
|
) -> Result<Value, ShellError> {
|
||||||
crate::utils::data::group(column_name.clone(), &values, None, tag)
|
crate::utils::data::group(Some(column_name.clone()), &values, None, tag)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
103
crates/nu-cli/src/commands/group_by_date.rs
Normal file
103
crates/nu-cli/src/commands/group_by_date.rs
Normal file
|
@ -0,0 +1,103 @@
|
||||||
|
use crate::commands::WholeStreamCommand;
|
||||||
|
use crate::prelude::*;
|
||||||
|
use nu_errors::ShellError;
|
||||||
|
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, Value};
|
||||||
|
use nu_source::Tagged;
|
||||||
|
|
||||||
|
pub struct GroupByDate;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct GroupByDateArgs {
|
||||||
|
column_name: Option<Tagged<String>>,
|
||||||
|
format: Option<Tagged<String>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
args: CommandArgs,
|
||||||
|
registry: &CommandRegistry,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
args.process(registry, group_by_date)?.run()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> &[Example] {
|
||||||
|
&[Example {
|
||||||
|
description: "Group files by type",
|
||||||
|
example: "ls | group-by date --fmt '%d/%m/%Y'",
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Grouper {
|
||||||
|
ByDate(Option<String>),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn group_by_date(
|
||||||
|
GroupByDateArgs {
|
||||||
|
column_name,
|
||||||
|
format,
|
||||||
|
}: GroupByDateArgs,
|
||||||
|
RunnableContext { input, name, .. }: RunnableContext,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
let stream = async_stream! {
|
||||||
|
let values: Vec<Value> = input.collect().await;
|
||||||
|
|
||||||
|
if values.is_empty() {
|
||||||
|
yield Err(ShellError::labeled_error(
|
||||||
|
"Expected table from pipeline",
|
||||||
|
"requires a table input",
|
||||||
|
name
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
|
||||||
|
let grouper = if let Some(Tagged { item: fmt, tag }) = format {
|
||||||
|
Grouper::ByDate(Some(fmt))
|
||||||
|
} else {
|
||||||
|
Grouper::ByDate(None)
|
||||||
|
};
|
||||||
|
|
||||||
|
match grouper {
|
||||||
|
Grouper::ByDate(None) => {
|
||||||
|
match crate::utils::data::group(column_name, &values, Some(Box::new(|row: &Value| row.format("%Y-%b-%d"))), &name) {
|
||||||
|
Ok(grouped) => yield ReturnSuccess::value(grouped),
|
||||||
|
Err(err) => yield Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Grouper::ByDate(Some(fmt)) => {
|
||||||
|
match crate::utils::data::group(column_name, &values, Some(Box::new(move |row: &Value| {
|
||||||
|
row.format(&fmt)
|
||||||
|
})), &name) {
|
||||||
|
Ok(grouped) => yield ReturnSuccess::value(grouped),
|
||||||
|
Err(err) => yield Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(stream.to_output_stream())
|
||||||
|
}
|
|
@ -1,12 +1,12 @@
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{TaggedDictBuilder, UntaggedValue, Value};
|
use nu_protocol::{TaggedDictBuilder, UntaggedValue, Value};
|
||||||
use nu_source::{Tag, Tagged};
|
use nu_source::{Tag, Tagged, TaggedItem};
|
||||||
use nu_value_ext::{as_string, get_data_by_key};
|
use nu_value_ext::{as_string, get_data_by_key};
|
||||||
|
|
||||||
#[allow(clippy::type_complexity)]
|
#[allow(clippy::type_complexity)]
|
||||||
pub fn group(
|
pub fn group(
|
||||||
column_name: Tagged<String>,
|
column_name: Option<Tagged<String>>,
|
||||||
values: &[Value],
|
values: &[Value],
|
||||||
grouper: Option<Box<dyn Fn(&Value) -> Result<String, ShellError> + Send>>,
|
grouper: Option<Box<dyn Fn(&Value) -> Result<String, ShellError> + Send>>,
|
||||||
tag: impl Into<Tag>,
|
tag: impl Into<Tag>,
|
||||||
|
@ -16,7 +16,11 @@ pub fn group(
|
||||||
let mut groups: IndexMap<String, Vec<Value>> = IndexMap::new();
|
let mut groups: IndexMap<String, Vec<Value>> = IndexMap::new();
|
||||||
|
|
||||||
for value in values {
|
for value in values {
|
||||||
let group_key = get_data_by_key(&value, column_name.borrow_spanned());
|
let group_key = if let Some(ref column_name) = column_name {
|
||||||
|
get_data_by_key(&value, column_name.borrow_spanned())
|
||||||
|
} else {
|
||||||
|
Some(value.clone())
|
||||||
|
};
|
||||||
|
|
||||||
if let Some(group_key) = group_key {
|
if let Some(group_key) = group_key {
|
||||||
let group_key = if let Some(ref grouper) = grouper {
|
let group_key = if let Some(ref grouper) = grouper {
|
||||||
|
@ -27,6 +31,8 @@ pub fn group(
|
||||||
let group = groups.entry(group_key?).or_insert(vec![]);
|
let group = groups.entry(group_key?).or_insert(vec![]);
|
||||||
group.push((*value).clone());
|
group.push((*value).clone());
|
||||||
} else {
|
} else {
|
||||||
|
let column_name = column_name.unwrap_or_else(|| String::from("").tagged(&tag));
|
||||||
|
|
||||||
let possibilities = value.data_descriptors();
|
let possibilities = value.data_descriptors();
|
||||||
|
|
||||||
let mut possible_matches: Vec<_> = possibilities
|
let mut possible_matches: Vec<_> = possibilities
|
||||||
|
|
|
@ -392,6 +392,7 @@ pub fn as_path_member(value: &Value) -> Result<PathMember, ShellError> {
|
||||||
pub fn as_string(value: &Value) -> Result<String, ShellError> {
|
pub fn as_string(value: &Value) -> Result<String, ShellError> {
|
||||||
match &value.value {
|
match &value.value {
|
||||||
UntaggedValue::Primitive(Primitive::String(s)) => Ok(s.clone()),
|
UntaggedValue::Primitive(Primitive::String(s)) => Ok(s.clone()),
|
||||||
|
UntaggedValue::Primitive(Primitive::Date(dt)) => Ok(dt.format("%Y-%b-%d").to_string()),
|
||||||
UntaggedValue::Primitive(Primitive::Boolean(x)) => Ok(format!("{}", x)),
|
UntaggedValue::Primitive(Primitive::Boolean(x)) => Ok(format!("{}", x)),
|
||||||
UntaggedValue::Primitive(Primitive::Decimal(x)) => Ok(format!("{}", x)),
|
UntaggedValue::Primitive(Primitive::Decimal(x)) => Ok(format!("{}", x)),
|
||||||
UntaggedValue::Primitive(Primitive::Int(x)) => Ok(format!("{}", x)),
|
UntaggedValue::Primitive(Primitive::Int(x)) => Ok(format!("{}", x)),
|
||||||
|
|
Loading…
Reference in a new issue