diff --git a/app/lib/dashboard/seeder.rb b/app/lib/dashboard/seeder.rb index 2918796..ff7a570 100644 --- a/app/lib/dashboard/seeder.rb +++ b/app/lib/dashboard/seeder.rb @@ -6,7 +6,7 @@ module Dashboard academic_years << { range: } end - AcademicYear.insert_all(academic_years, unique_by: [:id]) + AcademicYear.upsert_all(academic_years) end def seed_districts_and_schools(csv_file) @@ -30,7 +30,7 @@ module Dashboard is_hs: marked?(hs), slug: school_name.parameterize } end - School.upsert_all(schools) + School.insert_all(schools) Respondent.joins(:school).where.not("school.dese_id": dese_ids).destroy_all School.where.not(dese_id: dese_ids).destroy_all @@ -93,7 +93,7 @@ module Dashboard end end - AdminDataValue.where.not(admin_data_item_id: admin_data_item_ids).delete_all + AdminDataValue.where.not(admin_data_item: admin_data_item_ids).delete_all AdminDataItem.where.not(id: admin_data_item_ids).delete_all end diff --git a/app/models/dashboard/admin_data_item.rb b/app/models/dashboard/admin_data_item.rb index 8595e18..9d87b4d 100644 --- a/app/models/dashboard/admin_data_item.rb +++ b/app/models/dashboard/admin_data_item.rb @@ -1,7 +1,7 @@ module Dashboard class AdminDataItem < ApplicationRecord - belongs_to :dashboard_scale - has_many :dashboard_admin_data_values + belongs_to :scale, class_name: "Scale", foreign_key: :dashboard_scale_id + has_many :admin_data_values, class_name: "AdminDataValue", foreign_key: :dashboard_admin_data_item_id scope :for_measures, lambda { |measures| joins(:scale).where('scale.measure': measures) diff --git a/app/models/dashboard/admin_data_value.rb b/app/models/dashboard/admin_data_value.rb index cb2a437..1064a5c 100644 --- a/app/models/dashboard/admin_data_value.rb +++ b/app/models/dashboard/admin_data_value.rb @@ -1,8 +1,8 @@ module Dashboard class AdminDataValue < ApplicationRecord - belongs_to :school - belongs_to :dashboard_admin_data_item - belongs_to :dashboard_academic_year + belongs_to :school, class_name: "School", foreign_key: :dashboard_school_id + belongs_to :admin_data_item, class_name: "AdminDataItem", foreign_key: :dashboard_admin_data_item_id + belongs_to :academic_year, class_name: "AcademicYear", foreign_key: :dashboard_academic_year_id validates :likert_score, numericality: { greater_than: 0, less_than_or_equal_to: 5 } end diff --git a/app/models/dashboard/category.rb b/app/models/dashboard/category.rb index bfe879e..b98cfb8 100644 --- a/app/models/dashboard/category.rb +++ b/app/models/dashboard/category.rb @@ -5,7 +5,7 @@ module Dashboard scope :sorted, -> { order(:category_id) } - has_many :subcategories, class_name: "Subcategory", foreign_key: :dashboard_category_id + has_many :subcategories, class_name: "Subcategory", foreign_key: :dashboard_categories_id has_many :measures, through: :subcategories has_many :admin_data_items, through: :measures has_many :scales, through: :subcategories diff --git a/app/models/dashboard/measure.rb b/app/models/dashboard/measure.rb index c7d5533..c1cce60 100644 --- a/app/models/dashboard/measure.rb +++ b/app/models/dashboard/measure.rb @@ -1,6 +1,6 @@ module Dashboard class Measure < ApplicationRecord - belongs_to :dashboard_subcategory + belongs_to :subcategory, class_name: "Subcategory", foreign_key: :dashboard_subcategory_id has_one :dashboard_category, through: :dashboard_subcategory has_many :dashboard_scales has_many :dashboard_admin_data_items, through: :scales diff --git a/app/models/dashboard/scale.rb b/app/models/dashboard/scale.rb index f46a6c0..6f47ab4 100644 --- a/app/models/dashboard/scale.rb +++ b/app/models/dashboard/scale.rb @@ -1,9 +1,9 @@ module Dashboard class Scale < ApplicationRecord - belongs_to :dashboard_measure - has_many :dashboard_survey_items - has_many :dashboard_survey_item_responses, through: :dashboard_survey_items - has_many :dashboard_admin_data_items + belongs_to :measure, class_name: "Measure", foreign_key: :dashboard_measure_id + has_many :survey_items + has_many :survey_item_responses, through: :survey_items + has_many :admin_data_items, class_name: "AdminDataItem", foreign_key: :admin_data_item_id def score(school:, academic_year:) @score ||= Hash.new do |memo, (school, academic_year)| diff --git a/app/models/dashboard/student_response_rate_calculator.rb b/app/models/dashboard/student_response_rate_calculator.rb index da563e8..29f59a8 100644 --- a/app/models/dashboard/student_response_rate_calculator.rb +++ b/app/models/dashboard/student_response_rate_calculator.rb @@ -1,67 +1,69 @@ # frozen_string_literal: true -class StudentResponseRateCalculator < ResponseRateCalculator - def raw_response_rate - rates_by_grade.values.length.positive? ? weighted_average : 0 - end - - def weighted_average - num_possible_responses = 0.0 - rates_by_grade.keys.map do |grade| - num_possible_responses += enrollment_by_grade[grade] +module Dashboard + class StudentResponseRateCalculator < ResponseRateCalculator + def raw_response_rate + rates_by_grade.values.length.positive? ? weighted_average : 0 end - rates_by_grade.map do |grade, rate| - rate * (enrollment_by_grade[grade] / num_possible_responses) - end.sum - end - def rates_by_grade - @rates_by_grade ||= enrollment_by_grade.map do |grade, num_of_students_in_grade| - responses = survey_items_with_sufficient_responses(grade:) - actual_response_count_for_grade = responses.values.sum.to_f - count_of_survey_items_with_sufficient_responses = responses.count - if actual_response_count_for_grade.nil? || count_of_survey_items_with_sufficient_responses.nil? || count_of_survey_items_with_sufficient_responses.zero? || num_of_students_in_grade.nil? || num_of_students_in_grade.zero? - next nil + def weighted_average + num_possible_responses = 0.0 + rates_by_grade.keys.map do |grade| + num_possible_responses += enrollment_by_grade[grade] end + rates_by_grade.map do |grade, rate| + rate * (enrollment_by_grade[grade] / num_possible_responses) + end.sum + end - rate = actual_response_count_for_grade / count_of_survey_items_with_sufficient_responses / num_of_students_in_grade * 100 - [grade, rate] - end.compact.to_h - end + def rates_by_grade + @rates_by_grade ||= enrollment_by_grade.map do |grade, num_of_students_in_grade| + responses = survey_items_with_sufficient_responses(grade:) + actual_response_count_for_grade = responses.values.sum.to_f + count_of_survey_items_with_sufficient_responses = responses.count + if actual_response_count_for_grade.nil? || count_of_survey_items_with_sufficient_responses.nil? || count_of_survey_items_with_sufficient_responses.zero? || num_of_students_in_grade.nil? || num_of_students_in_grade.zero? + next nil + end - def enrollment_by_grade - @enrollment_by_grade ||= respondents.enrollment_by_grade - end + rate = actual_response_count_for_grade / count_of_survey_items_with_sufficient_responses / num_of_students_in_grade * 100 + [grade, rate] + end.compact.to_h + end - def survey_items_have_sufficient_responses? - rates_by_grade.values.length.positive? - end + def enrollment_by_grade + @enrollment_by_grade ||= respondents.enrollment_by_grade + end + + def survey_items_have_sufficient_responses? + rates_by_grade.values.length.positive? + end - def survey_items_with_sufficient_responses(grade:) - @survey_items_with_sufficient_responses ||= Hash.new do |memo, grade| - threshold = 10 - quarter_of_grade = enrollment_by_grade[grade] / 4 - threshold = threshold > quarter_of_grade ? quarter_of_grade : threshold + def survey_items_with_sufficient_responses(grade:) + @survey_items_with_sufficient_responses ||= Hash.new do |memo, grade| + threshold = 10 + quarter_of_grade = enrollment_by_grade[grade] / 4 + threshold = threshold > quarter_of_grade ? quarter_of_grade : threshold - si = SurveyItemResponse.student_survey_items_with_sufficient_responses_by_grade(school:, - academic_year:) - si = si.reject do |_key, value| - value < threshold - end + si = SurveyItemResponse.student_survey_items_with_sufficient_responses_by_grade(school:, + academic_year:) + si = si.reject do |_key, value| + value < threshold + end - ssi = @subcategory.student_survey_items.map(&:id) - grade_array = Array.new(ssi.length, grade) + ssi = @subcategory.student_survey_items.map(&:id) + grade_array = Array.new(ssi.length, grade) - memo[grade] = si.slice(*grade_array.zip(ssi)) + memo[grade] = si.slice(*grade_array.zip(ssi)) + end + @survey_items_with_sufficient_responses[grade] end - @survey_items_with_sufficient_responses[grade] - end - def total_possible_responses - @total_possible_responses ||= begin - return 0 unless respondents.present? + def total_possible_responses + @total_possible_responses ||= begin + return 0 unless respondents.present? - respondents.total_students + respondents.total_students + end end end end diff --git a/app/models/dashboard/subcategory.rb b/app/models/dashboard/subcategory.rb index 3d3f429..9e2f723 100644 --- a/app/models/dashboard/subcategory.rb +++ b/app/models/dashboard/subcategory.rb @@ -1,12 +1,12 @@ module Dashboard class Subcategory < ApplicationRecord - belongs_to :category, class_name: "Category", foreign_key: :dashboard_category_id + belongs_to :category, class_name: "Category", foreign_key: :dashboard_categories_id - has_many :dashboard_measures - has_many :dashboard_survey_items, through: :dashboard_measures - has_many :dashboard_admin_data_items, through: :dashboard_measures - has_many :dashboard_survey_items, through: :dashboard_measures - has_many :dashboard_scales, through: :dashboard_measures + has_many :measures, class_name: "Measure", foreign_key: :dashboard_subcategory_id + 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| diff --git a/app/models/dashboard/survey_item.rb b/app/models/dashboard/survey_item.rb index 40621c1..cecf380 100644 --- a/app/models/dashboard/survey_item.rb +++ b/app/models/dashboard/survey_item.rb @@ -1,11 +1,10 @@ module Dashboard class SurveyItem < ApplicationRecord - # belongs_to :dashboard_scale + belongs_to :scale, class_name: "Scale", foreign_key: :dashboard_scale_id + has_many :survey_item_responses, class_name: "SurveyItemResponse", foreign_key: :dashboard_survey_item_id - # has_one :dashboard_measure, through: dashboard_scale - # has_one :dashboard_subcategory, through: dashboard_measure - - has_many :dashboard_survey_item_responses + has_one :measure, through: :scale + has_one :subcategory, through: :measure def score(school:, academic_year:) @score ||= Hash.new do |memo, (school, academic_year)| diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index 5ff741c..1038077 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -8,7 +8,7 @@ namespace :dashboard do "2023-24" seeder.seed_districts_and_schools Dashboard::Engine.root.join("data", "dashboard", "master_list_of_schools_and_districts.csv") - # seeder.seed_sqm_framework Dashboard::Engine.root.join("data", "dashboard", "sqm_framework.csv") + seeder.seed_sqm_framework Dashboard::Engine.root.join("data", "dashboard", "sqm_framework.csv") # seeder.seed_demographics Rails.root.join("data", "demographics.csv") # seeder.seed_enrollment Rails.root.join("data", "enrollment", "enrollment.csv") # seeder.seed_enrollment Rails.root.join("data", "enrollment", "nj_enrollment.csv")