fix: preserve chosen birth scenarios without confirming the time
This commit is contained in:
@@ -162,14 +162,18 @@ def _line_view(line: Line, order: tuple[str, ...]) -> dict:
|
||||
|
||||
|
||||
def _line_summary(lines: list[Line], reference: datetime) -> dict:
|
||||
nearest = sorted(lines, key=lambda line: (abs(line.at - reference), line.at))
|
||||
charts: dict[str, dict] = {}
|
||||
for line in nearest:
|
||||
ordered = sorted((line for line in lines if line.flips.keys() & set(KEY_CHARTS)), key=lambda line: line.at)
|
||||
nearest = min(ordered, key=lambda line: (abs(line.at - reference), line.at), default=None)
|
||||
chart_lines: dict[str, list[dict]] = {}
|
||||
for line in ordered:
|
||||
view = _line_view(line, KEY_CHARTS)
|
||||
for chart, signs in view["flips"].items():
|
||||
row = charts.setdefault(chart, {"at": view["at"], "count": 0, **signs})
|
||||
row["count"] += 1
|
||||
return {"nearest_at": _at_s(nearest[0].at) if nearest else None, "charts": charts}
|
||||
chart_lines.setdefault(chart, []).append({"at": view["at"], **signs})
|
||||
charts: dict[str, dict] = {}
|
||||
for chart, flips in chart_lines.items():
|
||||
closest = min(flips, key=lambda flip: abs(_input_line(flip["at"]) - reference))
|
||||
charts[chart] = {**closest, "count": len(flips), "lines": flips}
|
||||
return {"nearest_at": _at_s(nearest.at) if nearest else None, "line_count": len(ordered), "charts": charts}
|
||||
|
||||
|
||||
def _comparisons(req: ChartRequest, before_at: datetime, after_at: datetime, charts: list[str]) -> list[dict]:
|
||||
@@ -645,6 +649,30 @@ def _self_choice_lines(req: ChartRequest, start: datetime, end: datetime) -> lis
|
||||
return sorted(by_time.values(), key=lambda line: line.at)
|
||||
|
||||
|
||||
def _self_choice_scenarios(
|
||||
req: ChartRequest, segment: Segment, lines: list[Line], reported: datetime, original: Segment,
|
||||
) -> tuple[list[dict], int]:
|
||||
starts = [segment[0], *(line.at for line in lines)]
|
||||
ends = [*(line.at - ONE_SECOND for line in lines), segment[1]]
|
||||
pieces = list(zip(starts, ends))
|
||||
|
||||
def distance(index: int) -> tuple[timedelta, int]:
|
||||
lo, hi = pieces[index]
|
||||
return max(lo - reported, reported - hi, timedelta()), index
|
||||
|
||||
nearest = sorted(range(len(pieces)), key=distance)[:4]
|
||||
primary = nearest[0]
|
||||
scenarios = []
|
||||
for index in sorted(nearest):
|
||||
piece = pieces[index]
|
||||
saved = _self_choice_at(piece, reported, original)
|
||||
scenarios.append({
|
||||
"segment": _segment(*piece), "saved_at": _saved_at(saved),
|
||||
"key_charts": _key_chart_rows(req, saved, [], []), "is_primary": index == primary,
|
||||
})
|
||||
return scenarios, len(pieces) - len(scenarios)
|
||||
|
||||
|
||||
def _self_choice(
|
||||
body: dict, req: ChartRequest, segment: Segment, reported: datetime, *, deciding: bool,
|
||||
) -> dict:
|
||||
@@ -658,7 +686,7 @@ def _self_choice(
|
||||
|
||||
lines = all_lines
|
||||
next_line = nearest_line(lines, reported)
|
||||
unsure = None
|
||||
comparison_segment = None
|
||||
for entry in choices:
|
||||
if not isinstance(entry, dict) or set(entry) != {"line_at", "side"}:
|
||||
raise ChartChoiceInputError("each choice needs only line_at and side")
|
||||
@@ -667,23 +695,45 @@ def _self_choice(
|
||||
or _input_line(entry["line_at"]) != next_line.at):
|
||||
raise ChartChoiceInputError("self-choice line is not the next available line")
|
||||
if side == "unsure":
|
||||
unsure, next_line = next_line, None
|
||||
next_line = None
|
||||
else:
|
||||
opposing = "after" if side == "before" else "before"
|
||||
lo, hi = _side(segment, next_line, opposing)
|
||||
opposing_lines = [line.at for line in lines if lo < line.at <= hi]
|
||||
if opposing_lines:
|
||||
if opposing == "before":
|
||||
lo = opposing_lines[-1]
|
||||
else:
|
||||
hi = opposing_lines[0] - ONE_SECOND
|
||||
comparison_segment = (lo, hi)
|
||||
segment = _side(segment, next_line, side)
|
||||
lines = [line for line in lines if segment[0] < line.at <= segment[1]]
|
||||
next_line = nearest_line(lines, reported)
|
||||
|
||||
saved = _self_choice_at(segment, reported, original)
|
||||
resolved = {"saved_at": _saved_at(saved), "segment": _segment(*segment), "choices": choices,
|
||||
"key_charts": _key_chart_rows(req, saved, lines, []),
|
||||
"line_summary": _line_summary(lines, reported), "both_versions": []}
|
||||
if unsure is not None:
|
||||
chosen = "before" if saved < unsure.at else "after"
|
||||
preceding = max((line.at for line in lines if line.at < unsure.at), default=segment[0])
|
||||
following = min((line.at for line in lines if line.at > unsure.at), default=segment[1] + ONE_SECOND)
|
||||
other = (unsure.at, following - ONE_SECOND) if chosen == "before" else (preceding, unsure.at - ONE_SECOND)
|
||||
resolved.update(chosen_side=chosen, other_saved_at=_saved_at(_self_choice_at(other, reported, original)),
|
||||
both_versions=[DOMAIN[key]["key"] for key in KEY_CHARTS if key in unsure.flips])
|
||||
scenarios, omitted = _self_choice_scenarios(req, segment, lines, reported, original)
|
||||
primary = next(item for item in scenarios if item["is_primary"])
|
||||
saved = datetime.fromisoformat(primary["saved_at"])
|
||||
resolved = {
|
||||
"saved_at": primary["saved_at"], "segment": _segment(*segment), "choices": choices,
|
||||
"key_charts": _key_chart_rows(req, saved, lines, []), "line_summary": _line_summary(lines, reported),
|
||||
"remaining_lines": [{"at": _at_s(line.at), "charts": [key for key in KEY_CHARTS if key in line.flips]}
|
||||
for line in lines],
|
||||
"scenarios": scenarios, "scenarios_omitted": omitted, "both_versions": [],
|
||||
}
|
||||
if comparison_segment:
|
||||
comparison_at = _self_choice_at(comparison_segment, reported, original)
|
||||
resolved["comparison_scenario"] = {
|
||||
"segment": _segment(*comparison_segment), "saved_at": _saved_at(comparison_at),
|
||||
"key_charts": _key_chart_rows(req, comparison_at, [], []), "is_primary": False,
|
||||
}
|
||||
if len(lines) == 1:
|
||||
line = lines[0]
|
||||
other = next(item for item in scenarios if not item["is_primary"])
|
||||
resolved.update(chosen_side="before" if saved < line.at else "after",
|
||||
other_saved_at=other["saved_at"],
|
||||
both_versions=[DOMAIN[key]["key"] for key in KEY_CHARTS if key in line.flips])
|
||||
elif lines:
|
||||
resolved.update(chosen_side="none", other_saved_at=None)
|
||||
elif choices:
|
||||
resolved["chosen_side"] = choices[-1]["side"]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user