Fakultas Ilmu Komputer UI

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

[REFACTOR] Tidy up comments and improve namings

parent 2c890830
Pipeline #47434 passed with stages
in 3 minutes and 5 seconds
# District
DISTRICTS = ( DISTRICTS = (
"Beji", "Beji",
"Bojongsari", "Bojongsari",
...@@ -12,11 +14,23 @@ DISTRICTS = ( ...@@ -12,11 +14,23 @@ DISTRICTS = (
"Tapos", "Tapos",
) )
DISTRICT = "district" DISTRICT = "district"
# Age
AGE = "age" AGE = "age"
# Sex
SEX = "sex" SEX = "sex"
FEMALE = "female" FEMALE = "female"
MALE = "male" MALE = "male"
# Outcome
POSITIVE = "positive" POSITIVE = "positive"
NEGATIVE = "negative" NEGATIVE = "negative"
UNDERTERMINED = "undetermined" UNDERTERMINED = "undetermined"
# Total
TOTAL = "total_count" TOTAL = "total_count"
from django.urls import path from django.urls import path
from apps.exportables.views import StatisticsView from apps.exportables.views import CaseCountsView
urlpatterns = [ urlpatterns = [
path("", StatisticsView.as_view()), path("", CaseCountsView.as_view()),
] ]
...@@ -19,7 +19,7 @@ def generate_initial_counts(): ...@@ -19,7 +19,7 @@ def generate_initial_counts():
} }
def generate_initial_groups(): def generate_initial_group_counts():
age_groups = {} age_groups = {}
sex_groups = { sex_groups = {
MALE: generate_initial_counts(), MALE: generate_initial_counts(),
...@@ -32,7 +32,7 @@ def generate_initial_groups(): ...@@ -32,7 +32,7 @@ def generate_initial_groups():
return [age_groups, sex_groups, district_groups] return [age_groups, sex_groups, district_groups]
def map_outcome(outcome): def map_outcome_value(outcome):
if outcome is None: if outcome is None:
return UNDERTERMINED return UNDERTERMINED
if outcome: if outcome:
...@@ -41,7 +41,7 @@ def map_outcome(outcome): ...@@ -41,7 +41,7 @@ def map_outcome(outcome):
return NEGATIVE return NEGATIVE
def map_sex(sex): def map_sex_value(sex):
if sex: if sex:
return MALE return MALE
return FEMALE return FEMALE
...@@ -21,23 +21,22 @@ from apps.exportables.constants import ( ...@@ -21,23 +21,22 @@ from apps.exportables.constants import (
) )
from apps.exportables.utils import ( from apps.exportables.utils import (
generate_initial_counts, generate_initial_counts,
generate_initial_groups, generate_initial_group_counts,
map_outcome, map_outcome_value,
map_sex, map_sex_value,
) )
class StatisticsView(APIView): class CaseCountsView(APIView):
def get(self, request, format=None): def get(self, request, format=None):
case_subjects = CaseSubject.objects.all() case_subjects = CaseSubject.objects.all()
investigation_cases = ( investigation_cases = (InvestigationCase.objects
InvestigationCase.objects .values("case_subject")
.values("case_subject") .annotate(latest_investigation_case=Max("created_at"))
.annotate(latest_investigation_case=Max("created_at")) .order_by()
.order_by() .values("case_subject__id", "is_positive")
.values("case_subject__id", "is_positive")
) )
outcomes = {} outcomes = {}
...@@ -46,13 +45,13 @@ class StatisticsView(APIView): ...@@ -46,13 +45,13 @@ class StatisticsView(APIView):
related_case_subject_id = investigation_case["case_subject__id"] related_case_subject_id = investigation_case["case_subject__id"]
outcomes[related_case_subject_id] = investigation_case["is_positive"] outcomes[related_case_subject_id] = investigation_case["is_positive"]
age_groups, sex_groups, district_groups = generate_initial_groups() age_groups, sex_groups, district_groups = generate_initial_group_counts()
for case_subject in case_subjects: for case_subject in case_subjects:
outcome = outcomes.get(case_subject.id) outcome = outcomes.get(case_subject.id)
outcome = map_outcome(outcome) outcome = map_outcome_value(outcome)
# Age Group # Age-based grouping
subject_age = str(case_subject.age) subject_age = str(case_subject.age)
if age_groups.get(subject_age) is None: if age_groups.get(subject_age) is None:
age_groups[subject_age] = generate_initial_counts() age_groups[subject_age] = generate_initial_counts()
...@@ -60,7 +59,7 @@ class StatisticsView(APIView): ...@@ -60,7 +59,7 @@ class StatisticsView(APIView):
age_groups[subject_age][outcome] += 1 age_groups[subject_age][outcome] += 1
age_groups[subject_age][TOTAL] += 1 age_groups[subject_age][TOTAL] += 1
# District Group # District-based grouping
subject_district = case_subject.district subject_district = case_subject.district
if district_groups.get(subject_district) is None: if district_groups.get(subject_district) is None:
district_groups[subject_district] = generate_initial_counts() district_groups[subject_district] = generate_initial_counts()
...@@ -68,9 +67,9 @@ class StatisticsView(APIView): ...@@ -68,9 +67,9 @@ class StatisticsView(APIView):
district_groups[subject_district][outcome] += 1 district_groups[subject_district][outcome] += 1
district_groups[subject_district][TOTAL] += 1 district_groups[subject_district][TOTAL] += 1
# Sex Group # Sex-based grouping
subject_sex = case_subject.is_male subject_sex = case_subject.is_male
subject_sex = map_sex(subject_sex) subject_sex = map_sex_value(subject_sex)
sex_groups[subject_sex][outcome] += 1 sex_groups[subject_sex][outcome] += 1
sex_groups[subject_sex][TOTAL] += 1 sex_groups[subject_sex][TOTAL] += 1
......
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