From 3031454682c5207957d18b40f32666b2aa1f1179 Mon Sep 17 00:00:00 2001 From: George Lacey Date: Mon, 11 Apr 2022 10:03:23 +0100 Subject: [PATCH] Retrieve monthly archives more efficiently --- borgweb/borg/models/repo.py | 28 +++++++++++++++------------- borgweb/borg/urls.py | 2 +- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/borgweb/borg/models/repo.py b/borgweb/borg/models/repo.py index b635efa..5d7d59c 100644 --- a/borgweb/borg/models/repo.py +++ b/borgweb/borg/models/repo.py @@ -1,4 +1,6 @@ from django.db import models +from django.db.models import DateTimeField +from django.db.models.functions import Trunc from datetime import datetime, timedelta from ..utility.time import seconds_to_string, subtract_months from ..utility.data import bytes_to_string, convert_bytes @@ -109,16 +111,16 @@ class Repo(models.Model): def monthly_archives(self, n_months: int = 12): archives = [] - for month in range(n_months): - current_date = subtract_months(datetime.utcnow(), month) - archive_current_month = self.archive_set.all() \ - .filter(start__year=current_date.year, - start__month=current_date.month) \ - .order_by('-start') - if len(archive_current_month) > 0: - archives.append(archive_current_month[0]) - else: - archives.append(None) + archive_set = self.archive_set.all().order_by('-start') + + current_date = datetime.utcnow() + for archive in archive_set: + if len(archives) >= n_months: + break + if archive.start.year == current_date.year and archive.start.month == current_date.month: + archives.append(archive) + current_date = subtract_months(current_date, 1) + return archives[::-1] def archives_on_dates(self, dates: list): @@ -136,9 +138,9 @@ class Repo(models.Model): archives = [] for hour in range(n_hours): current_hour = datetime.utcnow() - timedelta(hours=hour) - archives_hour = self.archive_set.all()\ - .filter(start__date=current_hour.date())\ - .filter(start__hour=current_hour.hour)\ + archives_hour = self.archive_set.all() \ + .filter(start__date=current_hour.date()) \ + .filter(start__hour=current_hour.hour) \ .order_by('-start') if len(archives_hour) > 0: archives.append(archives_hour[0]) diff --git a/borgweb/borg/urls.py b/borgweb/borg/urls.py index dc682e1..79da24e 100644 --- a/borgweb/borg/urls.py +++ b/borgweb/borg/urls.py @@ -10,7 +10,7 @@ urlpatterns = [ path('repo-list.json', cache_page(60)(views.repo_list_json), name='repo list'), # Repo - path('repo//monthly-size.json', cache_page(3600)(views.repo_monthly_size_json), + path('repo//monthly-size.json', cache_page(60)(views.repo_monthly_size_json), name='repo size time series'), path('repo/.json', cache_page(60)(views.repo_json), name='repo json'), path('repo//latest-backup.json', cache_page(60)(views.repo_latest_backup_json), name='repo json'),