chore: seed framework

main
Nelson Jovel 2 years ago
parent cd7b05df73
commit 27550e0b30

@ -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

@ -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)

@ -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

@ -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

@ -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

@ -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)|

@ -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

@ -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|

@ -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)|

@ -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")

Loading…
Cancel
Save