Skip to content

Commit

Permalink
feat: wire up dashboard summary numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
ngurenyaga committed Jul 29, 2021
1 parent e847150 commit 275660f
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 19 deletions.
60 changes: 60 additions & 0 deletions fahari/common/dashboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from django.contrib.auth import get_user_model
from django.db.models import Sum
from django.utils import timezone

from fahari.ops.models import DailyUpdate

from .constants import WHITELIST_COUNTIES
from .models import Facility

User = get_user_model()


def get_fahari_facilities_queryset():
return Facility.objects.filter(
is_fahari_facility=True,
operation_status="Operational",
county__in=WHITELIST_COUNTIES,
active=True,
)


def get_active_facility_count(user):
return (
get_fahari_facilities_queryset()
.filter(
organisation=user.organisation,
)
.count()
)


def get_open_ticket_count(user):
from fahari.ops.models import FacilitySystemTicket

return FacilitySystemTicket.objects.filter(
resolved__isnull=True,
active=True,
organisation=user.organisation,
).count()


def get_active_user_count(user):
return User.objects.filter(
is_approved=True,
approval_notified=True,
organisation=user.organisation,
).count()


def get_appointments_mtd(user):
today = timezone.datetime.today()
year = today.year
month = today.month
qs = DailyUpdate.objects.filter(
active=True,
date__year=year,
date__month=month,
organisation=user.organisation,
).aggregate(Sum("total"))
return qs["total__sum"]
64 changes: 64 additions & 0 deletions fahari/common/tests/test_dashboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import random

import pytest
from django.contrib.auth import get_user_model
from django.utils import timezone
from model_bakery import baker

from fahari.common.constants import WHITELIST_COUNTIES
from fahari.common.dashboard import (
get_active_facility_count,
get_active_user_count,
get_appointments_mtd,
get_open_ticket_count,
)
from fahari.common.models import Facility
from fahari.ops.models import DailyUpdate, FacilitySystemTicket

User = get_user_model()

pytestmark = pytest.mark.django_db


def test_get_active_facility_count(user):
baker.make(
Facility,
is_fahari_facility=True,
operation_status="Operational",
county=random.choice(WHITELIST_COUNTIES),
active=True,
organisation=user.organisation,
)
assert get_active_facility_count(user) == 1


def test_get_open_ticket_count(user):
baker.make(
FacilitySystemTicket,
resolved=None,
active=True,
organisation=user.organisation,
)
assert get_open_ticket_count(user) == 1


def test_get_active_user_count(user):
baker.make(
User,
is_approved=True,
approval_notified=True,
organisation=user.organisation,
)
assert get_active_user_count(user) >= 1


def test_get_appointments_mtd(user):
today = timezone.datetime.today()
baker.make(
DailyUpdate,
active=True,
date=today,
total=999,
organisation=user.organisation,
)
assert get_appointments_mtd(user) == 999
28 changes: 14 additions & 14 deletions fahari/common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from .constants import WHITELIST_COUNTIES
from .dashboard import (
get_active_facility_count,
get_active_user_count,
get_appointments_mtd,
get_fahari_facilities_queryset,
get_open_ticket_count,
)
from .filters import FacilityFilter, FacilityUserFilter, SystemFilter
from .forms import FacilityForm, FacilityUserForm, SystemForm
from .models import Facility, FacilityUser, System
Expand All @@ -21,15 +27,6 @@
User = get_user_model()


def get_fahari_facilities_queryset():
return Facility.objects.filter(
is_fahari_facility=True,
operation_status="Operational",
county__in=WHITELIST_COUNTIES,
active=True,
)


class BaseFormMixin(ModelFormMixin, View):
def form_valid(self, form):
user = self.request.user
Expand Down Expand Up @@ -85,13 +82,16 @@ class HomeView(LoginRequiredMixin, ApprovedMixin, TemplateView):
permission_required = "users.can_view_dashboard"

def get_context_data(self, **kwargs):
# TODO open_ticket_count
# TODO active_facility_count
# TODO appointments_mtd
# TODO active_user_count
context = super().get_context_data(**kwargs)
context["active"] = "dashboard-nav" # id of active nav element
context["selected"] = "dashboard" # id of selected page

# dashboard summaries
user = self.request.user
context["active_facility_count"] = get_active_facility_count(user)
context["open_ticket_count"] = get_open_ticket_count(user)
context["user_count"] = get_active_user_count(user)
context["appointments_mtd"] = get_appointments_mtd(user)
return context


Expand Down
2 changes: 1 addition & 1 deletion fahari/ops/forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.forms.widgets import DateTimeInput, TextInput

from fahari.common.dashboard import get_fahari_facilities_queryset
from fahari.common.forms import BaseModelForm
from fahari.common.views import get_fahari_facilities_queryset

from .models import (
ActivityLog,
Expand Down
16 changes: 12 additions & 4 deletions fahari/templates/pages/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ <h1 class="h3 mb-0 text-gray-800">Welcome, {% firstof user.name user.username us
<div class="text-xs font-weight-bold text-primary text-uppercase mb-1">
Open Tickets
</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">40,000</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">
{{open_ticket_count}}
</div>
</div>
<div class="col-auto">
<i class="fas fa-clipboard-list fa-2x text-gray-300"></i>
Expand All @@ -34,7 +36,9 @@ <h1 class="h3 mb-0 text-gray-800">Welcome, {% firstof user.name user.username us
<div class="text-xs font-weight-bold text-success text-uppercase mb-1">
Active Facilities
</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">215,000</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">
{{active_facility_count}}
</div>
</div>
<div class="col-auto">
<i class="fas fa-hospital fa-2x text-gray-300"></i>
Expand All @@ -52,7 +56,9 @@ <h1 class="h3 mb-0 text-gray-800">Welcome, {% firstof user.name user.username us
<div class="text-xs font-weight-bold text-info text-uppercase mb-1">
Appointments, Month-To-Date
</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">20</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">
{{appointments_mtd}}
</div>
</div>
<div class="col-auto">
<i class="fas fa-calendar-check fa-2x text-gray-300"></i>
Expand All @@ -70,7 +76,9 @@ <h1 class="h3 mb-0 text-gray-800">Welcome, {% firstof user.name user.username us
<div class="text-xs font-weight-bold text-warning text-uppercase mb-1">
Active Users
</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">18</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">
{{user_count}}
</div>
</div>
<div class="col-auto">
<i class="fas fa-user fa-2x text-gray-300"></i>
Expand Down

0 comments on commit 275660f

Please sign in to comment.