Implement repo size json request

This commit is contained in:
George Lacey 2022-04-07 10:32:17 +01:00
parent 5952578754
commit ab92a70685
3 changed files with 22 additions and 2 deletions

View File

@ -8,6 +8,7 @@ urlpatterns = [
path('repo_daily.json', cache_page(3600)(views.repo_daily_json), name='daily repo json'),
path('repo_monthly.json', cache_page(3600 * 12)(views.repo_monthly_json), name='monthly repo json'),
path('repo_list.json', cache_page(60)(views.repo_list_json), name='repo list'),
path('repo/<str:repo_label>/size.json', cache_page(60)(views.repo_size_json), 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>', cache_page(60)(views.repo), name='repo'),
path('post/repo', views.post_repo, name='post repo'),

View File

@ -1,3 +1,3 @@
from .views import index, repo, axes
from .post import post_repo, post_archive, post_error, post_location
from .json import repo_daily_json, repo_monthly_json, repo_list_json, repo_json
from .json import repo_daily_json, repo_monthly_json, repo_list_json, repo_json, repo_size_json

View File

@ -17,6 +17,26 @@ def repo_json(request, repo_label):
return JsonResponse(repo_dict)
def repo_size_json(request, repo_label, months_ago: int = 12):
repo = get_object_or_404(Repo, label__label=repo_label)
date_labels = [date.strftime("%b %Y") for date in last_day_previous_months(months_ago)]
max_unit = get_units([repo])
repo_dict = {"id": repo.id,
"label": repo.label.label,
"size": repo.size_on_months(max_unit, months_ago)}
response_dict = {
"dates": date_labels,
"repo": repo_dict,
"units": max_unit
}
return JsonResponse(response_dict)
def repo_list_json(request):
return JsonResponse({'labels': [repo.label.label for repo in Repo.objects.all()]})
@ -70,4 +90,3 @@ def get_units(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)
return max_unit