Refactor graph generation
This commit is contained in:
parent
89b3a33745
commit
3d6f8c8ed9
|
@ -1,26 +1,22 @@
|
|||
window.addEventListener("DOMContentLoaded", function () {
|
||||
$.getJSON( "repo_daily.json", function( json ) {
|
||||
set_daily_graph(json);
|
||||
draw_time_graph("daily_backup_size", json.repos, json.dates, json.units);
|
||||
});
|
||||
}, false);
|
||||
|
||||
function set_daily_graph(repoDict) {
|
||||
const labels = repoDict.date_labels;
|
||||
const y_units = repoDict.units
|
||||
|
||||
function draw_time_graph(chartID, repos, dateLabels, sizeUnits) {
|
||||
var datasets = []
|
||||
repoDict.repos.forEach(function (repo) {
|
||||
repos.forEach(function (repo) {
|
||||
datasets.push({
|
||||
label: repo.label,
|
||||
data: repo.daily_size,
|
||||
fill: false,
|
||||
tension: 0.1,
|
||||
borderColor: 'rgb(75, 192, 192)'
|
||||
});
|
||||
})
|
||||
|
||||
const data = {
|
||||
labels: labels,
|
||||
labels: dateLabels,
|
||||
datasets: datasets
|
||||
};
|
||||
|
||||
|
@ -32,8 +28,9 @@ function set_daily_graph(repoDict) {
|
|||
tooltip: {
|
||||
callbacks: {
|
||||
label: function (context) {
|
||||
if (context.parsed.y !== null) {
|
||||
return `${context.parsed.y} ${y_units}`
|
||||
const yValue = context.parsed.y
|
||||
if (yValue !== null) {
|
||||
return `${yValue} ${sizeUnits}`
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
|
@ -53,7 +50,7 @@ function set_daily_graph(repoDict) {
|
|||
},
|
||||
ticks: {
|
||||
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(
|
||||
document.getElementById('backup_csize_hourly'),
|
||||
document.getElementById(chartID),
|
||||
config
|
||||
);
|
||||
}
|
||||
|
||||
function getBaseLog(x, y) {
|
||||
return Math.log(y) / Math.log(x);
|
||||
}
|
|
@ -55,7 +55,7 @@
|
|||
</div>
|
||||
{% endfor %}
|
||||
<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>
|
||||
{% else %}
|
||||
<div style="width: 600px;" class="repo-container shadow rounded bg-primary overflow-hidden">
|
||||
|
|
|
@ -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 .json import repo_daily_json
|
||||
|
|
|
@ -1,8 +1,22 @@
|
|||
from datetime import datetime, timedelta
|
||||
from django.http import JsonResponse
|
||||
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):
|
||||
repo_list = Repo.objects.all()
|
||||
return JsonResponse(repo_daily_dict(repo_list, 31))
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
from django.shortcuts import render, get_object_or_404
|
||||
from datetime import datetime, timedelta
|
||||
from ..models import Repo, Location
|
||||
from ..utility import data
|
||||
|
||||
|
||||
def index(request):
|
||||
|
@ -15,20 +13,6 @@ def index(request):
|
|||
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):
|
||||
s_repo = get_object_or_404(Repo, label__label=repo_label)
|
||||
return render(request, 'borg/repo.html', {'repo': s_repo})
|
||||
|
|
Loading…
Reference in New Issue
Block a user