Include mailboxes resource usage details.

This commit is contained in:
Santiago Lamora 2020-02-17 12:39:49 +01:00
parent 0fa26d799b
commit 4ea7ca06e8
4 changed files with 80 additions and 27 deletions

View file

@ -15,7 +15,7 @@
<div class="card resource-usage resource-{{ resource }}">
<div class="card-body">
<h5 class="card-title">{{ usage.verbose_name }}</h5>
{% include "musician/components/usage_progress_bar.html" with detail=usage %}
{% include "musician/components/usage_progress_bar.html" with detail=usage.data %}
</div>
</div>
{% endfor %}

View file

@ -1,6 +1,7 @@
from django.test import TestCase
from .models import UserAccount
from .utils import get_bootstraped_percent
class DomainsTestCase(TestCase):
@ -37,3 +38,32 @@ class UserAccountTest(TestCase):
}
account = UserAccount.new_from_json(data)
self.assertIsNone(account.last_login)
class GetBootstrapedPercentTest(TestCase):
BS_WIDTH = [0, 25, 50, 100]
def test_exact_value(self):
value = get_bootstraped_percent(25, 100)
self.assertIn(value, self.BS_WIDTH)
self.assertEqual(value, 25)
def test_round_to_lower(self):
value = get_bootstraped_percent(26, 100)
self.assertIn(value, self.BS_WIDTH)
self.assertEqual(value, 25)
def test_round_to_higher(self):
value = get_bootstraped_percent(48, 100)
self.assertIn(value, self.BS_WIDTH)
self.assertEqual(value, 50)
def test_max_boundary(self):
value = get_bootstraped_percent(200, 100)
self.assertIn(value, self.BS_WIDTH)
self.assertEqual(value, 100)
def test_min_boundary(self):
value = get_bootstraped_percent(-10, 100)
self.assertIn(value, self.BS_WIDTH)
self.assertEqual(value, 0)

15
musician/utils.py Normal file
View file

@ -0,0 +1,15 @@
def get_bootstraped_percent(value, total):
"""
Get percent and round to be 0, 25, 50 or 100
Useful to set progress bar width using CSS classes (e.g. w-25)
"""
percent = value / total
bootstraped = round(percent * 4) * 100 // 4
# handle min and max boundaries
bootstraped = max(0, bootstraped)
bootstraped = min(100, bootstraped)
return bootstraped

View file

@ -21,6 +21,7 @@ from .mixins import (CustomContextMixin, ExtendedPaginationMixin,
from .models import (Bill, DatabaseService, MailinglistService, MailService,
PaymentSource, SaasService, UserAccount)
from .settings import ALLOWED_RESOURCES
from .utils import get_bootstraped_percent
class DashboardView(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
@ -34,38 +35,14 @@ class DashboardView(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
context = super().get_context_data(**kwargs)
domains = self.orchestra.retrieve_domain_list()
# TODO(@slamora) update when backend provides resource usage data
resource_usage = {
'disk': {
'verbose_name': _('Disk usage'),
'usage': 534,
'total': 1024,
'unit': 'MB',
'percent': 50,
},
'traffic': {
'verbose_name': _('Traffic'),
'usage': 300,
'total': 2048,
'unit': 'MB/month',
'percent': 25,
},
'mailbox': {
'verbose_name': _('Mailbox usage'),
'usage': 1,
'total': 2,
'unit': 'accounts',
'percent': 50,
},
}
# TODO(@slamora) update when backend supports notifications
notifications = []
# show resource usage based on plan definition
# TODO(@slamora): validate concept of limits with Pangea
profile_type = context['profile'].type
total_mailboxes = 0
for domain in domains:
total_mailboxes += len(domain.mails)
addresses_left = ALLOWED_RESOURCES[profile_type]['mailbox'] - len(domain.mails)
alert_level = None
if addresses_left == 1:
@ -78,6 +55,37 @@ class DashboardView(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
'alert_level': alert_level,
}
# TODO(@slamora) update when backend provides resource usage data
resource_usage = {
'disk': {
'verbose_name': _('Disk usage'),
'data': {
# 'usage': 534,
# 'total': 1024,
# 'unit': 'MB',
# 'percent': 50,
},
},
'traffic': {
'verbose_name': _('Traffic'),
'data': {
# 'usage': 300,
# 'total': 2048,
# 'unit': 'MB/month',
# 'percent': 25,
},
},
'mailbox': {
'verbose_name': _('Mailbox usage'),
'data': {
'usage': total_mailboxes,
'total': ALLOWED_RESOURCES[profile_type]['mailbox'],
'unit': 'accounts',
'percent': get_bootstraped_percent(total_mailboxes, ALLOWED_RESOURCES[profile_type]['mailbox']),
},
},
}
context.update({
'domains': domains,
'resource_usage': resource_usage,