musician dashboard2 show resources

This commit is contained in:
Jorge Pastor 2024-06-13 20:42:49 +02:00
parent cb3e482dfc
commit b72af48cd1
4 changed files with 141 additions and 35 deletions

View File

@ -9,7 +9,7 @@ def getsetting(name):
# provide a default value allowing to overwrite it for each type of account
def allowed_resources_default_factory():
return {'mailbox': 2}
return {'mailbox': 2, 'database': 1, 'account': 2, 'nextcloud': 2,}
DEFAULTS = {
# allowed resources limit hardcoded because cannot be retrieved from the API.
@ -21,11 +21,17 @@ DEFAULTS = {
# 'disk': 1024,
# 'traffic': 2048,
'mailbox': 2,
'database': 1,
'account': 2,
'nextcloud': 2,
},
'ASSOCIATION': {
# 'disk': 5 * 1024,
# 'traffic': 20 * 1024,
'mailbox': 10,
'database': 1,
'account': 8,
'nextcloud': 10,
}
}
),

View File

@ -276,11 +276,32 @@ h1.service-name {
font-weight: 900;
}
.card.resource-usage.resource-database h5.card-title:after {
content: "\f1c0";
font-weight: 900;
}
.card.resource-usage.resource-notifications h5.card-title:after {
content: "\f0f3";
font-weight: 900;
}
.card.resource-usage.resource-domains h5.card-title:after {
content: "\f7a2";
font-weight: 900;
}
.card.resource-usage.resource-nextcloud h5.card-title:after {
content: "\f0c2";
font-weight: 900;
}
.card.resource-usage.resource-list h5.card-title:after {
content: "\f674";
font-weight: 900;
}
.card.card-profile .card-header {
background: white;
border-bottom: none;

View File

@ -11,30 +11,60 @@
{% endif %}
<div class="card-deck">
{% for resource, usage in resource_usage.items %}
<div class="card resource-usage resource-{{ resource }}">
<div class="card-body">
<div class="card-body" data-toggle="collapse" data-target="#collapse-{{ resource }}">
<h5 class="card-title">{{ usage.verbose_name }}</h5>
{% include "musician/components/usage_progress_bar.html" with detail=usage.data %}
<div class="text-center">
{% if usage.data and usage.data.used %}
{{ usage.data.used|floatformat }} {{ usage.data.unit }}
{% endif %}
</div>
{% if usage.data.progres_bar %}
<div class="progress">
<div class="progress-bar bg-secondary w-{{ usage.data.percent }}" role="progressbar" aria-valuenow="{{ usage.data.used }}"
aria-valuemin="0" aria-valuemax="{{ usage.data.total }}"></div>
</div>
{% endif %}
{% if usage.data.alert %}
<div class="text-center mt-4">
{{ usage.data.alert }}
</div>
{% endif %}
</div>
<div id="collapse-{{ resource }}" class="collapse">
<ul class="list-group">
{% for obj_data in usage.objects %}
<li class="list-group-item d-flex justify-content-between align-items-center">
{{ obj_data }}
<span class="badge badge-primary badge-pill">{{ obj_data.used }} {{ obj_data.resource.unit }}</span>
</li>
{% endfor %}
</ul>
</div>
</div>
{% endfor %}
<div class="card resource-usage resource-notifications">
<div class="card-body">
<h5 class="card-title">{% trans "Notifications" %}</h5>
{% for message in notifications %}
<p class="card-text">{{ message }}</p>
{% empty %}
<p class="card-text">{% trans "There is no notifications at this time." %}</p>
{% endfor %}
<div class="card resource-usage resource-domains">
<div class="card-body" data-toggle="collapse" data-target="#collapse-domains">
<h5 class="card-title">{% trans "Domains" %}</h5>
<div class="text-center">{{ domains|length }} {% trans "Domains" %}</div>
</div>
<div id="collapse-domains" class="collapse">
<ul class="list-group">
{% for domain in domains %}
<li class="list-group-item d-flex justify-content-between align-items-center">
{{ domain }}
<a href="{% url 'musician:dashboard'%}" rel="noopener noreferrer"><i class="fas fa-external-link-alt"></i></a>
</li>
{% endfor %}
</ul>
</div>
</div>
</div>
{% endblock %}

View File

@ -69,8 +69,23 @@ class DashboardView2(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
context = super().get_context_data(**kwargs)
related_resources = self.get_all_resources()
# print([ x.resource for x in related_resources.filter(resource_id__verbose_name='mailbox-disk')])
# TODO: que mostrar en el panel
# account
account = related_resources.filter(resource_id__verbose_name='account-disk')
# account_trafic = related_resources.filter(resource_id__verbose_name='account-traffic')
# print(account_trafic.first())
# /admin/resources/resourcedata/17000
# mailbox
mailboxes = related_resources.filter(resource_id__verbose_name='mailbox-disk')
# lists
lists = related_resources.filter(resource_id__verbose_name='list-traffic')
# Database
databases = related_resources.filter(resource_id__verbose_name='database-disk')
# nextcloud
nextcloud = related_resources.filter(resource_id__verbose_name='nextcloud-disk')
# domains
domains = Domain.objects.filter(account_id=self.request.user)
# TODO(@slamora) update when backend supports notifications
@ -81,7 +96,11 @@ class DashboardView2(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
# TODO(@slamora) update when backend provides resource usage data
resource_usage = {
'mailbox': self.get_mailbox_usage(profile_type),
'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'),
'list': self.get_resource_usage(profile_type, lists, 'Mailman list Traffic'),
}
support_email = getattr(settings, "USER_SUPPORT_EMAIL", "suport@pangea.org")
@ -91,7 +110,7 @@ class DashboardView2(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
support_email,
)
context.update({
# 'domains': domains,
'domains': domains,
'resource_usage': resource_usage,
'notifications': notifications,
"support_email_anchor": support_email_anchor,
@ -99,7 +118,6 @@ class DashboardView2(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
return context
def get_all_resources(self):
user = self.request.user
resources = Resource.objects.select_related('content_type')
@ -117,27 +135,58 @@ class DashboardView2(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
qset = Q(qset) | Q(content_type_id=ct_id, object_id__in=ids, resource__is_active=True)
return ResourceData.objects.filter(qset)
def get_resource_usage(self, profile_type, resource_data, name_resource):
limit_rs = 0
total_rs = len(resource_data)
rs_left = 0
alert = ''
progres_bar = False
def get_mailbox_usage(self, profile_type):
allowed_mailboxes = ALLOWED_RESOURCES[profile_type]['mailbox']
total_mailboxes = len(self.orchestra.retrieve_mailbox_list())
mailboxes_left = allowed_mailboxes - total_mailboxes
if ALLOWED_RESOURCES[profile_type].get(name_resource):
progres_bar = True
limit_rs = ALLOWED_RESOURCES[profile_type][name_resource]
rs_left = limit_rs - total_rs
alert = ''
if mailboxes_left < 0:
alert = format_html("<span class='text-danger'>{} extra mailboxes</span>", mailboxes_left * -1)
elif mailboxes_left <= 1:
alert = format_html("<span class='text-warning'>{} mailbox left</span>", mailboxes_left)
if rs_left < 0:
alert = format_html(f"<span class='text-danger'>{rs_left * -1} extra {name_resource}</span>")
elif rs_left <= 1:
alert = format_html(f"<span class='text-warning'>{rs_left} {name_resource} left</span>")
return {
'verbose_name': _('Mailboxes'),
'verbose_name': _(name_resource.capitalize()),
'data': {
'used': total_mailboxes,
'total': allowed_mailboxes,
'progres_bar': progres_bar,
'used': total_rs,
'total': limit_rs,
'alert': alert,
'unit': 'mailboxes',
'percent': get_bootstraped_percent(total_mailboxes, allowed_mailboxes),
'unit': name_resource.capitalize(),
'percent': get_bootstraped_percent(total_rs, limit_rs),
},
'objects': resource_data,
}
def get_account_usage(self, profile_type, account):
allowed_size = ALLOWED_RESOURCES[profile_type]['account']
total_size = account.first().used
size_left = allowed_size - total_size
alert = ''
if size_left < 0:
alert = format_html(f"<span class='text-danger'>{size_left * -1} extra size</span>")
elif size_left <= 1:
alert = format_html(f"<span class='text-warning'>{size_left} size left</span>")
return {
'verbose_name': _('Account'),
'data': {
'progres_bar': True,
'used': total_size,
'total': allowed_size,
'alert': alert,
'unit': 'GiB Size',
'percent': get_bootstraped_percent(total_size, allowed_size),
},
'objects': account,
}