Compare commits

..

4 Commits

Author SHA1 Message Date
Jorge Pastor 33293d2ba8 musicias show_history 2024-08-20 10:46:04 +02:00
Jorge Pastor 20bcb83c83 musicias show_history 2024-08-14 13:04:26 +02:00
Jorge Pastor 14ff506a0d musicias show_history basic 2024-08-09 13:21:10 +02:00
Jorge Pastor 4ff5abe8f6 musicias show_history basic 2024-08-09 13:21:01 +02:00
8 changed files with 337 additions and 14 deletions

View File

@ -318,6 +318,10 @@ msgstr "És el primer cop que accedeixes, et donem la benvinguda!"
msgid " The disk space of resources is updated weekly "
msgstr "L'espai en disc dels recursos es va actualitzant setmanalment."
#: templates/musician/dashboard.html:47
msgid "Show history"
msgstr "Mostrar historial"
#: templates/musician/database_list.html:21
#: templates/musician/mailbox_list.html:30 templates/musician/saas_list.html:19
#: templates/musician/webapps/webapp_list.html:25

View File

@ -320,6 +320,10 @@ msgstr "Es la primera vez que accedes: ¡te damos la bienvenida!"
msgid " The disk space of resources is updated weekly "
msgstr "El espacio en disco de los recursos se actualiza semanalmente"
#: templates/musician/dashboard.html:47
msgid "Show history"
msgstr "Mostrar historial"
#: templates/musician/database_list.html:21
#: templates/musician/mailbox_list.html:30 templates/musician/saas_list.html:19
#: templates/musician/webapps/webapp_list.html:25

View File

@ -37,10 +37,22 @@
</div>
<ul class="list-group">
{% for name, obj_data in account.objects.items %}
<li class="list-group-item d-flex justify-content-between align-items-center">
{{ name }}
<span class="badge badge-primary badge-pill">{{ obj_data.ac.used }} {{ obj_data.ac.unit }}</span>
</li>
{% if obj_data.ac != None %}
<li class="list-group-item d-flex justify-content-between align-items-center">
<div class="row w-100 justify-content-between">
<div class="col-4">
{{ name }}
</div>
<div class="col-4 text-center">
<a href="{% url 'musician:dashboard-history' obj_data.ac.id %}" target="_blank">{% trans "Show history" %} <i class="fas fa-clock"></i></a>
</div>
<div class="col-3"></div>
<div class="col-1">
<span class="badge badge-primary badge-pill">{{ obj_data.ac.used }} {{ obj_data.ac.unit }}</span>
</div>
</div>
</li>
{% endif %}
{% endfor %}
</ul>
</div>

View File

@ -0,0 +1,251 @@
{% load i18n utils static %}
<html>
<head>
<title>Resource history</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" type="text/css" href="{% static "orchestra/css/dancing-dots.css" %}"/>
<script src="{% static "admin/js/jquery.js" %}" type="text/javascript"></script>
<script src="{% static "orchestra/js/highcharts/stock/highstock.js" %}" type="text/javascript"></script>
<script src="{% static "orchestra/js/highcharts/modules/exporting.js" %}" type="text/javascript"></script>
<script>
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}
function plot_charts(url) {
charts = {
series: function (div, i, seriesOptions, resource) {
$(div).highcharts('StockChart', {
chart: {
backgroundColor: (i % 2 ? "#EDF3FE" : "#FFFFFF")
},
rangeSelector: {
selected: 4
},
title: {
text: resource['content_type'].capitalize() + ' ' +
resource['verbose_name'].toLowerCase() + ' ' +
resource['aggregation'] +
(div.indexOf('aggregate') > 0 ? ' (aggregated)': '')
},
xAxis: {
ordinal: false
},
yAxis: {
labels: {
formatter: function () {
return this.value + ' ' + resource['unit'];
}
},
plotLines: [{
value: 0,
width: 2,
color: 'silver'
}],
min: 0,
},
/*legend: {
align: 'right',
x: -30,
verticalAlign: 'top',
y: 25,
floating: true,
backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || 'white',
borderColor: '#CCC',
borderWidth: 1,
shadow: false,
enabled: true
},
rangeSelector: {
enabled: false
},*/
tooltip: {
pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y:.3f} ' +
resource['unit']+ '</b><br/>',
valueDecimals: 3
},
series: seriesOptions
});
},
columns: function (div, i, seriesOptions, resource){
$(div).highcharts({
chart: {
type: 'column',
backgroundColor: (i % 2 ? "#EDF3FE" : "#FFFFFF")
},
title: {
text: resource['content_type'].capitalize() + ' ' +
resource['verbose_name'].toLowerCase() + ' ' +
resource['aggregation'] +
(div.indexOf('aggregate') > 0 ? ' (aggregated)': '')
},
xAxis: {
categories: resource['dates']
},
yAxis: {
min: 0,
title: {
text: resource['unit']
},
stackLabels: {
enabled: true,
style: {
fontWeight: 'bold',
color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
},
formatter: function () {
return this.total + ' ' + resource['unit'];
}
}
},
legend: {
align: 'right',
x: -30,
verticalAlign: 'top',
y: 25,
floating: true,
backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || 'white',
borderColor: '#CCC',
borderWidth: 1,
shadow: false
},
tooltip: {
formatter: function () {
var s = ['<b>' + this.x + '</b>'];
$.each(this.points, function(i, point) {
s.push('<span style="color:' + this.series.color + '">' + this.series.name + ': ' + this.y + ' ' + resource['unit']);
});
s.push('<b>Total: ' + this.points[0].total + ' ' + resource['unit'] + '</b>');
return s.join('<br>');
},
valueDecimals: 3,
shared: true
},
plotOptions: {
column: {
stacking: 'normal',
dataLabels: {
enabled: true,
color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
style: {
textShadow: '0 0 3px black'
},
formatter: function () {
return this.series.name + ': ' + this.y.toFixed(3) + ' ' + resource['unit'];
}
}
}
},
series: seriesOptions
})
}
};
$.getJSON(url, function(data) {
var dataLength = data.length;
$("#charts").empty();
for (i=0; i < dataLength; i++) {
var index = 0;
var seriesOptions = [];
var resource = data[i];
var objects = resource["objects"];
var objectsLength = objects.length;
var a_index = 0;
var aggregated = false;
var aggregates = []
for (j=0; j < objectsLength; j++) {
aggregate = [];
var object = objects[j];
var monitors = object["monitors"];
var monitorsLength = monitors.length;
for (k=0; k < monitorsLength; k++) {
var datasets = monitors[k]['datasets'];
if (resource['aggregated_history']) {
var datasetsLength = datasets.length;
for (l=0; l < datasetsLength; l++) {
seriesOptions.push(datasets[l]);
for (m=0; m < resource['dates'].length; m++) {
aggregate[m] = ((aggregate[m] || 0 ) + datasets[l]['data'][m]);
};
};
} else {
for (var object_name in datasets) {
seriesOptions[index] = {
name: object_name,
data: datasets[object_name]
};
index += 1;
};
};
};
if (k > 1)
aggregated = true;
aggregates[a_index] = {
name: object['object_name'],
data: aggregate
};
a_index += 1;
};
divs = (
'<div class="chart-box" style="background: '+(i % 2 ? "#EDF3FE" : "#FFFFFF")+';">' +
'<h1>'+resource['content_type'].capitalize() + ' ' + resource['verbose_name'].toLowerCase() + '</h1>' +
'<div id="resource-'+i+'" class="chart"></div>'
);
if (a_index > 1 && aggregated && resource['aggregated_history'])
divs += '<br><div class="chart" id="resource-'+i+'-aggregate"></div>';
divs += '</div>';
$("#charts").append(divs);
if (a_index > 1 && aggregated && resource['aggregated_history'])
charts['columns']('#resource-'+i+'-aggregate', i, aggregates, resource);
charts[(resource['aggregated_history'] ? 'columns': 'series')]('#resource-'+i, i, seriesOptions, resource);
};
});
};
plot_charts("{% url 'musician:dashboard-historydata' ids %}");
</script>
<style type="text/css">
@page {
size: 11.69in 8.27in;
}
h1 {
font-family: sans;
font-size: 21px;
}
#notice {
font-family: sans;
font-size: 12px;
text-align: right;
padding-right: 10px;
}
#message {
width:300px;
margin:0 auto;
font-family: monospace;
font-weight: bold;
font-size: 18px;
margin-top: 5%;
}
.chart-box {
margin: 10px;
margin-bottom: -1px;
border: 1px solid grey;
padding: 20px;
}
.chart {
height: 400px;
min-width: 310px;
}
</style>
</head>
<body>
<div id="notice">&#9830;Notice that resources used by deleted services will not appear.</div>
<div id="charts">
<div id="message">
> crunching data <span id="dancing-dots-text"> <span><span>.</span><span>.</span><span>.</span></span></span>
</div>
</div>
</body>
</html>

View File

