Group column diagnostics into actionable summaries

This commit is contained in:
Dandaedre 2026-11-05 07:16:28 +01:00
parent ac1787fb45
commit 53f0d1fd88
2 changed files with 260 additions and 69 deletions

View File

@ -108,6 +108,84 @@ fn ambiguous_columns(message: &str) -> Option<(String, Vec<usize>)> {
(!columns.is_empty()).then(|| (role.to_owned(), columns))
}
fn diagnostic_column(diagnostic: &Diagnostic, table: &Table) -> Option<usize> {
let cell = diagnostic
.location
.strip_prefix(&format!("{}!", table.name))?;
let letters: String = cell.chars().take_while(char::is_ascii_uppercase).collect();
let row = cell.strip_prefix(&letters)?;
if row.is_empty() || !row.chars().all(|c| c.is_ascii_digit()) {
return None;
}
crate::model::column_index(&letters)
.filter(|&c| c < table.rows.iter().map(Vec::len).max().unwrap_or(0))
}
fn column_issue_advice(code: &str, duration: bool) -> (&'static str, &'static str) {
match code {
"INCONSISTENT_COLUMN_MEANING" if duration => (
"Mixed or unclear durations",
"Please specify Duration handling, or choose another column with unambiguous durations.",
),
"INCONSISTENT_COLUMN_MEANING" => (
"Values do not fit the selected role",
"Choose the correct column role or correct the source values.",
),
"FORMULA_WITHOUT_VALUE" => (
"Formula results unavailable",
"Recalculate and save the workbook in Excel or LibreOffice.",
),
"INVALID_UNIT" => (
"Unclear duration unit",
"Choose the duration unit in Advanced settings.",
),
"INVALID_DURATION_MODE" => (
"Invalid duration handling",
"Choose a Duration handling option.",
),
"INVALID_DURATION_SEPARATOR" => (
"Missing split separator",
"Enter a separator for Split into multiple entries.",
),
"INVALID_MARGIN" => (
"Missing or invalid margin",
"Enter a positive margin duration, such as 30 s or 2 min.",
),
"INVALID_SECONDARY_TITLE_MODE" => (
"Invalid secondary title handling",
"Choose Either, Merge, Notes or Metadata.",
),
"PROFILE_HEADER_CHANGED" => (
"Saved header no longer matches",
"Check this column and update or reset the saved mapping.",
),
"INVALID_MAPPING" | "INVALID_COLUMN_USAGE" => (
"Conflicting column choices",
"Assign a single role to each column and check the selected columns.",
),
"AMBIGUOUS_COLUMN" => (
"Several columns could fit",
"Select the intended column manually.",
),
"MISSING_COLUMN" => (
"Required column missing",
"Select the required timing or title column manually.",
),
"HEADER_REQUIRED" | "INVALID_HEADER" => (
"Header row needs attention",
"Select the header row in Setup → Manual header mapping.",
),
"INVALID_ROW_OVERRIDE" => (
"Saved row correction is invalid",
"Review row corrections or reset the saved resolution.",
),
_ => (
"Mapping needs attention",
"Review the complete error log for the required correction.",
),
}
}
#[derive(Clone)]
struct PreviewRow {
row: usize,
@ -1351,6 +1429,7 @@ impl App {
mapping_focus_until = None;
}
let mut ambiguities = Vec::new();
let mut warning_columns = std::collections::BTreeSet::new();
if let (Some(table), Ok(mut profile)) = (
self.tables.iter().find(|t| t.name == self.sheet),
serde_json::from_str::<Profile>(&self.profile),
@ -1395,36 +1474,41 @@ impl App {
});
}
Err(errors) => {
let grouped = errors.iter().fold(
BTreeMap::<(&str, &str), Vec<&Diagnostic>>::new(),
|mut groups, error| {
groups
.entry((&error.code, &error.message))
let mut grouped = BTreeMap::<Option<usize>, Vec<&Diagnostic>>::new();
let mut ambiguous = BTreeMap::new();
for error in errors {
if let Some((role, candidates)) = ambiguous_columns(&error.message) {
ambiguous.entry((role, candidates)).or_insert(error);
} else {
grouped
.entry(diagnostic_column(error, table))
.or_default()
.push(error);
groups
},
);
}
}
let affected = grouped
.keys()
.filter_map(|column| *column)
.chain(
ambiguous
.keys()
.flat_map(|(_, columns)| columns.iter().copied()),
)
.collect::<std::collections::BTreeSet<_>>()
.len();
let column_word = if affected == 1 { "column" } else { "columns" };
ui.colored_label(
PREVIEW_AMBER,
if grouped.len() == errors.len() {
format!("! Column mapping needs attention · {} issues", errors.len())
} else {
let issue_type = if grouped.len() == 1 {
"issue type"
} else {
"issue types"
};
format!(
"! Column mapping needs attention · {} affected cells · {} {issue_type}",
errors.len(),
grouped.len()
)
},
format!(
"! Column mapping needs attention · {affected} {column_word} · {} issues",
errors.len()
),
);
for ((code, message), occurrences) in grouped {
let error = occurrences[0];
if let Some((role, candidates)) = ambiguous_columns(&error.message) {
for ((role, candidates), error) in ambiguous {
let code = error.code.as_str();
let message = error.message.as_str();
{
ambiguities.push((role.clone(), candidates.clone()));
let letters = candidates
.iter()
@ -1456,38 +1540,99 @@ impl App {
.wrap(),
);
});
} else {
let summary = if code == "FORMULA_WITHOUT_VALUE" {
format!(
"{} formulas have no saved result. Simple arithmetic is calculated automatically; recalculate the remaining formulas in Excel or LibreOffice.",
occurrences.len()
)
} else if occurrences.len() > 1 {
format!("{message} · {} occurrences", occurrences.len())
} else {
message.to_string()
};
ui.add(
egui::Label::new(RichText::new(summary).color(PREVIEW_AMBER))
.wrap(),
);
if occurrences.len() > 1 {
egui::CollapsingHeader::new(RichText::new("Affected cells").weak())
.id_salt(("column_affected_cells", &table.name, code, message))
.show(ui, |ui| {
ui.weak(
occurrences
.iter()
.map(|error| error.location.as_str())
.collect::<Vec<_>>()
.join(", "),
);
});
}
}
}
for (column, occurrences) in grouped {
if let Some(column) = column {
warning_columns.insert(column);
}
let duration = column.is_some_and(|c| {
column_choice(&profile, c) == Some("duration")
|| (column_choice(&profile, c).is_none()
&& suggestions.get(&c) == Some(&"Duration"))
});
let advice: std::collections::BTreeSet<_> = occurrences
.iter()
.map(|error| column_issue_advice(&error.code, duration))
.collect();
let title = column.map_or_else(
|| "Sheet / settings".into(),
|c| format!("Column {}", column_name(c)),
);
ui.horizontal_wrapped(|ui| {
ui.colored_label(
PREVIEW_AMBER,
format!("{title} · {} issues", occurrences.len()),
);
if let Some(column) = column {
if ui
.button(
if duration
&& occurrences.iter().any(|error| {
matches!(
error.code.as_str(),
"INCONSISTENT_COLUMN_MEANING"
| "INVALID_DURATION_MODE"
)
})
{
"Choose duration handling…"
} else {
"Review column…"
},
)
.clicked()
{
mapping_focus_scroll = Some(column);
mapping_focus = Some(("warning".into(), vec![column]));
mapping_focus_until = Some(now + COLUMN_FOCUS_PULSE_SECONDS);
}
} else if ui.button("Open Setup…").clicked() {
self.tab = Tab::Setup;
self.settings = true;
}
});
for (reason, action) in advice {
ui.label(format!("{reason}. {action}"));
}
egui::CollapsingHeader::new(RichText::new("Affected cells").weak())
.id_salt(("column_locations", &table.name, column))
.show(ui, |ui| {
ui.weak(
occurrences
.iter()
.map(|error| error.location.as_str())
.collect::<Vec<_>>()
.join(", "),
);
});
}
}
}
let full_errors = match &mapping {
Err(errors) => errors.as_slice(),
Ok(_) => self
.analysis
.as_ref()
.map_or(&[][..], |analysis| analysis.diagnostics.as_slice()),
};
if !full_errors.is_empty() {
egui::CollapsingHeader::new(RichText::new("Complete error log").weak())
.id_salt(("complete_column_log", &table.name))
.show(ui, |ui| {
let text = full_errors
.iter()
.map(|error| {
format!("{} — {}\n{}", error.code, error.location, error.message)
})
.collect::<Vec<_>>()
.join("\n\n");
if ui.button("Copy complete log").clicked() {
ui.ctx().copy_text(text.clone());
}
ui.label(text);
});
}
ui.collapsing("Detection evidence", |ui| {
for text in &evidence {
ui.label(text);
@ -1538,7 +1683,7 @@ impl App {
ui.ctx()
.request_repaint_after(std::time::Duration::from_millis(40));
}
} else if !highlighted_role.is_empty() {
} else if !highlighted_role.is_empty() || warning_columns.contains(&c) {
let pulse = if focused {
let until = mapping_focus_until.unwrap_or(now);
let progress = ((now - (until - COLUMN_FOCUS_PULSE_SECONDS))
@ -1575,6 +1720,9 @@ impl App {
if pending {
ui.colored_label(PREVIEW_PENDING, "Pending apply");
}
if warning_columns.contains(&c) {
ui.colored_label(PREVIEW_AMBER, "Needs attention");
}
if let Some(role) = suggestions.get(&c) {
ui.colored_label(PREVIEW_SUCCESS, format!("Suggested: {role}"))
.on_hover_text("Automatic recommendation without manual column choices. If detection is blocked, this is a header hint; Apply & recheck still validates the mapping.");
@ -2817,12 +2965,12 @@ mod tests {
render(&mut app, &ctx, [900.0, 900.0], vec![]);
let labels = render(&mut app, &ctx, [900.0, 900.0], vec![]);
assert!(labels.iter().any(|(text, _)| {
text == "! Column mapping needs attention · 41 affected cells · 1 issue type"
text == "! Column mapping needs attention · 1 column · 41 issues"
}));
assert_eq!(
labels
.iter()
.filter(|(text, _)| text.starts_with("41 formulas have no saved result."))
.filter(|(text, _)| text.starts_with("Formula results unavailable."))
.count(),
1
);
@ -2933,22 +3081,59 @@ mod tests {
let ctx = egui::Context::default();
configure_style(&ctx);
ctx.style_mut(|style| style.animation_time = 0.0);
let size = [1200.0, 1000.0];
let size = [1200.0, 1400.0];
render(&mut app, &ctx, size, vec![]);
let labels = render(&mut app, &ctx, size, vec![]);
let headers: Vec<_> = labels
assert_eq!(
labels
.iter()
.filter(|(text, _)| text.starts_with("Column B ·"))
.count(),
1
);
assert_eq!(
labels
.iter()
.filter(|(text, _)| text.starts_with("Mixed or unclear durations."))
.count(),
1
);
assert!(
!labels
.iter()
.any(|(text, _)| text.contains("does not fit duration"))
);
let action = labels
.iter()
.filter(|(text, _)| text == "Affected cells")
.collect();
assert_eq!(headers.len(), 2);
let second = headers[1].1.center();
click_size(&mut app, &ctx, second, size);
.find(|(text, _)| text == "Choose duration handling…")
.unwrap()
.1
.center();
click_size(&mut app, &ctx, action, size);
assert_eq!(app.mapping_focus.as_ref().unwrap().1, vec![1]);
let labels = render(&mut app, &ctx, size, vec![]);
assert!(labels.iter().any(|(text, _)| text == "Show!B4, Show!B5"));
assert!(!labels.iter().any(|(text, _)| text == "Show!B2, Show!B3"));
assert!(!labels.iter().any(|(text, _)| {
text.contains("use of widget ID") || text.contains("Widget is above this text")
}));
let log = labels
.iter()
.find(|(text, _)| text == "Complete error log")
.unwrap()
.1
.center();
click_size(&mut app, &ctx, log, size);
let labels = render(&mut app, &ctx, size, vec![]);
let log = labels
.iter()
.find(|(text, _)| text.contains("INCONSISTENT_COLUMN_MEANING — Show!B2"))
.unwrap();
for row in 2..=5 {
assert!(log.0.contains(&format!("Show!B{row}")));
}
assert!(log.0.contains("invalid alpha") && log.0.contains("invalid beta"));
assert!(labels.iter().any(|(text, _)| text == "Copy complete log"));
assert!(
!labels
.iter()
.any(|(text, _)| text.contains("use of widget ID"))
);
}
#[test]

View File

@ -117,7 +117,13 @@ visually distinct from labels and ordinary actions; the active tab uses the stro
the timing preview and diagnostics continue to show the previous analysis.
- **Columns** shows a concise verified mapping summary, green detected roles,
per-column role dropdowns, and sample values. **Suggested: …** shows useful
automatic recommendations even after a manual choice. When detection is blocked,
automatic recommendations even after a manual choice. Problems are grouped by
affected column, with short explanations and suggested corrections instead of
repeating each invalid cell. **Review column…** or **Choose duration handling…**
scrolls to and highlights the affected column; columns needing attention remain
amber. Expand **Complete error log** to read or copy every original diagnostic
and source location. This grouping changes only the GUI summary, not CLI logs.
When detection is blocked,
recognised headers still provide hints; these do not replace validation. Expand **Detection evidence** for
full inference and validation details; hover the **Columns** tab for a compact
explanation of the role choices. **Start–end** maps one column containing full