mirror of
https://github.com/edcommonwealth/sqm-dashboards.git
synced 2026-03-08 23:18:18 -07:00
perf: Reduce number of queries. Remove unneeded methods. Combine
subcategory and measure queries in controller
This commit is contained in:
parent
0662d7233f
commit
fae530d21f
5 changed files with 15 additions and 26 deletions
|
|
@ -25,10 +25,10 @@ class OverviewController < SqmApplicationController
|
|||
end
|
||||
|
||||
def measures
|
||||
@measures ||= Measure.all.includes(%i[scales admin_data_items category subcategory survey_items])
|
||||
@measures ||= subcategories.flat_map(&:measures)
|
||||
end
|
||||
|
||||
def subcategories
|
||||
@subcategories ||= Subcategory.all.includes(:measures)
|
||||
@subcategories ||= Subcategory.all.includes(%i[measures admin_data_items category])
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -192,34 +192,16 @@ class Measure < ActiveRecord::Base
|
|||
|
||||
def sufficient_student_data?(school:, academic_year:)
|
||||
return false unless includes_student_survey_items?
|
||||
return false if no_student_responses_exist?(school:, academic_year:)
|
||||
|
||||
subcategory.response_rate(school:, academic_year:).meets_student_threshold?
|
||||
end
|
||||
|
||||
def sufficient_teacher_data?(school:, academic_year:)
|
||||
return false unless includes_teacher_survey_items?
|
||||
return false if no_teacher_responses_exist?(school:, academic_year:)
|
||||
|
||||
subcategory.response_rate(school:, academic_year:).meets_teacher_threshold?
|
||||
end
|
||||
|
||||
def no_student_responses_exist?(school:, academic_year:)
|
||||
@no_student_responses_exist ||= Hash.new do |memo, (school, academic_year)|
|
||||
memo[[school, academic_year]] =
|
||||
SurveyItemResponse.where(school:, academic_year:, survey_item: survey_items.student_survey_items).count.zero?
|
||||
end
|
||||
@no_student_responses_exist[[school, academic_year]]
|
||||
end
|
||||
|
||||
def no_teacher_responses_exist?(school:, academic_year:)
|
||||
@no_teacher_responses_exist ||= Hash.new do |memo, (school, academic_year)|
|
||||
memo[[school, academic_year]] =
|
||||
SurveyItemResponse.where(school:, academic_year:, survey_item: survey_items.teacher_survey_items).count.zero?
|
||||
end
|
||||
@no_teacher_responses_exist[[school, academic_year]]
|
||||
end
|
||||
|
||||
def incalculable_score(school:, academic_year:)
|
||||
@incalculable_score ||= Hash.new do |memo, (school, academic_year)|
|
||||
lacks_sufficient_survey_data = !sufficient_student_data?(school:, academic_year:) &&
|
||||
|
|
|
|||
|
|
@ -5,6 +5,9 @@ class Subcategory < ActiveRecord::Base
|
|||
|
||||
has_many :measures
|
||||
has_many :survey_items, through: :measures
|
||||
has_many :admin_data_items, through: :measures
|
||||
has_many :survey_items, through: :measures
|
||||
has_many :scales, through: :measures
|
||||
|
||||
def score(school:, academic_year:)
|
||||
measures.map do |measure|
|
||||
|
|
|
|||
|
|
@ -2,11 +2,15 @@
|
|||
|
||||
class TeacherResponseRateCalculator < ResponseRateCalculator
|
||||
def survey_item_count
|
||||
@survey_item_count ||= @subcategory.measures.map do |measure|
|
||||
measure.teacher_survey_items.reject do |survey_item|
|
||||
survey_item.survey_item_responses.where(school:, academic_year:).none?
|
||||
end.count
|
||||
end.sum
|
||||
@survey_item_count ||= begin
|
||||
survey_items = @subcategory.measures.flat_map(&:teacher_survey_items)
|
||||
|
||||
SurveyItem.joins('inner join survey_item_responses on survey_item_responses.survey_item_id = survey_items.id')
|
||||
.teacher_survey_items
|
||||
.where("survey_item_responses.school": school, "survey_item_responses.academic_year": academic_year, "survey_item_responses.survey_item_id": survey_items)
|
||||
.group('survey_items.id')
|
||||
.having('count(*) >= 0').count.length
|
||||
end
|
||||
end
|
||||
|
||||
def survey_items_have_sufficient_responses?
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ class VarianceChartRowPresenter
|
|||
@meets_student_threshold = score.meets_student_threshold?
|
||||
@measure_name = @measure.name
|
||||
@measure_id = @measure.measure_id
|
||||
@category = @measure.category
|
||||
@category = @measure.subcategory.category
|
||||
end
|
||||
|
||||
def sufficient_data?
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue