Modify subcategory report to be threaded

rpp-main
rebuilt 3 years ago
parent f29d72abff
commit 7cf6c6cc24

@ -1,43 +1,56 @@
module Report module Report
class Subcategory class Subcategory
def self.create_report(schools: School.all, academic_years: AcademicYear.all, subcategories: ::Subcategory.all, filename: 'subcategories.csv') def self.create_report(schools: School.all.includes(:district), academic_years: AcademicYear.all, subcategories: ::Subcategory.all, filename: 'subcategories.csv')
data = [] data = []
mutex = Thread::Mutex.new
data << ['District', 'School', 'School Code', 'Academic Year', 'Grades', 'Subcategory', 'Student Score', 'Student Zone', 'Teacher Score', data << ['District', 'School', 'School Code', 'Academic Year', 'Grades', 'Subcategory', 'Student Score', 'Student Zone', 'Teacher Score',
'Teacher Zone', 'Admin Score', 'Admin Zone', 'All Score (Average)', 'All Score Zone'] 'Teacher Zone', 'Admin Score', 'Admin Zone', 'All Score (Average)', 'All Score Zone']
schools.each do |school| pool_size = 2
academic_years.each do |academic_year| jobs = Queue.new
subcategories.each do |subcategory| schools.each { |school| jobs << school }
respondents = Respondent.find_by(school:, academic_year:)
next if respondents.nil? workers = pool_size.times.map do
Thread.new do
response_rate = subcategory.response_rate(school:, academic_year:) while school = jobs.pop(true)
next unless response_rate.meets_student_threshold? || response_rate.meets_teacher_threshold? academic_years.each do |academic_year|
subcategories.each do |subcategory|
score = subcategory.score(school:, academic_year:) respondents = Respondent.find_by(school:, academic_year:)
zone = subcategory.zone(school:, academic_year:).type.to_s.capitalize next if respondents.nil?
row = [response_rate, subcategory, school, academic_year] response_rate = subcategory.response_rate(school:, academic_year:)
next unless response_rate.meets_student_threshold? || response_rate.meets_teacher_threshold?
all_grades = respondents.counts_by_grade.keys
grades = "#{all_grades.first}-#{all_grades.last}" score = subcategory.score(school:, academic_year:)
data << [school.district.name, zone = subcategory.zone(school:, academic_year:).type.to_s.capitalize
school.name,
school.dese_id, row = [response_rate, subcategory, school, academic_year]
academic_year.range,
grades, all_grades = respondents.counts_by_grade.keys
subcategory.subcategory_id, grades = "#{all_grades.first}-#{all_grades.last}"
student_score(row:), mutex.synchronize do
student_zone(row:), data << [school.district.name,
teacher_score(row:), school.name,
teacher_zone(row:), school.dese_id,
admin_score(row:), academic_year.range,
admin_zone(row:), grades,
score, subcategory.subcategory_id,
zone] student_score(row:),
student_zone(row:),
teacher_score(row:),
teacher_zone(row:),
admin_score(row:),
admin_zone(row:),
score,
zone]
end
end
end
end end
rescue ThreadError
end end
end end
workers.each(&:join)
FileUtils.mkdir_p Rails.root.join('tmp', 'reports') FileUtils.mkdir_p Rails.root.join('tmp', 'reports')
filepath = Rails.root.join('tmp', 'reports', filename) filepath = Rails.root.join('tmp', 'reports', filename)
write_csv(data:, filepath:) write_csv(data:, filepath:)

Loading…
Cancel
Save