diff --git a/musician/templates/musician/dashboard.html b/musician/templates/musician/dashboard.html index 0b2b09b..addca3d 100644 --- a/musician/templates/musician/dashboard.html +++ b/musician/templates/musician/dashboard.html @@ -15,7 +15,7 @@
{{ usage.verbose_name }}
- {% include "musician/components/usage_progress_bar.html" with detail=usage %} + {% include "musician/components/usage_progress_bar.html" with detail=usage.data %}
{% endfor %} diff --git a/musician/tests.py b/musician/tests.py index 805ff05..13c365e 100644 --- a/musician/tests.py +++ b/musician/tests.py @@ -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) diff --git a/musician/utils.py b/musician/utils.py new file mode 100644 index 0000000..7b029c1 --- /dev/null +++ b/musician/utils.py @@ -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 diff --git a/musician/views.py b/musician/views.py index 85a6d4e..b70fbf5 100644 --- a/musician/views.py +++ b/musician/views.py @@ -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,