Move models to separate files

- Implement last backup method
This commit is contained in:
George Lacey 2021-05-06 13:29:20 +01:00
parent f0b685d802
commit 2aa5917691
7 changed files with 114 additions and 55 deletions

View File

@ -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'

View File

@ -0,0 +1,5 @@
from .repo import Repo
from .archive import Archive
from .cache import Cache
from .label import Label
from .error import Error

View File

@ -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'

View File

@ -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'

View File

@ -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'

View File

@ -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

View File

@ -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