@ -17,6 +17,8 @@ urlpatterns = [
path('auth/login/', views.LoginView.as_view(), name='login'),
path('auth/logout/', views.LogoutView.as_view(), name='logout'),
path('dashboard/', views.DashboardView.as_view(), name='dashboard'),
path('dashboard/historydata/<int:pk>/', views.HistoryDataView.as_view(), name='dashboard-historydata'),
path('dashboard/history/<int:pk>/', views.HistoryView.as_view(), name='dashboard-history'),
path('domains/', views.DomainListView.as_view(), name='domain-list'),
path('domains/<int:pk>/', views.DomainDetailView.as_view(), name='domain-detail'),

View File

@ -10,7 +10,7 @@ from django.db.models import Value
from django.db.models.functions import Concat
from django.http import (HttpResponse, HttpResponseNotFound,
HttpResponseRedirect)
from django.shortcuts import get_object_or_404
from django.shortcuts import get_object_or_404, render
from django.urls import reverse_lazy
from django.utils import translation
from django.utils.html import format_html
@ -59,6 +59,57 @@ from .lists.views import *
logger = logging.getLogger(__name__)
import json
from urllib.parse import parse_qs
from orchestra.contrib.resources.helpers import get_history_data
from django.http import HttpResponseNotFound, Http404
class HistoryView(CustomContextMixin, UserTokenRequiredMixin, View):
def check_resource(self, pk):
related_resources = self.get_all_resources()
account = related_resources.filter(resource_id__verbose_name='account-disk').first()
account_trafic = related_resources.filter(resource_id__verbose_name='account-traffic').first()
account = getattr(account, "id", False) == pk
account_trafic = getattr(account_trafic, "id", False) == pk
if account == False and account_trafic == False:
raise Http404(f"Resource with id {pk} does not exist")
def get(self, request, pk, *args, **kwargs):
context = {
'ids': pk
}
self.check_resource(pk)
return render(request, "musician/history.html", context)
# TODO: funcion de dashborad, mirar como no repetir esta funcion
def get_all_resources(self):
user = self.request.user
resources = Resource.objects.select_related('content_type')
resource_models = {r.content_type.model_class(): r.content_type_id for r in resources}
ct_id = resource_models[user._meta.model]
qset = Q(content_type_id=ct_id, object_id=user.id, resource__is_active=True)
for field, rel in user._meta.fields_map.items():
try:
ct_id = resource_models[rel.related_model]
except KeyError:
pass
else:
manager = getattr(user, field)
ids = manager.values_list('id', flat=True)
qset = Q(qset) | Q(content_type_id=ct_id, object_id__in=ids, resource__is_active=True)
return ResourceData.objects.filter(qset)
class HistoryDataView(CustomContextMixin, UserTokenRequiredMixin, View):
def get(self, request, pk, *args, **kwargs):
ids = [pk]
queryset = ResourceData.objects.filter(id__in=ids)
history = get_history_data(queryset)
response = json.dumps(history, indent=4)
return HttpResponse(response, content_type="application/json")
class DashboardView(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
@ -76,10 +127,6 @@ class DashboardView(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
account = related_resources.filter(resource_id__verbose_name='account-disk').first()
account_trafic = related_resources.filter(resource_id__verbose_name='account-traffic').first()
# TODO: sacar los graficos de alguna manera
# url_history_disk = reverse('admin:resources_resourcedata_show_history', args=(account.pk,))
# url_history_traffic = reverse('admin:resources_resourcedata_show_history', args=(account_trafic.pk,))
mailboxes = related_resources.filter(resource_id__verbose_name='mailbox-disk')
lists = related_resources.filter(resource_id__verbose_name='list-traffic')
databases = related_resources.filter(resource_id__verbose_name='database-disk')
@ -95,7 +142,6 @@ class DashboardView(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
# TODO(@slamora) update when backend provides resource usage data
resource_usage = {
# 'account': self.get_account_usage(profile_type, account),
'mailbox': self.get_resource_usage(profile_type, mailboxes, 'mailbox'),
'database': self.get_resource_usage(profile_type, databases, 'database'),
'nextcloud': self.get_resource_usage(profile_type, nextcloud, 'nextcloud'),
@ -168,15 +214,19 @@ class DashboardView(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
}
def get_account_usage(self, profile_type, account, account_trafic):
total_size = 0
if account != None and getattr(account, "used") != None:
total_size = account.used
allowed_size = ALLOWED_RESOURCES[profile_type]['account']
total_size = account.used
size_left = allowed_size - total_size
unit = account.unit if account != None else "GiB"
alert = ''
if size_left < 0:
alert = format_html(f"<span class='text-danger'>{size_left * -1} {account.unit} extra</span>")
alert = format_html(f"<span class='text-danger'>{size_left * -1} {unit} extra</span>")
elif size_left <= 1:
alert = format_html(f"<span class='text-warning'>{size_left} {account.unit} available</span>")
alert = format_html(f"<span class='text-warning'>{size_left} {unit} available</span>")
return {
'verbose_name': _('Account'),