feat: Implement data archiving functionality in DataManager, enhance input validation, and add UI option for archiving old data

This commit is contained in:
William Valentin
2025-08-08 17:33:02 -07:00
parent 117e489072
commit 9e107f6125
4 changed files with 218 additions and 30 deletions
+41 -16
View File
@@ -233,34 +233,59 @@ class InputValidator:
entry_data: dict[str, Any],
) -> tuple[bool, list[str]]:
"""
Validate that an entry has the minimum required data.
Backward-compat entry completeness check.
Delegates to validate_entry_completeness_with_keys when possible.
"""
# Heuristic split: treat keys ending with _doses and note/date as
# non-core and assume the rest are a mix of pathologies and medicines;
# callers should prefer the explicit API below.
keys = [
k
for k in entry_data
if k not in {"date", "note"} and not str(k).endswith("_doses")
]
# Even split guess is unreliable; use value patterns instead:
path_keys = [k for k in keys if isinstance(entry_data.get(k), int | float)]
med_keys = [k for k in keys if k not in path_keys]
return InputValidator.validate_entry_completeness_with_keys(
entry_data, path_keys, med_keys
)
@staticmethod
def validate_entry_completeness_with_keys(
entry_data: dict[str, Any],
pathology_keys: list[str],
medicine_keys: list[str],
) -> tuple[bool, list[str]]:
"""
Validate that an entry has the minimum required data using explicit keys.
Args:
entry_data: Dictionary containing entry data
pathology_keys: Keys representing pathology scores (numeric, >0 meaningful)
medicine_keys: Keys representing medicine taken flags (0/1 boolean)
Returns:
Tuple of (is_complete, list_of_missing_fields)
"""
missing_fields = []
# Check required fields
missing_fields: list[str] = []
if not entry_data.get("date"):
missing_fields.append("Date")
# Check that at least one pathology or medicine is recorded
has_pathology_data = any(
entry_data.get(key, 0) > 0
for key in entry_data
if not key.endswith("_doses") and key not in ["date", "note"]
)
def _as_int(v: Any) -> int:
try:
return int(v)
except Exception:
try:
return int(float(v))
except Exception:
return 0
has_medicine_data = any(
entry_data.get(key, 0) > 0
for key in entry_data
if not key.endswith("_doses") and key not in ["date", "note"]
)
has_pathology = any(_as_int(entry_data.get(k, 0)) > 0 for k in pathology_keys)
has_medicine = any(_as_int(entry_data.get(k, 0)) == 1 for k in medicine_keys)
if not (has_pathology_data or has_medicine_data):
if not (has_pathology or has_medicine):
missing_fields.append("At least one pathology score or medicine entry")
return len(missing_fields) == 0, missing_fields