backups: Format better when showing archive time delete page

- When archives are listed using list_archives() method, return datetime objects
instead of pre-formatted strings. datetime objects can be compared easily and
shown in a more human readable format.

Tests:

- Unit tests pass.

- Backups with a future date are ignored when considering recent backup
times (when they logged to console).

- Most recent scheduled backup times are retrieved correctly (when they logged
to console).

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2024-12-24 09:17:04 -08:00 committed by James Valleroy
parent d423b40239
commit 91270331cc
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
3 changed files with 13 additions and 13 deletions

View File

@ -3,6 +3,7 @@
import abc
import contextlib
import datetime
import io
import logging
import os
@ -121,6 +122,12 @@ class BaseBorgRepository(abc.ABC):
"""Return list of archives in this repository."""
archives = privileged.list_repo(
self.borg_path, self._get_encryption_passpharse())['archives']
for archive in archives:
archive['time'] = datetime.datetime.strptime(
archive['time'], '%Y-%m-%dT%H:%M:%S.%f')
archive['start'] = datetime.datetime.strptime(
archive['start'], '%Y-%m-%dT%H:%M:%S.%f')
return sorted(archives, key=lambda archive: archive['start'],
reverse=True)

View File

@ -242,14 +242,11 @@ class Schedule:
archive['comment'] = comment
start_time = datetime.strptime(archive['start'],
'%Y-%m-%dT%H:%M:%S.%f')
if start_time > now:
if archive['start'] > now:
# This backup was taken when clock was set in future. Ignore it
# to ensure backups continue to be taken.
continue
archive['start'] = start_time
scheduled_archives.append(archive)
return scheduled_archives

View File

@ -84,16 +84,12 @@ def _get_archives_from_test_data(data):
if isinstance(archive_time, str):
archive_time = datetime.strptime(archive_time,
'%Y-%m-%d %H:%M:%S+0000')
comment = json.dumps({'type': 'scheduled', 'periods': item['periods']})
archive = {
'comment':
json.dumps({
'type': 'scheduled',
'periods': item['periods']
}),
'start':
archive_time.strftime('%Y-%m-%dT%H:%M:%S.%f'),
'name':
f'archive-{index}'
'comment': comment,
'start': archive_time,
'name': f'archive-{index}'
}
archives.append(archive)