Skip to content

Commit

Permalink
Merge pull request #225 from matematikk-mooc/aj/DIT-113
Browse files Browse the repository at this point in the history
 DIT-113: Remove enrollment percentage categories with direct members and teachers count
  • Loading branch information
manilpit authored Jun 11, 2024
2 parents 0abdd3a + 73c76c8 commit 804ef14
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 215 deletions.
46 changes: 18 additions & 28 deletions statistics_api/course_info/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Tuple
from statistics_api.course_info.models import Group, School
from django.db.models import Sum, F, Value, IntegerField, QuerySet
from django.db.models import Sum, F, Value, IntegerField


class Utils:
Expand Down Expand Up @@ -41,46 +41,36 @@ def _get_registered_school_counts(school_org_numbers: set, course_observation_id
)
)

def _combine_counts(unregistered_counts: list, registered_counts: list, schools: QuerySet) -> list:
def _combine_course_member_counts(unregistered_counts: list, registered_counts: list) -> list:

teacher_counts = schools.values('organization_number').annotate(
teacher_count=F('number_of_teachers')
)

org_nrs_enrollment_and_teacher_counts = [
(
unregistered_count['organization_number'],
unregistered_count['enrollment_count'],
unregistered_count['teacher_count']
)
for unregistered_count in unregistered_counts
]
schools = []
for unregistered_count in unregistered_counts:
schools.append({
"organization_number": unregistered_count["organization_number"],
"school_teacher_count": unregistered_count["teacher_count"],
"course_member_count": 0
})

for registered_count in registered_counts:
for schoolItem in schools:
if schoolItem["organization_number"] == registered_count["organization_number"]:
schoolItem["course_member_count"] = registered_count["enrollment_count"]
break

org_nrs_enrollment_and_teacher_counts += [
(
registered_count['organization_number'],
registered_count['enrollment_count'],
next((teacher_count['teacher_count'] for teacher_count in teacher_counts if teacher_count['organization_number'] == registered_count['organization_number']), 0)
)
for registered_count in registered_counts
]

return org_nrs_enrollment_and_teacher_counts
return schools

@staticmethod
def get_org_nrs_enrollment_and_teacher_counts(
def get_course_member_counts(
county_schools_org_nrs: Tuple[str],
course_observation_id: int,
teacher_count_year: int
) -> Tuple[Tuple[str, int, int]]:
) -> Tuple[Tuple[str, int, int, int]]:

school_org_numbers = Utils._get_school_org_numbers(county_schools_org_nrs, teacher_count_year)
unregistered_counts = Utils._get_unregistered_school_counts(county_schools_org_nrs, teacher_count_year)
registered_counts = Utils._get_registered_school_counts(school_org_numbers, course_observation_id)
schools = School.objects.filter(organization_number__in=county_schools_org_nrs, year=teacher_count_year)

return Utils._combine_counts(unregistered_counts, registered_counts, schools)
return Utils._combine_course_member_counts(unregistered_counts, registered_counts)

@staticmethod
def get_org_nrs_and_enrollment_counts(
Expand Down
44 changes: 16 additions & 28 deletions statistics_api/course_info/views/view_high_school.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
from statistics_api.course_info.utils import Utils

from statistics_api.clients.kpas_client import KpasClient
from statistics_api.definitions import CATEGORY_CODE_INFORMATION_DICT
from statistics_api.course_info.models import County
from statistics_api.course_info.models import CourseObservation
from statistics_api.utils.url_parameter_parser import get_url_parameters_dict, START_DATE_KEY, END_DATE_KEY \
, NR_OF_DATES_LIMIT_KEY
from statistics_api.utils.utils import filter_high_schools, calculate_enrollment_percentage_category, \
from statistics_api.utils.url_parameter_parser import get_url_parameters_dict, \
START_DATE_KEY, END_DATE_KEY, NR_OF_DATES_LIMIT_KEY
from statistics_api.utils.utils import filter_high_schools, \
get_closest_matching_year, get_target_year


def county_high_school_statistics_by_county_id(request, county_id, canvas_course_id) -> HttpResponse:

url_parameters_dict = get_url_parameters_dict(request)
Expand Down Expand Up @@ -41,45 +39,35 @@ def county_high_school_statistics_by_county_id(request, county_id, canvas_course
json_response = []

db_counties = County.objects.filter(county_id__exact=int(county_id))

year_to_db_county_mapping = {}

for db_county in db_counties:
year_to_db_county_mapping[db_county.year] = db_county

teacher_count_available_years = tuple([key for key in year_to_db_county_mapping.keys()])

for course_observation in course_observations:

target_year = get_target_year(course_observation.date_retrieved)
course_observation: CourseObservation
total_course_member_count = 0
total_school_teacher_count = 0

try:
target_year = get_target_year(course_observation.date_retrieved)
closest_matching_year = get_closest_matching_year(teacher_count_available_years, target_year)
county_teacher_count = year_to_db_county_mapping[closest_matching_year].number_of_teachers
total_school_teacher_count = year_to_db_county_mapping[closest_matching_year].number_of_teachers
except ValueError:
return HttpResponseServerError()

course_observation: CourseObservation
course_member_counts = Utils.get_org_nrs_and_enrollment_counts(school_org_nrs, course_observation.pk)
for course_member_count_item in course_member_counts:
total_course_member_count += course_member_count_item["enrollment_count"]

county_enrollment_count = 0

org_nrs_and_enrollment_counts = Utils.get_org_nrs_and_enrollment_counts(
school_org_nrs, course_observation.pk)

for org_nr, enrollment_count in org_nrs_and_enrollment_counts:
county_enrollment_count += enrollment_count # Looping through the schools, calculating total enrollment in entire county

county_enrollment_percentage_category = calculate_enrollment_percentage_category(county_enrollment_count,
county_teacher_count)

course_observation_json = {
json_response.append({
"date": course_observation.date_retrieved,
"county_name": county_name,
"county_id": county_id,
"county_organization_number": county_organization_number,
"enrollment_percentage_category": county_enrollment_percentage_category,
}

json_response.append(course_observation_json)
"total_course_member_count": total_course_member_count,
"total_school_teacher_count": total_school_teacher_count
})

return JsonResponse({**CATEGORY_CODE_INFORMATION_DICT, **{"Result": json_response}})
return JsonResponse({"Result": json_response})
Loading

0 comments on commit 804ef14

Please sign in to comment.