Fakultas Ilmu Komputer UI

Commit a2826f71 authored by Jonathan Christopher Jakub's avatar Jonathan Christopher Jakub
Browse files

[GREEN] Implement case count views

parent 4eafe7c7
Pipeline #47432 failed with stages
in 1 minute and 44 seconds
from django.urls import path
from apps.exportables.views import StatisticsView
urlpatterns = [
path("", StatisticsView.as_view()),
]
from django.http import JsonResponse
from rest_framework import status
from rest_framework.views import APIView
from apps.cases.models import (
CaseSubject,
InvestigationCase,
)
from apps.exportables.constants import (
AGE,
DISTRICT,
DISTRICTS,
FEMALE,
MALE,
NEGATIVE,
POSITIVE,
SEX,
TOTAL,
UNDERTERMINED
)
from apps.exportables.utils import (
generate_initial_counts,
generate_initial_groups,
map_outcome,
map_sex,
)
class StatisticsView(APIView):
def get(self, request, format=None):
case_subjects = CaseSubject.objects.all()
investigation_case_subjects = InvestigationCase.objects \
.order_by("case_subject__id", "-created_at") \
.distinct("case_subject")
outcomes = {}
for investigation_case_subject in investigation_case_subjects:
related_case_subject_id = investigation_case_subject.case_subject.id
outcomes[related_case_subject_id] = investigation_case_subject.is_positive
age_groups, sex_groups, district_groups = generate_initial_groups()
for case_subject in case_subjects:
outcome = outcomes.get(case_subject.id)
outcome = map_outcome(outcome)
# Age Group
subject_age = str(case_subject.age)
if age_groups.get(subject_age) is None:
age_groups[subject_age] = generate_initial_counts()
age_groups[subject_age][outcome] += 1
age_groups[subject_age][TOTAL] += 1
# District Group
subject_district = case_subject.district
if district_groups.get(subject_district) is None:
district_groups[subject_district] = generate_initial_counts()
district_groups[subject_district][outcome] += 1
district_groups[subject_district][TOTAL] += 1
# Sex Group
subject_sex = case_subject.is_male
subject_sex = map_sex(subject_sex)
sex_groups[subject_sex][outcome] += 1
sex_groups[subject_sex][TOTAL] += 1
data = {
AGE: age_groups,
DISTRICT: district_groups,
SEX: sex_groups,
TOTAL: len(case_subjects),
}
return JsonResponse(data=data, status=status.HTTP_200_OK)
......@@ -52,6 +52,7 @@ INSTALLED_APPS = [
# Apps
"apps.accounts",
"apps.cases",
"apps.exportables",
"apps.logs",
]
......
......@@ -10,4 +10,5 @@ urlpatterns = [
path("logs/", include("apps.logs.urls")),
path("auth/token/", obtain_auth_token, name="api_token_auth"),
path("auth/", include("django.contrib.auth.urls")),
path("exportables/", include("apps.exportables.urls")),
]
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment