Retrieve monthly archives more efficiently
This commit is contained in:
parent
aa4f31df23
commit
3031454682
|
@ -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):
|
||||||
|
@ -136,9 +138,9 @@ class Repo(models.Model):
|
||||||
archives = []
|
archives = []
|
||||||
for hour in range(n_hours):
|
for hour in range(n_hours):
|
||||||
current_hour = datetime.utcnow() - timedelta(hours=hour)
|
current_hour = datetime.utcnow() - timedelta(hours=hour)
|
||||||
archives_hour = self.archive_set.all()\
|
archives_hour = self.archive_set.all() \
|
||||||
.filter(start__date=current_hour.date())\
|
.filter(start__date=current_hour.date()) \
|
||||||
.filter(start__hour=current_hour.hour)\
|
.filter(start__hour=current_hour.hour) \
|
||||||
.order_by('-start')
|
.order_by('-start')
|
||||||
if len(archives_hour) > 0:
|
if len(archives_hour) > 0:
|
||||||
archives.append(archives_hour[0])
|
archives.append(archives_hour[0])
|
||||||
|
|
|
@ -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'),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user