fix: correctly memoize student_survey_items_with_sufficient_responses so that on the analyze page, when the first year has insufficient responses, it doesn't affect all subsequent years

mciea-main
Nelson Jovel 2 years ago
parent b77d6134df
commit 53f3362a34

@ -25,7 +25,8 @@ class Measure < ActiveRecord::Base
end end
def student_survey_items_with_sufficient_responses(school:, academic_year:) def student_survey_items_with_sufficient_responses(school:, academic_year:)
@student_survey_items_with_sufficient_responses ||= SurveyItem.where(id: SurveyItem.joins("inner join survey_item_responses on survey_item_responses.survey_item_id = survey_items.id") @student_survey_items_with_sufficient_responses ||= Hash.new do |memo, (school, academic_year)|
memo[[school, academic_year]] = SurveyItem.where(id: SurveyItem.joins("inner join survey_item_responses on survey_item_responses.survey_item_id = survey_items.id")
.student_survey_items .student_survey_items
.where("survey_item_responses.school": school, .where("survey_item_responses.school": school,
"survey_item_responses.academic_year": academic_year, "survey_item_responses.academic_year": academic_year,
@ -34,6 +35,8 @@ class Measure < ActiveRecord::Base
.group("survey_items.id") .group("survey_items.id")
.having("count(*) >= 10") .having("count(*) >= 10")
.count.keys) .count.keys)
end
@student_survey_items_with_sufficient_responses[[school, academic_year]]
end end
def teacher_scales def teacher_scales

@ -31,6 +31,11 @@ module Analyze
def basis def basis
"student surveys" "student surveys"
end end
def n_size(academic_year)
SurveyItemResponse.where(survey_item: measure.student_survey_items, school:, grade: grades,
academic_year:).select(:response_id).distinct.count
end
end end
end end
end end

@ -13,8 +13,8 @@ module Analyze
end end
def show_insufficient_data_message? def show_insufficient_data_message?
scores = academic_years.map do |year| scores = academic_years.map do |academic_year|
measure.score(school:, academic_year: year) measure.student_score(school:, academic_year:)
end end
scores.none? { |score| score.meets_student_threshold? } scores.none? { |score| score.meets_student_threshold? }
@ -27,6 +27,11 @@ module Analyze
def type def type
:student :student
end end
def n_size(academic_year)
SurveyItemResponse.where(survey_item: measure.student_survey_items, school:, grade: grades,
academic_year:).select(:response_id).distinct.count
end
end end
end end
end end

@ -21,13 +21,13 @@ module Analyze
end end
def bars def bars
@bars ||= yearly_scores.map.each_with_index do |yearly_score, index| @bars ||= academic_years.map.with_index do |academic_year, index|
year = yearly_score.year Analyze::BarPresenter.new(measure:, academic_year:,
Analyze::BarPresenter.new(measure:, academic_year: year, score: score(academic_year),
score: yearly_score.score,
x_position: bar_x(index), x_position: bar_x(index),
color: bar_color(year)) color: bar_color(academic_year))
end.reject(&:blank?).select { |bar| bar.score.average&.positive? } end.reject(&:blank?).select { |bar| bar.score.average&.positive? }
@bars
end end
def label def label
@ -50,6 +50,10 @@ module Analyze
raise NotImplementedError raise NotImplementedError
end end
def n_size(academic_year)
raise NotImplementedError
end
def basis def basis
"student surveys" "student surveys"
end end
@ -111,13 +115,8 @@ module Analyze
%i[student teacher].include? type %i[student teacher].include? type
end end
def n_size(year_index) def popover_content(academic_year)
SurveyItemResponse.where(survey_item: measure.student_survey_items, school:, grade: grades, "#{n_size(academic_year)} #{type.to_s.capitalize}s"
academic_year: academic_years[year_index]).select(:response_id).distinct.count
end
def popover_content(year_index)
"#{n_size(year_index)} #{type.to_s.capitalize}s"
end end
def insufficiency_message def insufficiency_message
@ -130,12 +129,12 @@ module Analyze
private private
YearlyScore = Struct.new(:year, :score) # YearlyScore = Struct.new(:year, :score)
def yearly_scores # def yearly_scores
yearly_scores = academic_years.each.map do |year| # @yearly_scores ||= academic_years.each.map do |year|
YearlyScore.new(year, score(year)) # YearlyScore.new(year, score(year))
end.reject { |year| year.score.nil? || year.score.blank? } # end.reject { |year| year.score.nil? || year.score.blank? }
end # end
def bar_color(year) def bar_color(year)
@available_academic_years ||= AcademicYear.order(:range).all @available_academic_years ||= AcademicYear.order(:range).all
@ -144,7 +143,7 @@ module Analyze
def bar_x(index) def bar_x(index)
column_start_x + (index * bar_width * 1.2) + column_start_x + (index * bar_width * 1.2) +
((column_end_x - column_start_x) - (yearly_scores.size * bar_width * 1.2)) / 2 ((column_end_x - column_start_x) - (academic_years.size * bar_width * 1.2)) / 2
end end
end end
end end

Loading…
Cancel
Save