data problem fix

This commit is contained in:
Ayzen
2026-03-11 14:51:30 +03:00
parent 1f715ce22c
commit bee3d306a4
4 changed files with 101 additions and 25 deletions
+57 -1
View File
@@ -3,10 +3,13 @@
from __future__ import annotations
from collections import deque
from typing import TypeVar
from python_app.models.dataset_model import ResultCollection
from python_app.models.dataset_model import ResultCollection, SweepCollection
from python_app.models.run_config_model import RunConfigModel
THistoryCollection = TypeVar("THistoryCollection", SweepCollection, ResultCollection)
def record_result_history(
result_history: deque[ResultCollection],
@@ -26,6 +29,35 @@ def record_result_history(
return True
def remove_last_aligned_histories(
raw_history: list[SweepCollection],
preprocessed_history: list[SweepCollection],
result_history: list[ResultCollection],
) -> tuple[list[SweepCollection], list[SweepCollection], list[ResultCollection]]:
"""Remove the newest aligned history entry using results as preferred anchor."""
retained_raw = list(raw_history)
retained_preprocessed = list(preprocessed_history)
retained_results = list(result_history)
if retained_results:
target_key = _tail_occurrence_key(retained_results, len(retained_results) - 1)
retained_results.pop()
_remove_by_tail_occurrence_key(retained_raw, target_key)
_remove_by_tail_occurrence_key(retained_preprocessed, target_key)
return retained_raw, retained_preprocessed, retained_results
if retained_preprocessed:
target_key = _tail_occurrence_key(retained_preprocessed, len(retained_preprocessed) - 1)
retained_preprocessed.pop()
_remove_by_tail_occurrence_key(retained_raw, target_key)
return retained_raw, retained_preprocessed, retained_results
if retained_raw:
retained_raw.pop()
return retained_raw, retained_preprocessed, retained_results
def build_run_history_signature(
config: RunConfigModel,
) -> tuple[object, ...]:
@@ -51,3 +83,27 @@ def build_run_history_signature(
str(config.preprocess.reference_set),
combos_signature,
)
def _tail_occurrence_key(history: list[THistoryCollection], index: int) -> tuple[int, int]:
"""Return `(collection_id, occurrence_from_tail)` for the item at `index`."""
collection_id = int(history[index].collection_id)
occurrence_from_tail = 0
for cursor in range(len(history) - 1, index, -1):
if int(history[cursor].collection_id) == collection_id:
occurrence_from_tail += 1
return collection_id, occurrence_from_tail
def _remove_by_tail_occurrence_key(history: list[THistoryCollection], key: tuple[int, int]) -> bool:
"""Remove newest matching entry identified by `(collection_id, occurrence_from_tail)`."""
collection_id, target_occurrence = key
occurrence_from_tail = 0
for index in range(len(history) - 1, -1, -1):
if int(history[index].collection_id) != collection_id:
continue
if occurrence_from_tail == target_occurrence:
history.pop(index)
return True
occurrence_from_tail += 1
return False