sqm-dashboards/app/services/staffing_loader.rb
rebuilt 07ed8dd259 Update logic for calculating student response rate. Remove references
to survey table.  We no longer check or keep track of the survey type.
Instead we look in the database to see if a survey item has at least 10
responses.  If it does, that survey item was presented to the respondent
and we count it, and all responses when calculating the response rate.

Remove response rate timestamp from caching logic because we no longer
add the response rate to the database. All response rates are calculated
on the fly

Update three_b_two scraper to use teacher only numbers

swap over to using https://profiles.doe.mass.edu/statereport/gradesubjectstaffing.aspx as the source of staffing information
2023-04-22 14:00:20 -07:00

72 lines
1.8 KiB
Ruby

# frozen_string_literal: true
require 'csv'
class StaffingLoader
def self.load_data(filepath:)
schools = []
respondents = []
CSV.parse(File.read(filepath), headers: true) do |row|
row = StaffingRowValues.new(row:)
next unless row.school.present? && row.academic_year.present?
schools << row.school
respondents << create_staffing_entry(row:)
end
Respondent.import respondents, batch_size: 1000, on_duplicate_key_update: [:total_teachers]
Respondent.where.not(school: schools).destroy_all
end
def self.clone_previous_year_data
years = AcademicYear.order(:range).last(2)
previous_year = years.first
current_year = years.last
respondents = []
School.all.each do |school|
Respondent.where(school:, academic_year: previous_year).each do |respondent|
current_respondent = Respondent.find_or_initialize_by(school:, academic_year: current_year)
current_respondent.total_teachers = respondent.total_teachers
respondents << current_respondent
end
end
Respondent.import respondents, batch_size: 1000, on_duplicate_key_update: [:total_teachers]
end
private
def self.create_staffing_entry(row:)
respondent = Respondent.find_or_initialize_by(school: row.school, academic_year: row.academic_year)
respondent.total_teachers = row.fte_count
respondent
end
private_class_method :create_staffing_entry
end
class StaffingRowValues
attr_reader :row
def initialize(row:)
@row = row
end
def school
@school ||= begin
dese_id = row['DESE ID'].strip.to_i
School.find_by_dese_id(dese_id)
end
end
def academic_year
@academic_year ||= begin
year = row['Academic Year']
AcademicYear.find_by_range(year)
end
end
def fte_count
row['FTE Count']
end
end