From 6b2555c1f01e9e5d0ccfff8f13a12196f70fc239 Mon Sep 17 00:00:00 2001 From: rebuilt Date: Fri, 12 May 2023 12:27:00 -0700 Subject: [PATCH] Fix response rates for small schools by accounting for expected minimum counts of responses less than 10 --- app/models/student_response_rate_calculator.rb | 5 ++++- spec/models/response_rate_calculator_spec.rb | 5 +++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/models/student_response_rate_calculator.rb b/app/models/student_response_rate_calculator.rb index 342327fb..cc01dda7 100644 --- a/app/models/student_response_rate_calculator.rb +++ b/app/models/student_response_rate_calculator.rb @@ -28,11 +28,14 @@ class StudentResponseRateCalculator < ResponseRateCalculator end def survey_items_with_sufficient_responses(grade:) + threshold = 10 + quarter_of_grade = enrollment_by_grade[grade] / 4 + threshold = threshold > quarter_of_grade ? quarter_of_grade : threshold 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, "survey_item_responses.grade": grade, "survey_item_responses.survey_item_id": subcategory.survey_items.student_survey_items) .group('survey_items.id') - .having('count(*) >= 10') + .having("count(*) >= #{threshold}") .count end diff --git a/spec/models/response_rate_calculator_spec.rb b/spec/models/response_rate_calculator_spec.rb index 3375e34c..08ba51d1 100644 --- a/spec/models/response_rate_calculator_spec.rb +++ b/spec/models/response_rate_calculator_spec.rb @@ -65,8 +65,9 @@ describe ResponseRateCalculator, type: :model do context 'there are responses for another survey item but not enough to meet the minimum threshold' do before do - create_list(:survey_item_response, 9, survey_item: insufficient_student_survey_item_1, academic_year:, - school:, grade: 1) + less_than_a_quarter_of_respondents_for_first_grade = 4 + create_list(:survey_item_response, less_than_a_quarter_of_respondents_for_first_grade, survey_item: insufficient_student_survey_item_1, academic_year:, + school:, grade: 1) end it 'returns an average of the response rates for all grades' do expect(StudentResponseRateCalculator.new(subcategory:, school:, academic_year:).rate).to eq 50