Allow retrieval of archives based on given dates

This commit is contained in:
George Lacey 2021-05-21 19:40:20 +01:00
parent a9cd4f1c65
commit 76fb5b3763
3 changed files with 17 additions and 41 deletions

View File

@ -79,10 +79,10 @@ class Repo(models.Model):
errors = self.label.errors.all().filter(time__gt=days_ago) errors = self.label.errors.all().filter(time__gt=days_ago)
return errors return errors
def get_archive_days(self): def get_archive_days(self, count: int = 31):
current_day = datetime.utcnow().day current_day = datetime.utcnow().day
days = [] days = []
for day in reversed(range(1, 31)): for day in reversed(range(1, count)):
try: try:
cday = datetime.utcnow().replace(day=day) cday = datetime.utcnow().replace(day=day)
except ValueError: except ValueError:
@ -94,12 +94,12 @@ class Repo(models.Model):
days.append(len(cday_archives) > 0) days.append(len(cday_archives) > 0)
return days return days
def daily_dict(self, units, n_hours: int = 7): def size_on_dates(self, units, dates: list):
archives = self.daily_archives(n_hours) archives = self.archives_on_dates(dates)
return { return {
"id": self.id, "id": self.id,
"label": self.label.label, "label": self.label.label,
"daily_size": list(reversed(self.series_csize(archives, units))) "size": list(self.series_csize(archives, units))
} }
@staticmethod @staticmethod
@ -111,43 +111,16 @@ class Repo(models.Model):
return [convert_bytes(archive.cache.unique_csize, units)[0] return [convert_bytes(archive.cache.unique_csize, units)[0]
if archive is not None else None for archive in archives] if archive is not None else None for archive in archives]
@staticmethod
def series_csize_pretty(archives):
return [archive.cache.unique_csize if archive is not None else None for archive in archives]
@staticmethod
def series_success_string(archives):
return ''.join(['H' if archive is not None else '-' for archive in archives])
def hourly_archive_string(self): def hourly_archive_string(self):
return ''.join(['H' if archive is not None else '-' for archive in self.hourly_archives(8)]) return ''.join(['H' if archive is not None else '-' for archive in self.hourly_archives(8)])
def monthly_archives(self, n_months: int = 12): def archives_on_dates(self, dates: list):
archives = [] archives = []
for month in range(n_months): archive_queryset = self.archive_set.all()
current_date = subtract_months(datetime.utcnow().date(), month) for date in dates:
print(current_date) date_archives = archive_queryset.filter(start__date=date).order_by('-start')
archive_current_month = self.archive_set.all()\ if date_archives.exists():
.filter(start__year__gte=current_date.year, archives.append(date_archives[0])
start__month__gte=current_date.month,
start__year__lte=current_date.year,
start__month__lte=current_date.month)\
.order_by('-start')
if len(archive_current_month) > 0:
archives.append(archive_current_month[0])
else:
archives.append(None)
return archives
def daily_archives(self, n_days: int = 7):
archives = []
for day in range(n_days):
current_date = (datetime.utcnow() - timedelta(days=day)).date()
archive_current_date = self.archive_set.all()\
.filter(start__date=current_date)\
.order_by('-start')
if len(archive_current_date) > 0:
archives.append(archive_current_date[0])
else: else:
archives.append(None) archives.append(None)
return archives return archives

View File

@ -9,7 +9,7 @@ function draw_time_graph(chartID, repos, dateLabels, sizeUnits) {
repos.forEach(function (repo) { repos.forEach(function (repo) {
datasets.push({ datasets.push({
label: repo.label, label: repo.label,
data: repo.daily_size, data: repo.size,
fill: false, fill: false,
borderColor: 'rgb(75, 192, 192)' borderColor: 'rgb(75, 192, 192)'
}); });

View File

@ -5,11 +5,13 @@ from ..utility import data
def repo_daily_dict(repo_list, n_days=14): def repo_daily_dict(repo_list, n_days=14):
date_labels = list(reversed([(datetime.utcnow() - timedelta(days=day)).strftime("%d %b") for day in range(n_days)])) dates = [(datetime.utcnow() - timedelta(days=day)) for day in range(n_days)][::-1]
date_labels = list([day.strftime("%d %b") for day in dates])
max_repo_size = max(repo.latest_archive().cache.unique_csize for repo in repo_list) max_repo_size = max(repo.latest_archive().cache.unique_csize for repo in repo_list)
_, max_unit = data.convert_bytes(max_repo_size) _, max_unit = data.convert_bytes(max_repo_size)
repo_dicts = [repo.daily_dict(max_unit, n_days) for repo in repo_list] repo_dicts = [repo.size_on_dates(max_unit, dates) for repo in repo_list]
return { return {
"dates": date_labels, "dates": date_labels,
@ -17,6 +19,7 @@ def repo_daily_dict(repo_list, n_days=14):
"units": max_unit "units": max_unit
} }
def repo_daily_json(request): def repo_daily_json(request):
repo_list = Repo.objects.all() repo_list = Repo.objects.all()
return JsonResponse(repo_daily_dict(repo_list, 31)) return JsonResponse(repo_daily_dict(repo_list, 31))