Add median duration handling

This commit is contained in:
Dandaedre 2026-11-05 07:04:06 +01:00
parent 6447dacdd9
commit 5aaf5cae85
7 changed files with 83 additions and 6 deletions

View File

@ -634,13 +634,17 @@ fn infer_inner(
let dur = selected.get("duration").copied();
let mut unit = profile.duration_unit.clone();
let duration_mode = profile.duration_mode.as_deref().unwrap_or("strict");
if !["strict", "minimum", "maximum", "sum", "margin", "split"].contains(&duration_mode) {
if ![
"strict", "minimum", "maximum", "median", "sum", "margin", "split",
]
.contains(&duration_mode)
{
diagnostics.push(err(
"INVALID_DURATION_MODE",
table,
header,
dur.unwrap_or(start),
"Use strict, minimum, maximum, sum, margin or split",
"Use strict, minimum, maximum, median, sum, margin or split",
));
}
if profile.duration_add_margin && profile.margin_ms().is_none() {

View File

@ -86,6 +86,7 @@ fn duration_mode_label(mode: &str) -> &'static str {
match mode {
"minimum" => "Minimum",
"maximum" => "Maximum",
"median" => "Median",
"sum" => "Sum",
_ => "Use the whole cell",
}
@ -125,6 +126,7 @@ fn duration_handling_controls(ui: &mut egui::Ui, profile: &mut Profile, id: egui
("strict", "Use the whole cell"),
("minimum", "Minimum"),
("maximum", "Maximum"),
("median", "Median"),
("sum", "Sum"),
] {
ui.selectable_value(&mut mode, value.into(), label);

View File

@ -71,7 +71,7 @@ pub struct Profile {
pub secondary_title_mode: Option<String>,
/// minutes, seconds, hh:mm, mm:ss, or excel.
pub duration_unit: Option<String>,
/// strict, minimum, maximum, sum, or margin (legacy split is accepted).
/// strict, minimum, maximum, median, sum, or margin (legacy split is accepted).
pub duration_mode: Option<String>,
/// Separator used by split mode. Escaped forms such as \\n and \\t are accepted.
pub duration_separator: Option<String>,

View File

@ -217,6 +217,21 @@ pub fn select_durations(
margin: 0,
margins: vec![],
},
"median" => {
let mut sorted = values;
sorted.sort_unstable();
let middle = sorted.len() / 2;
let value = if sorted.len().is_multiple_of(2) {
(sorted[middle - 1] + sorted[middle]) / 2
} else {
sorted[middle]
};
SelectedDurations {
events: vec![value],
margin: 0,
margins: vec![],
}
}
"sum" => SelectedDurations {
events: vec![values.iter().sum()],
margin: 0,

View File

@ -1659,3 +1659,56 @@ fn fixed_margin_requires_a_value_and_applies_to_overrides_and_derived_durations(
assert_eq!(r.gaps[0].end_ms, Some(r.entries[0].end_ms + 30_000));
}
}
#[test]
fn median_duration_handles_odd_even_duplicates_and_single_values() {
for (text, expected) in [
("20 min + 5 min + 10 min", 600_000),
("20 min + 5 min", 750_000),
("20 min + 5 min + 5 min", 300_000),
("3 min", 180_000),
] {
let cell = Cell {
text: text.into(),
..Default::default()
};
assert_eq!(
time::select_durations(&cell, None, Some("median"), None)
.unwrap()
.events,
[expected]
);
}
}
#[test]
fn median_is_applied_per_split_entry_with_independent_fixed_margins() {
let t = table(&[
&["Start", "Duration", "Title"],
&["09:00", "20 min + 5 min + 10 min\n20 min + 5 min", "Talks"],
]);
let profile = Profile {
duration_mode: Some("median".into()),
duration_split: true,
duration_add_margin: true,
duration_margin: Some("30 s".into()),
..Default::default()
};
let profile: Profile = serde_json::from_str(&serde_json::to_string(&profile).unwrap()).unwrap();
let a = analyze(&t, &profile);
assert!(a.diagnostics.is_empty(), "{:?}", a.diagnostics);
let r = a.rundown.unwrap();
assert_eq!(
r.entries.iter().map(|e| e.duration_ms).collect::<Vec<_>>(),
[600_000, 750_000]
);
assert_eq!(r.entries[1].start_ms, r.entries[0].end_ms + 30_000);
assert_eq!(r.gaps.len(), 2);
let json = export::ontime(&r).unwrap();
for e in &r.entries {
assert_eq!(
json["rundowns"]["default"]["entries"][&e.id]["duration"],
e.duration_ms
);
}
}

View File

@ -297,7 +297,7 @@ errors must remain visible with direct corrective actions.
errors. Test ambiguous cells clarified by other rows, incompatible meanings in
one column, structural rows and misleading headers/units.
- [x] Keep one Duration column role while offering nested whole-cell, minimum,
maximum and sum extraction policies, plus independent margin and split toggles. Let a Start mapping select
maximum, median and sum extraction policies, plus independent margin and split toggles. Let a Start mapping select
only the first clock in a mixed range; retain margin gaps and split entries in
the IR.
- [x] Add a secondary title role with Either, Merge, Notes and Metadata handling,

View File

@ -124,7 +124,7 @@ visually distinct from labels and ordinary actions; the active tab uses the stro
bounds. **Start** selects only the first clock from a cell, so a mixed
`08h45 - 09h10` value can provide the 08:45 anchor while the selected duration
column determines the timer. A duration column has a nested **Duration handling**
choice instead of extra column roles: whole cell, minimum, maximum or sum.
choice instead of extra column roles: whole cell, minimum, maximum, median or sum.
Separate **Add margin** and **Split into multiple entries** toggles can be
combined with that choice. Add margin takes an explicit positive duration (for example `2 min`) and adds
that gap after every entry, including the last one;
@ -312,7 +312,10 @@ Notes and Metadata columns. The primary title column remains required; rows with
both titles empty still need correction.
`duration_mode` is optional and remains attached to the single `duration` role.
`minimum`, `maximum` and `sum` select from explicit duration values in mixed text.
`minimum`, `maximum`, `median` and `sum` select from explicit duration values in mixed text.
Median selects the middle value after sorting; for an even count it averages
the two middle values (rounded down to a whole millisecond). With splitting,
it operates on each separated group independently.
`duration_add_margin: true` with `duration_margin: "2 min"` adds a two-minute
untimed gap after each event. The value is independent of the source durations
and selected operation; it also applies with Sum and whole-cell handling.