Files
improvise/src/import/wizard.rs
Edward Langley cc6504e9b6 test(import): add tests for label field import behavior
Add two new tests for label field functionality:
- label_fields_imported_as_label_category_coords: Verifies that
  high-cardinality fields (>20 distinct values) are classified as
  Label kind, default to accepted, and are stored as category coords
- label_category_defaults_to_none_axis: Verifies that label categories
  default to Axis::None in the active view

Co-Authored-By: fiddlerwoaroof/git-smart-commit (unsloth/Qwen3.5-35B-A3B-GGUF:Q5_K_M)
2026-04-06 08:58:23 -07:00

774 lines
28 KiB
Rust

use anyhow::{anyhow, Result};
use serde_json::Value;
use super::analyzer::{
analyze_records, extract_array_at_path, extract_date_component, find_array_paths,
DateComponent, FieldKind, FieldProposal,
};
use crate::formula::parse_formula;
use crate::model::cell::{CellKey, CellValue};
use crate::model::Model;
// ── Pipeline (no UI state) ────────────────────────────────────────────────────
/// Pure data + logic for turning a JSON value into a Model.
/// No cursor, no display messages — those live in [`ImportWizard`].
#[derive(Debug)]
pub struct ImportPipeline {
pub raw: Value,
pub array_paths: Vec<String>,
pub selected_path: String,
pub records: Vec<Value>,
pub proposals: Vec<FieldProposal>,
pub model_name: String,
/// Raw formula strings to add to the model (e.g., "Profit = Revenue - Cost").
pub formulas: Vec<String>,
}
impl ImportPipeline {
pub fn new(raw: Value) -> Self {
let array_paths = find_array_paths(&raw);
let mut pipeline = Self {
raw: raw.clone(),
array_paths,
selected_path: String::new(),
records: vec![],
proposals: vec![],
model_name: "Imported Model".to_string(),
formulas: vec![],
};
// Auto-select if root is an array or there is exactly one candidate path.
if raw.is_array() {
pipeline.select_path("");
} else if pipeline.array_paths.len() == 1 {
let path = pipeline.array_paths[0].clone();
pipeline.select_path(&path);
}
pipeline
}
pub fn select_path(&mut self, path: &str) {
self.selected_path = path.to_string();
if let Some(arr) = extract_array_at_path(&self.raw, path) {
self.records = arr.clone();
self.proposals = analyze_records(&self.records);
}
}
pub fn needs_path_selection(&self) -> bool {
self.records.is_empty() && self.raw.is_object() && self.array_paths.len() != 1
}
pub fn preview_summary(&self) -> String {
match &self.raw {
Value::Array(arr) => format!(
"Array of {} records. Sample keys: {}",
arr.len(),
arr.first()
.and_then(|r| r.as_object())
.map(|m| m.keys().take(5).cloned().collect::<Vec<_>>().join(", "))
.unwrap_or_default()
),
Value::Object(map) => format!(
"Object with {} top-level keys: {}",
map.len(),
map.keys().take(10).cloned().collect::<Vec<_>>().join(", ")
),
_ => "Unknown JSON structure".to_string(),
}
}
/// Build a Model from the current proposals. Pure — no side effects.
pub fn build_model(&self) -> Result<Model> {
let categories: Vec<&FieldProposal> = self
.proposals
.iter()
.filter(|p| {
p.accepted && matches!(p.kind, FieldKind::Category | FieldKind::TimeCategory)
})
.collect();
let measures: Vec<&FieldProposal> = self
.proposals
.iter()
.filter(|p| p.accepted && p.kind == FieldKind::Measure)
.collect();
let labels: Vec<&FieldProposal> = self
.proposals
.iter()
.filter(|p| p.accepted && p.kind == FieldKind::Label)
.collect();
if categories.is_empty() {
return Err(anyhow!("At least one category must be accepted"));
}
// Collect date component extractions: (field_name, format, component, derived_cat_name)
let date_extractions: Vec<(&str, &str, DateComponent, String)> = self
.proposals
.iter()
.filter(|p| {
p.accepted
&& p.kind == FieldKind::TimeCategory
&& p.date_format.is_some()
&& !p.date_components.is_empty()
})
.flat_map(|p| {
let fmt = p.date_format.as_deref().unwrap();
p.date_components.iter().map(move |comp| {
let suffix = match comp {
DateComponent::Year => "Year",
DateComponent::Month => "Month",
DateComponent::Quarter => "Quarter",
};
let derived_name = format!("{}_{}", p.field, suffix);
(p.field.as_str(), fmt, *comp, derived_name)
})
})
.collect();
let mut model = Model::new(&self.model_name);
for cat_proposal in &categories {
model.add_category(&cat_proposal.field)?;
if let Some(cat) = model.category_mut(&cat_proposal.field) {
for val in &cat_proposal.distinct_values {
cat.add_item(val);
}
}
}
// Create derived date-component categories
for (_, _, _, ref derived_name) in &date_extractions {
model.add_category(derived_name)?;
}
// Create label categories (stored but not pivoted by default)
for lab in &labels {
model.add_label_category(&lab.field)?;
}
if !measures.is_empty() {
model.add_category("Measure")?;
if let Some(cat) = model.category_mut("Measure") {
for m in &measures {
cat.add_item(&m.field);
}
}
}
for record in &self.records {
if let Value::Object(map) = record {
let mut coords: Vec<(String, String)> = vec![];
let mut valid = true;
for cat_proposal in &categories {
let val = map
.get(&cat_proposal.field)
.and_then(|v| v.as_str())
.map(|s| s.to_string())
.or_else(|| map.get(&cat_proposal.field).map(|v| v.to_string()));
if let Some(v) = val {
if let Some(cat) = model.category_mut(&cat_proposal.field) {
cat.add_item(&v);
}
coords.push((cat_proposal.field.clone(), v.clone()));
// Extract date components from this field's value
for (field, fmt, comp, ref derived_name) in &date_extractions {
if *field == cat_proposal.field {
if let Some(derived_val) = extract_date_component(&v, fmt, *comp) {
if let Some(cat) = model.category_mut(derived_name) {
cat.add_item(&derived_val);
}
coords.push((derived_name.clone(), derived_val));
}
}
}
} else {
valid = false;
break;
}
}
if !valid {
continue;
}
// Attach label values as coords (missing labels become "").
for lab in &labels {
let val = map
.get(&lab.field)
.and_then(|v| v.as_str())
.map(|s| s.to_string())
.or_else(|| {
map.get(&lab.field).and_then(|v| {
if v.is_null() {
None
} else {
Some(v.to_string())
}
})
})
.unwrap_or_default();
if let Some(cat) = model.category_mut(&lab.field) {
cat.add_item(&val);
}
coords.push((lab.field.clone(), val));
}
for measure in &measures {
if let Some(val) = map.get(&measure.field).and_then(|v| v.as_f64()) {
let mut cell_coords = coords.clone();
cell_coords.push(("Measure".to_string(), measure.field.clone()));
model.set_cell(CellKey::new(cell_coords), CellValue::Number(val));
}
}
}
}
// Parse and add formulas
// Formulas target the "Measure" category by default.
let formula_cat: String = if model.category("Measure").is_some() {
"Measure".to_string()
} else {
model
.categories
.keys()
.next()
.cloned()
.unwrap_or_else(|| "Measure".to_string())
};
for raw in &self.formulas {
if let Ok(formula) = parse_formula(raw, &formula_cat) {
model.add_formula(formula);
}
}
Ok(model)
}
}
// ── Wizard (UI state wrapped around the pipeline) ─────────────────────────────
#[derive(Debug, Clone, PartialEq)]
pub enum WizardStep {
Preview,
SelectArrayPath,
ReviewProposals,
ConfigureDates,
DefineFormulas,
NameModel,
Done,
}
/// Interactive state layered on top of [`ImportPipeline`] for the TUI wizard.
/// The pipeline holds all data; the wizard holds only what the UI needs to
/// drive the multi-step interaction (current step, list cursor, error message).
#[derive(Debug)]
pub struct ImportWizard {
pub pipeline: ImportPipeline,
pub step: WizardStep,
/// Cursor within the current list (array paths or proposals).
pub cursor: usize,
/// One-line message to display at the bottom of the wizard panel.
pub message: Option<String>,
/// Whether we're in formula text-input mode.
pub formula_editing: bool,
/// Buffer for the formula being typed.
pub formula_buffer: String,
}
impl ImportWizard {
pub fn new(raw: Value) -> Self {
let pipeline = ImportPipeline::new(raw);
let step = if pipeline.needs_path_selection() {
WizardStep::SelectArrayPath
} else if pipeline.records.is_empty() {
WizardStep::Preview
} else {
WizardStep::ReviewProposals
};
Self {
pipeline,
step,
cursor: 0,
message: None,
formula_editing: false,
formula_buffer: String::new(),
}
}
// ── Step transitions ──────────────────────────────────────────────────────
pub fn advance(&mut self) {
self.step = match self.step {
WizardStep::Preview => {
if self.pipeline.array_paths.len() > 1 && self.pipeline.needs_path_selection() {
WizardStep::SelectArrayPath
} else {
WizardStep::ReviewProposals
}
}
WizardStep::SelectArrayPath => WizardStep::ReviewProposals,
WizardStep::ReviewProposals => {
if self.has_time_categories() {
WizardStep::ConfigureDates
} else {
WizardStep::DefineFormulas
}
}
WizardStep::ConfigureDates => WizardStep::DefineFormulas,
WizardStep::DefineFormulas => WizardStep::NameModel,
WizardStep::NameModel => WizardStep::Done,
WizardStep::Done => WizardStep::Done,
};
self.cursor = 0;
self.message = None;
}
fn has_time_categories(&self) -> bool {
self.pipeline
.proposals
.iter()
.any(|p| p.accepted && p.kind == FieldKind::TimeCategory && p.date_format.is_some())
}
/// Get accepted TimeCategory proposals (for ConfigureDates step).
pub fn time_category_proposals(&self) -> Vec<&FieldProposal> {
self.pipeline
.proposals
.iter()
.filter(|p| p.accepted && p.kind == FieldKind::TimeCategory && p.date_format.is_some())
.collect()
}
pub fn confirm_path(&mut self) {
if self.cursor < self.pipeline.array_paths.len() {
let path = self.pipeline.array_paths[self.cursor].clone();
self.pipeline.select_path(&path);
self.advance();
}
}
// ── Cursor movement ───────────────────────────────────────────────────────
pub fn move_cursor(&mut self, delta: i32) {
let len = match self.step {
WizardStep::SelectArrayPath => self.pipeline.array_paths.len(),
WizardStep::ReviewProposals => self.pipeline.proposals.len(),
WizardStep::ConfigureDates => self.date_config_item_count(),
WizardStep::DefineFormulas => self.pipeline.formulas.len(),
_ => 0,
};
if len == 0 {
return;
}
if delta > 0 {
self.cursor = (self.cursor + 1).min(len - 1);
} else if self.cursor > 0 {
self.cursor -= 1;
}
}
// ── Proposal editing (delegates to pipeline) ──────────────────────────────
pub fn toggle_proposal(&mut self) {
if self.cursor < self.pipeline.proposals.len() {
self.pipeline.proposals[self.cursor].accepted =
!self.pipeline.proposals[self.cursor].accepted;
}
}
pub fn cycle_proposal_kind(&mut self) {
if self.cursor < self.pipeline.proposals.len() {
let p = &mut self.pipeline.proposals[self.cursor];
p.kind = match p.kind {
FieldKind::Category => FieldKind::Measure,
FieldKind::Measure => FieldKind::TimeCategory,
FieldKind::TimeCategory => FieldKind::Label,
FieldKind::Label => FieldKind::Category,
};
}
}
// ── Model name input ──────────────────────────────────────────────────────
pub fn push_name_char(&mut self, c: char) {
self.pipeline.model_name.push(c);
}
pub fn pop_name_char(&mut self) {
self.pipeline.model_name.pop();
}
// ── Date config ────────────────────────────────────────────────────────────
/// Total number of items in the ConfigureDates list.
/// Each TimeCategory field gets 3 rows (Year, Month, Quarter).
fn date_config_item_count(&self) -> usize {
self.time_category_proposals().len() * 3
}
/// Get the (field_index, component) for the current cursor position.
pub fn date_config_at_cursor(&self) -> Option<(usize, DateComponent)> {
let tc_indices = self.time_category_indices();
if tc_indices.is_empty() {
return None;
}
let field_idx = self.cursor / 3;
let comp_idx = self.cursor % 3;
let component = match comp_idx {
0 => DateComponent::Year,
1 => DateComponent::Month,
_ => DateComponent::Quarter,
};
tc_indices.get(field_idx).map(|&pi| (pi, component))
}
/// Indices into pipeline.proposals for accepted TimeCategory fields.
fn time_category_indices(&self) -> Vec<usize> {
self.pipeline
.proposals
.iter()
.enumerate()
.filter(|(_, p)| {
p.accepted && p.kind == FieldKind::TimeCategory && p.date_format.is_some()
})
.map(|(i, _)| i)
.collect()
}
/// Toggle a date component for the field at the current cursor.
pub fn toggle_date_component(&mut self) {
if let Some((pi, component)) = self.date_config_at_cursor() {
let proposal = &mut self.pipeline.proposals[pi];
if let Some(pos) = proposal
.date_components
.iter()
.position(|c| *c == component)
{
proposal.date_components.remove(pos);
} else {
proposal.date_components.push(component);
}
}
}
// ── Formula editing ────────────────────────────────────────────────────────
/// Buffer for typing a new formula in the DefineFormulas step.
pub fn push_formula_char(&mut self, c: char) {
if !self.formula_editing {
self.formula_editing = true;
self.formula_buffer.clear();
}
self.formula_buffer.push(c);
}
pub fn pop_formula_char(&mut self) {
self.formula_buffer.pop();
}
/// Commit the current formula buffer to the pipeline's formula list.
pub fn confirm_formula(&mut self) {
let text = self.formula_buffer.trim().to_string();
if !text.is_empty() {
self.pipeline.formulas.push(text);
}
self.formula_buffer.clear();
self.formula_editing = false;
self.cursor = self.pipeline.formulas.len().saturating_sub(1);
}
/// Delete the formula at the current cursor position.
pub fn delete_formula(&mut self) {
if self.cursor < self.pipeline.formulas.len() {
self.pipeline.formulas.remove(self.cursor);
if self.cursor > 0 && self.cursor >= self.pipeline.formulas.len() {
self.cursor -= 1;
}
}
}
/// Start editing a new formula.
pub fn start_formula_edit(&mut self) {
self.formula_editing = true;
self.formula_buffer.clear();
}
/// Cancel formula editing.
pub fn cancel_formula_edit(&mut self) {
self.formula_editing = false;
self.formula_buffer.clear();
}
/// Generate sample formulas based on accepted measures.
pub fn sample_formulas(&self) -> Vec<String> {
let measures: Vec<&str> = self
.pipeline
.proposals
.iter()
.filter(|p| p.accepted && p.kind == FieldKind::Measure)
.map(|p| p.field.as_str())
.collect();
let mut samples = Vec::new();
if measures.len() >= 2 {
samples.push(format!("Diff = {} - {}", measures[0], measures[1]));
}
if !measures.is_empty() {
samples.push(format!("Total = SUM({})", measures[0]));
}
if measures.len() >= 2 {
samples.push(format!("Ratio = {} / {}", measures[0], measures[1]));
}
samples
}
// ── Delegate build to pipeline ────────────────────────────────────────────
pub fn build_model(&self) -> Result<Model> {
self.pipeline.build_model()
}
}
#[cfg(test)]
mod tests {
use super::ImportPipeline;
use crate::import::analyzer::FieldKind;
use serde_json::json;
#[test]
fn flat_array_auto_selected() {
let raw = json!([
{"region": "East", "product": "Shirts", "revenue": 100.0},
{"region": "West", "product": "Shirts", "revenue": 200.0},
]);
let p = ImportPipeline::new(raw);
assert!(!p.records.is_empty());
assert!(!p.proposals.is_empty());
assert!(!p.needs_path_selection());
}
#[test]
fn numeric_field_proposed_as_measure() {
let raw = json!([{"region": "East", "revenue": 100.0, "cost": 50.0}]);
let p = ImportPipeline::new(raw);
let revenue = p.proposals.iter().find(|p| p.field == "revenue").unwrap();
assert_eq!(revenue.kind, FieldKind::Measure);
}
#[test]
fn low_cardinality_string_field_proposed_as_category() {
let raw = json!([
{"region": "East", "revenue": 100.0},
{"region": "West", "revenue": 200.0},
{"region": "East", "revenue": 150.0},
]);
let p = ImportPipeline::new(raw);
let region = p.proposals.iter().find(|p| p.field == "region").unwrap();
assert_eq!(region.kind, FieldKind::Category);
}
#[test]
fn nested_json_needs_path_selection_when_multiple_arrays() {
let raw = json!({
"orders": [{"id": 1}, {"id": 2}],
"products": [{"name": "A"}, {"name": "B"}],
});
let p = ImportPipeline::new(raw);
assert!(p.needs_path_selection());
}
#[test]
fn single_nested_array_auto_selected() {
let raw = json!({
"data": [
{"region": "East", "revenue": 100.0},
{"region": "West", "revenue": 200.0},
]
});
let p = ImportPipeline::new(raw);
assert!(!p.records.is_empty());
assert!(!p.needs_path_selection());
}
#[test]
fn select_path_populates_records_and_proposals() {
let raw = json!({
"a": [{"x": 1}, {"x": 2}],
"b": [{"y": "foo"}, {"y": "bar"}],
});
let mut p = ImportPipeline::new(raw);
p.select_path("b");
assert!(!p.records.is_empty());
assert!(!p.proposals.is_empty());
}
#[test]
fn build_model_fails_with_no_accepted_categories() {
let raw = json!([{"revenue": 100.0, "cost": 50.0}]);
let mut p = ImportPipeline::new(raw);
for prop in &mut p.proposals {
prop.accepted = false;
}
assert!(p.build_model().is_err());
}
#[test]
fn build_model_creates_categories_and_measure_category() {
let raw = json!([
{"region": "East", "revenue": 100.0},
{"region": "West", "revenue": 200.0},
]);
let p = ImportPipeline::new(raw);
let model = p.build_model().unwrap();
assert!(model.category("region").is_some());
assert!(model.category("Measure").is_some());
}
#[test]
fn label_fields_imported_as_label_category_coords() {
use crate::model::category::CategoryKind;
// 25 unique descriptions → classified as Label (> CATEGORY_THRESHOLD=20)
let records: Vec<serde_json::Value> = (0..25)
.map(|i| json!({"region": "East", "desc": format!("row-{i}"), "revenue": i as f64}))
.collect();
let raw = serde_json::Value::Array(records);
let p = ImportPipeline::new(raw);
let desc = p.proposals.iter().find(|p| p.field == "desc").unwrap();
assert_eq!(desc.kind, FieldKind::Label);
assert!(desc.accepted, "labels should default to accepted");
let model = p.build_model().unwrap();
// Label field exists as a category with Label kind
let cat = model.category("desc").expect("desc category exists");
assert_eq!(cat.kind, CategoryKind::Label);
// Each record's cell key carries the desc label coord
use crate::model::cell::CellKey;
let k = CellKey::new(vec![
("Measure".to_string(), "revenue".to_string()),
("desc".to_string(), "row-7".to_string()),
("region".to_string(), "East".to_string()),
]);
assert_eq!(model.get_cell(&k).and_then(|v| v.as_f64()), Some(7.0));
}
#[test]
fn label_category_defaults_to_none_axis() {
use crate::view::Axis;
let records: Vec<serde_json::Value> = (0..25)
.map(|i| json!({"region": "East", "desc": format!("r{i}"), "n": 1.0}))
.collect();
let raw = serde_json::Value::Array(records);
let p = ImportPipeline::new(raw);
let model = p.build_model().unwrap();
let v = model.active_view();
assert_eq!(v.axis_of("desc"), Axis::None);
}
#[test]
fn build_model_cells_match_source_data() {
let raw = json!([
{"region": "East", "revenue": 100.0},
{"region": "West", "revenue": 200.0},
]);
let p = ImportPipeline::new(raw);
let model = p.build_model().unwrap();
use crate::model::cell::CellKey;
let k_east = CellKey::new(vec![
("Measure".to_string(), "revenue".to_string()),
("region".to_string(), "East".to_string()),
]);
let k_west = CellKey::new(vec![
("Measure".to_string(), "revenue".to_string()),
("region".to_string(), "West".to_string()),
]);
assert_eq!(
model.get_cell(&k_east).and_then(|v| v.as_f64()),
Some(100.0)
);
assert_eq!(
model.get_cell(&k_west).and_then(|v| v.as_f64()),
Some(200.0)
);
}
#[test]
fn model_name_defaults_to_imported_model() {
let raw = json!([{"x": 1.0}]);
let p = ImportPipeline::new(raw);
assert_eq!(p.model_name, "Imported Model");
}
#[test]
fn build_model_adds_formulas_from_pipeline() {
let raw = json!([
{"region": "East", "revenue": 100.0, "cost": 40.0},
{"region": "West", "revenue": 200.0, "cost": 80.0},
]);
let mut p = ImportPipeline::new(raw);
p.formulas.push("Profit = revenue - cost".to_string());
let model = p.build_model().unwrap();
// The formula should produce Profit = 60 for East (100-40)
use crate::model::cell::CellKey;
let key = CellKey::new(vec![
("Measure".to_string(), "Profit".to_string()),
("region".to_string(), "East".to_string()),
]);
let val = model.evaluate(&key).and_then(|v| v.as_f64());
assert_eq!(val, Some(60.0));
}
#[test]
fn build_model_extracts_date_month_component() {
use crate::import::analyzer::DateComponent;
let raw = json!([
{"Date": "01/15/2025", "Amount": 100.0},
{"Date": "01/20/2025", "Amount": 50.0},
{"Date": "02/05/2025", "Amount": 200.0},
]);
let mut p = ImportPipeline::new(raw);
// Enable Month extraction on the Date field
for prop in &mut p.proposals {
if prop.field == "Date" && prop.kind == FieldKind::TimeCategory {
prop.date_components.push(DateComponent::Month);
}
}
let model = p.build_model().unwrap();
assert!(model.category("Date_Month").is_some());
let cat = model.category("Date_Month").unwrap();
let items: Vec<&str> = cat.items.keys().map(|s| s.as_str()).collect();
assert!(items.contains(&"2025-01"));
assert!(items.contains(&"2025-02"));
}
#[test]
fn build_model_date_components_appear_in_cell_keys() {
use crate::import::analyzer::DateComponent;
use crate::model::cell::CellKey;
let raw = json!([
{"Date": "03/31/2026", "Amount": 100.0},
]);
let mut p = ImportPipeline::new(raw);
for prop in &mut p.proposals {
if prop.field == "Date" {
prop.date_components.push(DateComponent::Month);
}
}
let model = p.build_model().unwrap();
let key = CellKey::new(vec![
("Date".to_string(), "03/31/2026".to_string()),
("Date_Month".to_string(), "2026-03".to_string()),
("Measure".to_string(), "Amount".to_string()),
]);
assert_eq!(model.get_cell(&key).and_then(|v| v.as_f64()), Some(100.0));
}
}