mirror of
https://github.com/edcommonwealth/sqm-dashboards.git
synced 2026-03-07 21:48:16 -08:00
load total students and batch importing records
This commit is contained in:
parent
bfa5f28d7b
commit
d059177f0c
2 changed files with 16 additions and 3 deletions
|
|
@ -5,6 +5,7 @@ require 'csv'
|
|||
class EnrollmentLoader
|
||||
def self.load_data(filepath:)
|
||||
schools = []
|
||||
enrollments = []
|
||||
CSV.parse(File.read(filepath), headers: true) do |row|
|
||||
row = EnrollmentRowValues.new(row:)
|
||||
|
||||
|
|
@ -12,16 +13,20 @@ class EnrollmentLoader
|
|||
|
||||
schools << row.school
|
||||
|
||||
create_enrollment_entry(row:)
|
||||
enrollments << create_enrollment_entry(row:)
|
||||
end
|
||||
|
||||
# It's possible that instead of updating all columns on duplicate key, we could just update the student columns and leave total_teachers alone. Right now enrollment data loads before staffing data so it works correctly.
|
||||
Respondent.import enrollments, batch_size: 1000,
|
||||
on_duplicate_key_update: %i[pk k one two three four five six seven eight nine ten eleven twelve total_students]
|
||||
|
||||
Respondent.where.not(school: schools).destroy_all
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.create_enrollment_entry(row:)
|
||||
respondent = Respondent.find_or_create_by(school: row.school, academic_year: row.academic_year)
|
||||
respondent = Respondent.find_or_initialize_by(school: row.school, academic_year: row.academic_year)
|
||||
respondent.pk = row.pk
|
||||
respondent.k = row.k
|
||||
respondent.one = row.one
|
||||
|
|
@ -36,7 +41,8 @@ class EnrollmentLoader
|
|||
respondent.ten = row.ten
|
||||
respondent.eleven = row.eleven
|
||||
respondent.twelve = row.twelve
|
||||
respondent.save
|
||||
respondent.total_students = row.total_students
|
||||
respondent
|
||||
end
|
||||
|
||||
private_class_method :create_enrollment_entry
|
||||
|
|
@ -118,4 +124,8 @@ class EnrollmentRowValues
|
|||
def twelve
|
||||
row['12']
|
||||
end
|
||||
|
||||
def total_students
|
||||
row['Total'].gsub(',', '').to_i
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -23,15 +23,18 @@ describe EnrollmentLoader do
|
|||
it 'loads the correct enrollment numbers' do
|
||||
academic_year = ay_2022_23
|
||||
expect(Respondent.find_by(school: attleboro, academic_year:).nine).to eq 506
|
||||
# expect(Respondent.find_by(school: attleboro, academic_year:).total_students).to eq 1844
|
||||
|
||||
expect(Respondent.find_by(school: beachmont, academic_year:).pk).to eq 34
|
||||
expect(Respondent.find_by(school: beachmont, academic_year:).k).to eq 64
|
||||
expect(Respondent.find_by(school: beachmont, academic_year:).one).to eq 58
|
||||
expect(Respondent.find_by(school: beachmont, academic_year:).total_students).to eq 336
|
||||
|
||||
expect(Respondent.find_by(school: winchester, academic_year:).nine).to eq 361
|
||||
expect(Respondent.find_by(school: winchester, academic_year:).ten).to eq 331
|
||||
expect(Respondent.find_by(school: winchester, academic_year:).eleven).to eq 339
|
||||
expect(Respondent.find_by(school: winchester, academic_year:).twelve).to eq 352
|
||||
expect(Respondent.find_by(school: winchester, academic_year:).total_students).to eq 1383
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue