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

View File

@ -276,11 +276,32 @@ h1.service-name {
font-weight: 900; 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 { .card.resource-usage.resource-notifications h5.card-title:after {
content: "\f0f3"; content: "\f0f3";
font-weight: 900; 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 { .card.card-profile .card-header {
background: white; background: white;
border-bottom: none; border-bottom: none;

View File

@ -11,30 +11,60 @@
{% endif %} {% endif %}
<div class="card-deck"> <div class="card-deck">
{% for resource, usage in resource_usage.items %} {% for resource, usage in resource_usage.items %}
<div class="card resource-usage resource-{{ resource }}"> <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> <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 %} {% if usage.data.alert %}
<div class="text-center mt-4"> <div class="text-center mt-4">
{{ usage.data.alert }} {{ usage.data.alert }}
</div> </div>
{% endif %} {% endif %}
</div> </div>
</div>
{% endfor %} <div id="collapse-{{ resource }}" class="collapse">
<div class="card resource-usage resource-notifications"> <ul class="list-group">
<div class="card-body"> {% for obj_data in usage.objects %}
<h5 class="card-title">{% trans "Notifications" %}</h5> <li class="list-group-item d-flex justify-content-between align-items-center">
{% for message in notifications %} {{ obj_data }}
<p class="card-text">{{ message }}</p> <span class="badge badge-primary badge-pill">{{ obj_data.used }} {{ obj_data.resource.unit }}</span>
{% empty %} </li>
<p class="card-text">{% trans "There is no notifications at this time." %}</p> {% endfor %}
{% endfor %} </ul>
</div> </div>
</div> </div>
{% 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> </div>
{% endblock %} {% endblock %}

View File

@ -69,9 +69,24 @@ class DashboardView2(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
related_resources = self.get_all_resources() 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 # TODO(@slamora) update when backend supports notifications
notifications = [] notifications = []
@ -81,7 +96,11 @@ class DashboardView2(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
# TODO(@slamora) update when backend provides resource usage data # TODO(@slamora) update when backend provides resource usage data
resource_usage = { 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") support_email = getattr(settings, "USER_SUPPORT_EMAIL", "suport@pangea.org")
@ -91,7 +110,7 @@ class DashboardView2(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
support_email, support_email,
) )
context.update({ context.update({
# 'domains': domains, 'domains': domains,
'resource_usage': resource_usage, 'resource_usage': resource_usage,
'notifications': notifications, 'notifications': notifications,
"support_email_anchor": support_email_anchor, "support_email_anchor": support_email_anchor,
@ -99,7 +118,6 @@ class DashboardView2(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
return context return context
def get_all_resources(self): def get_all_resources(self):
user = self.request.user user = self.request.user
resources = Resource.objects.select_related('content_type') 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) qset = Q(qset) | Q(content_type_id=ct_id, object_id__in=ids, resource__is_active=True)
return ResourceData.objects.filter(qset) return ResourceData.objects.filter(qset)
def get_resource_usage(self, profile_type, resource_data, name_resource):
def get_mailbox_usage(self, profile_type): limit_rs = 0
allowed_mailboxes = ALLOWED_RESOURCES[profile_type]['mailbox'] total_rs = len(resource_data)
total_mailboxes = len(self.orchestra.retrieve_mailbox_list()) rs_left = 0
mailboxes_left = allowed_mailboxes - total_mailboxes
alert = '' alert = ''
if mailboxes_left < 0: progres_bar = False
alert = format_html("<span class='text-danger'>{} extra mailboxes</span>", mailboxes_left * -1)
elif mailboxes_left <= 1: if ALLOWED_RESOURCES[profile_type].get(name_resource):
alert = format_html("<span class='text-warning'>{} mailbox left</span>", mailboxes_left) progres_bar = True
limit_rs = ALLOWED_RESOURCES[profile_type][name_resource]
rs_left = limit_rs - total_rs
alert = ''
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 { return {
'verbose_name': _('Mailboxes'), 'verbose_name': _(name_resource.capitalize()),
'data': { 'data': {
'used': total_mailboxes, 'progres_bar': progres_bar,
'total': allowed_mailboxes, 'used': total_rs,
'total': limit_rs,
'alert': alert, 'alert': alert,
'unit': 'mailboxes', 'unit': name_resource.capitalize(),
'percent': get_bootstraped_percent(total_mailboxes, allowed_mailboxes), '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,
} }