perf: reduce time spent in sql query for student response rate

rpp-main
Nelson Jovel 2 years ago
parent 6bea0bc2d0
commit 946b0833fd

@ -42,12 +42,13 @@ class StudentResponseRateCalculator < ResponseRateCalculator
threshold = 10 threshold = 10
quarter_of_grade = enrollment_by_grade[grade] / 4 quarter_of_grade = enrollment_by_grade[grade] / 4
threshold = threshold > quarter_of_grade ? quarter_of_grade : threshold threshold = threshold > quarter_of_grade ? quarter_of_grade : threshold
memo[grade] = SurveyItem.joins("inner join survey_item_responses on survey_item_responses.survey_item_id = survey_items.id")
.student_survey_items si = SurveyItemResponse.student_survey_items_with_sufficient_responses_by_grade(school:, academic_year:,
.where("survey_item_responses.school": school, "survey_item_responses.academic_year": academic_year, "survey_item_responses.grade": grade, "survey_item_responses.survey_item_id": subcategory.survey_items.student_survey_items) threshold:)
.group("survey_items.id") ssi = @subcategory.survey_items.student_survey_items.map(&:id)
.having("count(*) >= #{threshold}") grade_array = Array.new(ssi.length, grade)
.count
memo[grade] = si.slice(*grade_array.zip(ssi))
end end
@survey_items_with_sufficient_responses[grade] @survey_items_with_sufficient_responses[grade]
end end

@ -69,4 +69,18 @@ class SurveyItemResponse < ActiveRecord::Base
end end
@teacher_survey_items_with_sufficient_responses[[school, academic_year]] @teacher_survey_items_with_sufficient_responses[[school, academic_year]]
end end
def self.student_survey_items_with_sufficient_responses_by_grade(school:, academic_year:, threshold:)
@student_survey_items_with_sufficient_responses_by_grade ||= Hash.new do |memo, (school, academic_year)|
hash = SurveyItem.joins("inner join survey_item_responses on survey_item_responses.survey_item_id = survey_items.id")
.student_survey_items
.where("survey_item_responses.school": school, "survey_item_responses.academic_year": academic_year)
.group(:grade, :id)
.having("count(*) >= #{threshold}")
.count
memo[[school, academic_year]] = hash
end
@student_survey_items_with_sufficient_responses_by_grade[[school, academic_year]]
end
end end

Loading…
Cancel
Save