From be4e883b12d7da3f3d9b77d123610c4c09292b7c Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 26 Jul 2024 10:10:56 +0000 Subject: [PATCH] feature(backup): autobackup slice detection --- selfprivacy_api/backup/__init__.py | 38 +++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/selfprivacy_api/backup/__init__.py b/selfprivacy_api/backup/__init__.py index f00fede..d4a4f88 100644 --- a/selfprivacy_api/backup/__init__.py +++ b/selfprivacy_api/backup/__init__.py @@ -278,7 +278,7 @@ class Backups: return Storage.get_cached_snapshot_by_id(snapshot.id) @staticmethod - def _auto_snaps(service): + def _auto_snaps(service) -> List[Snapshot]: return [ snap for snap in Backups.get_snapshots(service) @@ -751,3 +751,39 @@ class Backups: ServiceStatus.FAILED, ]: raise NotDeadError(service) + + @staticmethod + def last_autobackup_slice() -> List[Snapshot]: + """ + Guarantees that the slice is valid, ie, it has an api snapshot too + """ + slice: List[Snapshot] = [] + + # We need ones that were made in the same autobackup session. + # For this we step through api snapshots + api_autosnaps = Backups._auto_snaps(ServiceManager()) + api_autosnaps.sort(key=lambda x: x.created_at, reverse=True) + + api_snap = api_autosnaps[0] + period_minutes = Backups.autobackup_period_minutes() + if period_minutes is None: + # this is potentially problematic. Maybe add more metainfo for autobackup snapshots, like a slice id or something? + period_minutes = ( + 24 * 60 + ) # we ASSUME that picking latest snap of the same day is safe enough + + for service in ServiceManager.get_all_services(): + if isinstance(service, ServiceManager): + continue + snaps = Backups._auto_snaps(service) + snaps.sort(key=lambda x: x.created_at, reverse=True) + for snap in snaps: + if snap.created_at < api_snap.created_at + timedelta( + minutes=period_minutes + ) and snap.created_at > api_snap.created_at - timedelta( + minutes=period_minutes + ): + slice.append(snap) + break + + return slice