chore: seed framework

main
Nelson Jovel 2 years ago
parent cd7b05df73
commit 27550e0b30

@ -6,7 +6,7 @@ module Dashboard
academic_years << { range: } academic_years << { range: }
end end
AcademicYear.insert_all(academic_years, unique_by: [:id]) AcademicYear.upsert_all(academic_years)
end end
def seed_districts_and_schools(csv_file) def seed_districts_and_schools(csv_file)
@ -30,7 +30,7 @@ module Dashboard
is_hs: marked?(hs), slug: school_name.parameterize } is_hs: marked?(hs), slug: school_name.parameterize }
end end
School.upsert_all(schools) School.insert_all(schools)
Respondent.joins(:school).where.not("school.dese_id": dese_ids).destroy_all Respondent.joins(:school).where.not("school.dese_id": dese_ids).destroy_all
School.where.not(dese_id: dese_ids).destroy_all School.where.not(dese_id: dese_ids).destroy_all
@ -93,7 +93,7 @@ module Dashboard
end end
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 AdminDataItem.where.not(id: admin_data_item_ids).delete_all
end end

@ -1,7 +1,7 @@
module Dashboard module Dashboard
class AdminDataItem < ApplicationRecord class AdminDataItem < ApplicationRecord
belongs_to :dashboard_scale belongs_to :scale, class_name: "Scale", foreign_key: :dashboard_scale_id
has_many :dashboard_admin_data_values has_many :admin_data_values, class_name: "AdminDataValue", foreign_key: :dashboard_admin_data_item_id
scope :for_measures, lambda { |measures| scope :for_measures, lambda { |measures|
joins(:scale).where('scale.measure': measures) joins(:scale).where('scale.measure': measures)

@ -1,8 +1,8 @@
module Dashboard module Dashboard
class AdminDataValue < ApplicationRecord class AdminDataValue < ApplicationRecord
belongs_to :school belongs_to :school, class_name: "School", foreign_key: :dashboard_school_id
belongs_to :dashboard_admin_data_item belongs_to :admin_data_item, class_name: "AdminDataItem", foreign_key: :dashboard_admin_data_item_id
belongs_to :dashboard_academic_year 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 } validates :likert_score, numericality: { greater_than: 0, less_than_or_equal_to: 5 }
end end

@ -5,7 +5,7 @@ module Dashboard
scope :sorted, -> { order(:category_id) } 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 :measures, through: :subcategories
has_many :admin_data_items, through: :measures has_many :admin_data_items, through: :measures
has_many :scales, through: :subcategories has_many :scales, through: :subcategories

@ -1,6 +1,6 @@
module Dashboard module Dashboard
class Measure < ApplicationRecord 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_one :dashboard_category, through: :dashboard_subcategory
has_many :dashboard_scales has_many :dashboard_scales
has_many :dashboard_admin_data_items, through: :scales has_many :dashboard_admin_data_items, through: :scales

@ -1,9 +1,9 @@
module Dashboard module Dashboard
class Scale < ApplicationRecord class Scale < ApplicationRecord
belongs_to :dashboard_measure belongs_to :measure, class_name: "Measure", foreign_key: :dashboard_measure_id
has_many :dashboard_survey_items has_many :survey_items
has_many :dashboard_survey_item_responses, through: :dashboard_survey_items has_many :survey_item_responses, through: :survey_items
has_many :dashboard_admin_data_items has_many :admin_data_items, class_name: "AdminDataItem", foreign_key: :admin_data_item_id
def score(school:, academic_year:) def score(school:, academic_year:)
@score ||= Hash.new do |memo, (school, academic_year)| @score ||= Hash.new do |memo, (school, academic_year)|

@ -1,67 +1,69 @@
# frozen_string_literal: true # frozen_string_literal: true
class StudentResponseRateCalculator < ResponseRateCalculator module Dashboard
def raw_response_rate class StudentResponseRateCalculator < ResponseRateCalculator
rates_by_grade.values.length.positive? ? weighted_average : 0 def raw_response_rate
end rates_by_grade.values.length.positive? ? weighted_average : 0
def weighted_average
num_possible_responses = 0.0
rates_by_grade.keys.map do |grade|
num_possible_responses += enrollment_by_grade[grade]
end end
rates_by_grade.map do |grade, rate|
rate * (enrollment_by_grade[grade] / num_possible_responses)
end.sum
end
def rates_by_grade def weighted_average
@rates_by_grade ||= enrollment_by_grade.map do |grade, num_of_students_in_grade| num_possible_responses = 0.0
responses = survey_items_with_sufficient_responses(grade:) rates_by_grade.keys.map do |grade|
actual_response_count_for_grade = responses.values.sum.to_f num_possible_responses += enrollment_by_grade[grade]
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 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 def rates_by_grade
[grade, rate] @rates_by_grade ||= enrollment_by_grade.map do |grade, num_of_students_in_grade|
end.compact.to_h responses = survey_items_with_sufficient_responses(grade:)
end 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 rate = actual_response_count_for_grade / count_of_survey_items_with_sufficient_responses / num_of_students_in_grade * 100
@enrollment_by_grade ||= respondents.enrollment_by_grade [grade, rate]
end end.compact.to_h
end
def survey_items_have_sufficient_responses? def enrollment_by_grade
rates_by_grade.values.length.positive? @enrollment_by_grade ||= respondents.enrollment_by_grade
end end
def survey_items_have_sufficient_responses?
rates_by_grade.values.length.positive?
end
def survey_items_with_sufficient_responses(grade:) def survey_items_with_sufficient_responses(grade:)
@survey_items_with_sufficient_responses ||= Hash.new do |memo, grade| @survey_items_with_sufficient_responses ||= Hash.new do |memo, grade|
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
si = SurveyItemResponse.student_survey_items_with_sufficient_responses_by_grade(school:, si = SurveyItemResponse.student_survey_items_with_sufficient_responses_by_grade(school:,
academic_year:) academic_year:)
si = si.reject do |_key, value| si = si.reject do |_key, value|
value < threshold value < threshold
end end
ssi = @subcategory.student_survey_items.map(&:id) ssi = @subcategory.student_survey_items.map(&:id)
grade_array = Array.new(ssi.length, grade) 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 end
@survey_items_with_sufficient_responses[grade]
end
def total_possible_responses def total_possible_responses
@total_possible_responses ||= begin @total_possible_responses ||= begin
return 0 unless respondents.present? return 0 unless respondents.present?
respondents.total_students respondents.total_students
end
end end
end end
end end

@ -1,12 +1,12 @@
module Dashboard module Dashboard
class Subcategory < ApplicationRecord 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 :measures, class_name: "Measure", foreign_key: :dashboard_subcategory_id
has_many :dashboard_survey_items, through: :dashboard_measures has_many :survey_items, through: :measures
has_many :dashboard_admin_data_items, through: :dashboard_measures has_many :admin_data_items, through: :measures
has_many :dashboard_survey_items, through: :dashboard_measures has_many :survey_items, through: :measures
has_many :dashboard_scales, through: :dashboard_measures has_many :scales, through: :measures
def score(school:, academic_year:) def score(school:, academic_year:)
measures.map do |measure| measures.map do |measure|

@ -1,11 +1,10 @@
module Dashboard module Dashboard
class SurveyItem < ApplicationRecord 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 :measure, through: :scale
# has_one :dashboard_subcategory, through: dashboard_measure has_one :subcategory, through: :measure
has_many :dashboard_survey_item_responses
def score(school:, academic_year:) def score(school:, academic_year:)
@score ||= Hash.new do |memo, (school, academic_year)| @score ||= Hash.new do |memo, (school, academic_year)|

@ -8,7 +8,7 @@ namespace :dashboard do
"2023-24" "2023-24"
seeder.seed_districts_and_schools Dashboard::Engine.root.join("data", "dashboard", seeder.seed_districts_and_schools Dashboard::Engine.root.join("data", "dashboard",
"master_list_of_schools_and_districts.csv") "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_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", "enrollment.csv")
# seeder.seed_enrollment Rails.root.join("data", "enrollment", "nj_enrollment.csv") # seeder.seed_enrollment Rails.root.join("data", "enrollment", "nj_enrollment.csv")

Loading…
Cancel
Save