Retrieve monthly archives more efficiently

This commit is contained in:
George Lacey 2022-04-11 10:03:23 +01:00
parent aa4f31df23
commit 3031454682
2 changed files with 16 additions and 14 deletions

View File

@ -1,4 +1,6 @@
from django.db import models from django.db import models
from django.db.models import DateTimeField
from django.db.models.functions import Trunc
from datetime import datetime, timedelta from datetime import datetime, timedelta
from ..utility.time import seconds_to_string, subtract_months from ..utility.time import seconds_to_string, subtract_months
from ..utility.data import bytes_to_string, convert_bytes 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): def monthly_archives(self, n_months: int = 12):
archives = [] archives = []
for month in range(n_months): archive_set = self.archive_set.all().order_by('-start')
current_date = subtract_months(datetime.utcnow(), month)
archive_current_month = self.archive_set.all() \ current_date = datetime.utcnow()
.filter(start__year=current_date.year, for archive in archive_set:
start__month=current_date.month) \ if len(archives) >= n_months:
.order_by('-start') break
if len(archive_current_month) > 0: if archive.start.year == current_date.year and archive.start.month == current_date.month:
archives.append(archive_current_month[0]) archives.append(archive)
else: current_date = subtract_months(current_date, 1)
archives.append(None)
return archives[::-1] return archives[::-1]
def archives_on_dates(self, dates: list): def archives_on_dates(self, dates: list):

View File

@ -10,7 +10,7 @@ urlpatterns = [
path('repo-list.json', cache_page(60)(views.repo_list_json), name='repo list'), path('repo-list.json', cache_page(60)(views.repo_list_json), name='repo list'),
# Repo # Repo
path('repo/<str:repo_label>/monthly-size.json', cache_page(3600)(views.repo_monthly_size_json), path('repo/<str:repo_label>/monthly-size.json', cache_page(60)(views.repo_monthly_size_json),
name='repo size time series'), name='repo size time series'),
path('repo/<str:repo_label>.json', cache_page(60)(views.repo_json), name='repo json'), path('repo/<str:repo_label>.json', cache_page(60)(views.repo_json), name='repo json'),
path('repo/<str:repo_label>/latest-backup.json', cache_page(60)(views.repo_latest_backup_json), name='repo json'), path('repo/<str:repo_label>/latest-backup.json', cache_page(60)(views.repo_latest_backup_json), name='repo json'),