Refactor graph generation

This commit is contained in:
George Lacey 2021-05-21 17:45:14 +01:00
parent 89b3a33745
commit 3d6f8c8ed9
5 changed files with 26 additions and 35 deletions

View File

@ -1,26 +1,22 @@
window.addEventListener("DOMContentLoaded", function () { window.addEventListener("DOMContentLoaded", function () {
$.getJSON( "repo_daily.json", function( json ) { $.getJSON( "repo_daily.json", function( json ) {
set_daily_graph(json); draw_time_graph("daily_backup_size", json.repos, json.dates, json.units);
}); });
}, false); }, false);
function set_daily_graph(repoDict) { function draw_time_graph(chartID, repos, dateLabels, sizeUnits) {
const labels = repoDict.date_labels;
const y_units = repoDict.units
var datasets = [] var datasets = []
repoDict.repos.forEach(function (repo) { repos.forEach(function (repo) {
datasets.push({ datasets.push({
label: repo.label, label: repo.label,
data: repo.daily_size, data: repo.daily_size,
fill: false, fill: false,
tension: 0.1,
borderColor: 'rgb(75, 192, 192)' borderColor: 'rgb(75, 192, 192)'
}); });
}) })
const data = { const data = {
labels: labels, labels: dateLabels,
datasets: datasets datasets: datasets
}; };
@ -32,8 +28,9 @@ function set_daily_graph(repoDict) {
tooltip: { tooltip: {
callbacks: { callbacks: {
label: function (context) { label: function (context) {
if (context.parsed.y !== null) { const yValue = context.parsed.y
return `${context.parsed.y} ${y_units}` if (yValue !== null) {
return `${yValue} ${sizeUnits}`
} else { } else {
return "" return ""
} }
@ -53,7 +50,7 @@ function set_daily_graph(repoDict) {
}, },
ticks: { ticks: {
callback: function (value, index, values) { callback: function (value, index, values) {
return `${value} ${y_units}` return `${value} ${sizeUnits}`
} }
} }
} }
@ -62,11 +59,7 @@ function set_daily_graph(repoDict) {
} }
var myChart = new Chart( var myChart = new Chart(
document.getElementById('backup_csize_hourly'), document.getElementById(chartID),
config config
); );
} }
function getBaseLog(x, y) {
return Math.log(y) / Math.log(x);
}

View File

@ -55,7 +55,7 @@
</div> </div>
{% endfor %} {% endfor %}
<div style="width: 600px;" class="repo-container shadow rounded bg-primary overflow-hidden"> <div style="width: 600px;" class="repo-container shadow rounded bg-primary overflow-hidden">
<canvas id="backup_csize_hourly" width="400" height="200"></canvas> <canvas id="daily_backup_size" width="400" height="200"></canvas>
</div> </div>
{% else %} {% else %}
<div style="width: 600px;" class="repo-container shadow rounded bg-primary overflow-hidden"> <div style="width: 600px;" class="repo-container shadow rounded bg-primary overflow-hidden">

View File

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

View File

@ -1,8 +1,22 @@
from datetime import datetime, timedelta
from django.http import JsonResponse from django.http import JsonResponse
from ..models import Repo from ..models import Repo
from . import repo_daily_dict from ..utility import data
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)]))
max_repo_size = max(repo.latest_archive().cache.unique_csize for repo in repo_list)
_, max_unit = data.convert_bytes(max_repo_size)
repo_dicts = [repo.daily_dict(max_unit, n_days) for repo in repo_list]
return {
"dates": date_labels,
"repos": repo_dicts,
"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))

View File

@ -1,7 +1,5 @@
from django.shortcuts import render, get_object_or_404 from django.shortcuts import render, get_object_or_404
from datetime import datetime, timedelta
from ..models import Repo, Location from ..models import Repo, Location
from ..utility import data
def index(request): def index(request):
@ -15,20 +13,6 @@ def index(request):
return render(request, 'borg/index.html', context) return render(request, 'borg/index.html', context)
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)]))
max_repo_size = max(repo.latest_archive().cache.unique_csize for repo in repo_list)
_, max_unit = data.convert_bytes(max_repo_size)
repo_dicts = [repo.daily_dict(max_unit, n_days) for repo in repo_list]
return {
"date_labels": date_labels,
"repos": repo_dicts,
"units": max_unit
}
def repo(request, repo_label: str): def repo(request, repo_label: str):
s_repo = get_object_or_404(Repo, label__label=repo_label) s_repo = get_object_or_404(Repo, label__label=repo_label)
return render(request, 'borg/repo.html', {'repo': s_repo}) return render(request, 'borg/repo.html', {'repo': s_repo})