From 2aa591769184f41c55e711b9852dde770abf6875 Mon Sep 17 00:00:00 2001 From: George Lacey Date: Thu, 6 May 2021 13:29:20 +0100 Subject: [PATCH] Move models to separate files - Implement last backup method --- borgweb/borg/models.py | 55 --------------------------------- borgweb/borg/models/__init__.py | 5 +++ borgweb/borg/models/archive.py | 17 ++++++++++ borgweb/borg/models/cache.py | 15 +++++++++ borgweb/borg/models/error.py | 11 +++++++ borgweb/borg/models/label.py | 13 ++++++++ borgweb/borg/models/repo.py | 53 +++++++++++++++++++++++++++++++ 7 files changed, 114 insertions(+), 55 deletions(-) delete mode 100644 borgweb/borg/models.py create mode 100644 borgweb/borg/models/__init__.py create mode 100644 borgweb/borg/models/archive.py create mode 100644 borgweb/borg/models/cache.py create mode 100644 borgweb/borg/models/error.py create mode 100644 borgweb/borg/models/label.py create mode 100644 borgweb/borg/models/repo.py diff --git a/borgweb/borg/models.py b/borgweb/borg/models.py deleted file mode 100644 index 3e9aee3..0000000 --- a/borgweb/borg/models.py +++ /dev/null @@ -1,55 +0,0 @@ -from django.db import models - - -class Repo(models.Model): - fingerprint = models.TextField() - location = models.TextField() - last_modified = models.DateTimeField() - - class Meta: - db_table = 'repo' - - -class Archive(models.Model): - fingerprint = models.TextField() - repo_id = models.ForeignKey(Repo, on_delete=models.CASCADE) - name = models.TextField() - start = models.DateTimeField() - end = models.DateTimeField() - file_count = models.IntegerField() - original_size = models.IntegerField() - compressed_size = models.IntegerField() - deduplicated_size = models.IntegerField() - - class Meta: - db_table = 'archive' - - -class Cache(models.Model): - archive_id = models.ForeignKey(Archive, on_delete=models.CASCADE) - total_chunks = models.IntegerField() - total_csize = models.IntegerField() - total_size = models.IntegerField() - total_unique_chunks = models.IntegerField() - unique_csize = models.IntegerField() - unique_size = models.IntegerField() - - class Meta: - db_table = 'cache' - - -class Label(models.Model): - repo_id = models.ForeignKey(Repo, on_delete=models.CASCADE) - label = models.TextField() - - class Meta: - db_table = 'label' - - -class Error(models.Model): - label_id = models.ForeignKey(Label, on_delete=models.CASCADE) - error = models.TextField() - time = models.DateTimeField() - - class Meta: - db_table = 'error' diff --git a/borgweb/borg/models/__init__.py b/borgweb/borg/models/__init__.py new file mode 100644 index 0000000..dcbac15 --- /dev/null +++ b/borgweb/borg/models/__init__.py @@ -0,0 +1,5 @@ +from .repo import Repo +from .archive import Archive +from .cache import Cache +from .label import Label +from .error import Error diff --git a/borgweb/borg/models/archive.py b/borgweb/borg/models/archive.py new file mode 100644 index 0000000..c128bc8 --- /dev/null +++ b/borgweb/borg/models/archive.py @@ -0,0 +1,17 @@ +from django.db import models +from . import Repo + + +class Archive(models.Model): + fingerprint = models.TextField() + repo = models.ForeignKey(Repo, on_delete=models.CASCADE, related_name='archives') + name = models.TextField() + start = models.DateTimeField() + end = models.DateTimeField() + file_count = models.IntegerField() + original_size = models.IntegerField() + compressed_size = models.IntegerField() + deduplicated_size = models.IntegerField() + + class Meta: + db_table = 'archive' diff --git a/borgweb/borg/models/cache.py b/borgweb/borg/models/cache.py new file mode 100644 index 0000000..a758c9a --- /dev/null +++ b/borgweb/borg/models/cache.py @@ -0,0 +1,15 @@ +from django.db import models +from . import Archive + + +class Cache(models.Model): + archive = models.OneToOneField(Archive, on_delete=models.CASCADE) + total_chunks = models.IntegerField() + total_csize = models.IntegerField() + total_size = models.IntegerField() + total_unique_chunks = models.IntegerField() + unique_csize = models.IntegerField() + unique_size = models.IntegerField() + + class Meta: + db_table = 'cache' \ No newline at end of file diff --git a/borgweb/borg/models/error.py b/borgweb/borg/models/error.py new file mode 100644 index 0000000..dd115c3 --- /dev/null +++ b/borgweb/borg/models/error.py @@ -0,0 +1,11 @@ +from django.db import models +from . import Label + + +class Error(models.Model): + label_id = models.ForeignKey(Label, on_delete=models.CASCADE) + error = models.TextField() + time = models.DateTimeField() + + class Meta: + db_table = 'error' \ No newline at end of file diff --git a/borgweb/borg/models/label.py b/borgweb/borg/models/label.py new file mode 100644 index 0000000..bd7bb5a --- /dev/null +++ b/borgweb/borg/models/label.py @@ -0,0 +1,13 @@ +from django.db import models +from . import Repo + + +class Label(models.Model): + repo = models.OneToOneField(Repo, on_delete=models.CASCADE) + label = models.TextField() + + class Meta: + db_table = 'label' + + def __str__(self): + return self.label \ No newline at end of file diff --git a/borgweb/borg/models/repo.py b/borgweb/borg/models/repo.py new file mode 100644 index 0000000..968b3d8 --- /dev/null +++ b/borgweb/borg/models/repo.py @@ -0,0 +1,53 @@ +from django.db import models +from datetime import datetime + + +class Repo(models.Model): + fingerprint = models.TextField() + location = models.TextField() + last_modified = models.DateTimeField() + + class Meta: + db_table = 'repo' + + def last_backup(self): + latest = self.archives.order_by('-start')[0].start.replace(tzinfo=None) + seconds_since = int((datetime.utcnow() - latest).total_seconds()) + return f"{self.seconds_to_string(seconds_since)} ago" + + @staticmethod + def seconds_to_string(seconds: int, short=False, truncate=False): + seconds = int(seconds) + increments = [('year', 'yr', 31557600), + ('week', 'wk', 604800), + ('day', 'day', 86400), + ('hour', 'hr', 3600), + ('minute', 'min', 60), + ('second', 'sec', 1)] + + if seconds == 0: + if short: + return f"0 {increments[-1][1]}s" + else: + return f"0 {increments[-1][0]}s" + + time_string = "" + + remainder = seconds + for st, sst, s in increments: + if remainder == 0: + break + if short: + st = sst + if remainder < s or remainder == 0: + continue + else: + exact, remainder = divmod(remainder, s) + if exact > 1: + time_string += f"{exact} {st}s, " + else: + time_string += f"{exact} {st}, " + if truncate: + break + return time_string.strip().strip(',')[::-1].replace(' ,', ' dna ', 1)[::-1] # lmao +