mirror of
https://github.com/nushell/nushell.git
synced 2025-08-10 08:49:33 +02:00
This is part of on-going work with capabilities when working with
tables and able to work with them for data processing & viewing purposes. At the moment, certain ways to process said tables we are able to view a histogram of a given column. As usage matures, we may find certain core commands that could be used ergonomically when working with tables on Nu.
This commit is contained in:
@ -1,10 +1,10 @@
|
||||
use crate::commands::WholeStreamCommand;
|
||||
use crate::commands::evaluate_by::evaluate;
|
||||
use crate::commands::group_by::group;
|
||||
use crate::commands::map_max_by::map_max;
|
||||
use crate::commands::reduce_by::reduce;
|
||||
use crate::commands::t_sort_by::columns_sorted;
|
||||
use crate::commands::t_sort_by::t_sort;
|
||||
use crate::commands::evaluate_by::evaluate;
|
||||
use crate::commands::reduce_by::reduce;
|
||||
use crate::commands::map_max_by::map_max;
|
||||
use crate::commands::WholeStreamCommand;
|
||||
use crate::data::TaggedDictBuilder;
|
||||
use crate::errors::ShellError;
|
||||
use crate::prelude::*;
|
||||
@ -15,6 +15,7 @@ pub struct Histogram;
|
||||
#[derive(Deserialize)]
|
||||
pub struct HistogramArgs {
|
||||
column_name: Tagged<String>,
|
||||
rest: Vec<Tagged<String>>,
|
||||
}
|
||||
|
||||
impl WholeStreamCommand for Histogram {
|
||||
@ -23,11 +24,16 @@ impl WholeStreamCommand for Histogram {
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("histogram").required(
|
||||
"column_name",
|
||||
SyntaxShape::String,
|
||||
"the name of the column to graph by",
|
||||
)
|
||||
Signature::build("histogram")
|
||||
.required(
|
||||
"column_name",
|
||||
SyntaxShape::String,
|
||||
"the name of the column to graph by",
|
||||
)
|
||||
.rest(
|
||||
SyntaxShape::Member,
|
||||
"column name to give the histogram's frequency column",
|
||||
)
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
@ -44,7 +50,7 @@ impl WholeStreamCommand for Histogram {
|
||||
}
|
||||
|
||||
pub fn histogram(
|
||||
HistogramArgs { column_name }: HistogramArgs,
|
||||
HistogramArgs { column_name, rest }: HistogramArgs,
|
||||
RunnableContext { input, name, .. }: RunnableContext,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
let stream = async_stream! {
|
||||
@ -68,13 +74,24 @@ pub fn histogram(
|
||||
|
||||
let mut idx = 0;
|
||||
|
||||
let column_names_supplied: Vec<_> = rest.iter().map(|f| f.item.clone()).collect();
|
||||
|
||||
let frequency_column_name = if column_names_supplied.is_empty() {
|
||||
"frecuency".to_string()
|
||||
} else {
|
||||
column_names_supplied[0].clone()
|
||||
};
|
||||
|
||||
let column = (*column_name).clone();
|
||||
|
||||
if let Tagged { item: Value::Table(start), .. } = datasets.get(0).unwrap() {
|
||||
for percentage in start.into_iter() {
|
||||
|
||||
let mut fact = TaggedDictBuilder::new(&name);
|
||||
fact.insert_tagged("committer", group_labels.get(idx).unwrap().clone());
|
||||
fact.insert_tagged(&column, group_labels.get(idx).unwrap().clone());
|
||||
|
||||
if let Tagged { item: Value::Primitive(Primitive::Int(ref num)), .. } = percentage.clone() {
|
||||
fact.insert("activity", std::iter::repeat("*").take(num.to_i32().unwrap() as usize).collect::<String>());
|
||||
fact.insert(&frequency_column_name, std::iter::repeat("*").take(num.to_i32().unwrap() as usize).collect::<String>());
|
||||
}
|
||||
|
||||
idx = idx + 1;
|
||||
@ -104,38 +121,36 @@ fn percentages(
|
||||
} => {
|
||||
let datasets: Vec<_> = datasets
|
||||
.into_iter()
|
||||
.map(|subsets| {
|
||||
match subsets {
|
||||
Tagged {
|
||||
item: Value::Table(data),
|
||||
..
|
||||
} => {
|
||||
let data = data
|
||||
.into_iter()
|
||||
.map(|d| match d {
|
||||
Tagged {
|
||||
item: Value::Primitive(Primitive::Int(n)),
|
||||
..
|
||||
} => {
|
||||
let max = match max {
|
||||
Tagged {
|
||||
item: Value::Primitive(Primitive::Int(ref maxima)),
|
||||
..
|
||||
} => maxima.to_i32().unwrap(),
|
||||
_ => 0,
|
||||
};
|
||||
.map(|subsets| match subsets {
|
||||
Tagged {
|
||||
item: Value::Table(data),
|
||||
..
|
||||
} => {
|
||||
let data = data
|
||||
.into_iter()
|
||||
.map(|d| match d {
|
||||
Tagged {
|
||||
item: Value::Primitive(Primitive::Int(n)),
|
||||
..
|
||||
} => {
|
||||
let max = match max {
|
||||
Tagged {
|
||||
item: Value::Primitive(Primitive::Int(ref maxima)),
|
||||
..
|
||||
} => maxima.to_i32().unwrap(),
|
||||
_ => 0,
|
||||
};
|
||||
|
||||
let n = { n.to_i32().unwrap() * 100 / max };
|
||||
let n = { n.to_i32().unwrap() * 100 / max };
|
||||
|
||||
Value::number(n).tagged(&tag)
|
||||
}
|
||||
_ => Value::number(0).tagged(&tag),
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
Value::Table(data).tagged(&tag)
|
||||
}
|
||||
_ => Value::Table(vec![]).tagged(&tag),
|
||||
Value::number(n).tagged(&tag)
|
||||
}
|
||||
_ => Value::number(0).tagged(&tag),
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
Value::Table(data).tagged(&tag)
|
||||
}
|
||||
_ => Value::Table(vec![]).tagged(&tag),
|
||||
})
|
||||
.collect();
|
||||
|
||||
|
@ -81,29 +81,27 @@ pub fn map_max(
|
||||
} => {
|
||||
let datasets: Vec<_> = datasets
|
||||
.into_iter()
|
||||
.map(|subsets| {
|
||||
match subsets {
|
||||
Tagged {
|
||||
item: Value::Table(data),
|
||||
..
|
||||
} => {
|
||||
let data = data.into_iter().fold(0, |acc, value| match value {
|
||||
Tagged {
|
||||
item: Value::Primitive(Primitive::Int(n)),
|
||||
..
|
||||
} => {
|
||||
if n.to_i32().unwrap() > acc {
|
||||
n.to_i32().unwrap()
|
||||
} else {
|
||||
acc
|
||||
}
|
||||
.map(|subsets| match subsets {
|
||||
Tagged {
|
||||
item: Value::Table(data),
|
||||
..
|
||||
} => {
|
||||
let data = data.into_iter().fold(0, |acc, value| match value {
|
||||
Tagged {
|
||||
item: Value::Primitive(Primitive::Int(n)),
|
||||
..
|
||||
} => {
|
||||
if n.to_i32().unwrap() > acc {
|
||||
n.to_i32().unwrap()
|
||||
} else {
|
||||
acc
|
||||
}
|
||||
_ => acc,
|
||||
});
|
||||
Value::number(data).tagged(&tag)
|
||||
}
|
||||
_ => Value::number(0).tagged(&tag),
|
||||
}
|
||||
_ => acc,
|
||||
});
|
||||
Value::number(data).tagged(&tag)
|
||||
}
|
||||
_ => Value::number(0).tagged(&tag),
|
||||
})
|
||||
.collect();
|
||||
|
||||
|
@ -57,25 +57,25 @@ fn t_sort_by(
|
||||
RunnableContext { input, name, .. }: RunnableContext,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
Ok(OutputStream::new(async_stream! {
|
||||
let values: Vec<Tagged<Value>> = input.values.collect().await;
|
||||
let values: Vec<Tagged<Value>> = input.values.collect().await;
|
||||
|
||||
let column_grouped_by_name = if let Some(grouped_by) = group_by {
|
||||
Some(grouped_by.item().clone())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let column_grouped_by_name = if let Some(grouped_by) = group_by {
|
||||
Some(grouped_by.item().clone())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if show_columns {
|
||||
for label in columns_sorted(column_grouped_by_name, &values[0], &name).iter() {
|
||||
yield ReturnSuccess::value(label.clone());
|
||||
}
|
||||
} else {
|
||||
match t_sort(column_grouped_by_name, None, &values[0], name) {
|
||||
Ok(sorted) => yield ReturnSuccess::value(sorted),
|
||||
Err(err) => yield Err(err)
|
||||
}
|
||||
if show_columns {
|
||||
for label in columns_sorted(column_grouped_by_name, &values[0], &name).iter() {
|
||||
yield ReturnSuccess::value(label.clone());
|
||||
}
|
||||
}))
|
||||
} else {
|
||||
match t_sort(column_grouped_by_name, None, &values[0], name) {
|
||||
Ok(sorted) => yield ReturnSuccess::value(sorted),
|
||||
Err(err) => yield Err(err)
|
||||
}
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn columns_sorted(
|
||||
@ -125,7 +125,7 @@ pub fn columns_sorted(
|
||||
|
||||
keys.into_iter().map(|k| k.tagged(&origin_tag)).collect()
|
||||
}
|
||||
_ => vec![Value::string("default").tagged(&origin_tag)]
|
||||
_ => vec![Value::string("default").tagged(&origin_tag)],
|
||||
}
|
||||
}
|
||||
|
||||
@ -238,7 +238,7 @@ mod tests {
|
||||
use crate::data::meta::*;
|
||||
use crate::Value;
|
||||
use indexmap::IndexMap;
|
||||
|
||||
|
||||
fn string(input: impl Into<String>) -> Tagged<Value> {
|
||||
Value::string(input.into()).tagged_unknown()
|
||||
}
|
||||
@ -305,7 +305,7 @@ mod tests {
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn sorts_the_tables() {
|
||||
let group_by = String::from("date");
|
||||
|
Reference in New Issue
Block a user