From 7cf6c6cc241059a5707c558ebaf54a2993bde528 Mon Sep 17 00:00:00 2001 From: rebuilt Date: Fri, 9 Jun 2023 16:58:51 -0700 Subject: [PATCH] Modify subcategory report to be threaded --- app/models/report/subcategory.rb | 75 +++++++++++++++++++------------- 1 file changed, 44 insertions(+), 31 deletions(-) diff --git a/app/models/report/subcategory.rb b/app/models/report/subcategory.rb index ac8356c8..aa31f6c0 100644 --- a/app/models/report/subcategory.rb +++ b/app/models/report/subcategory.rb @@ -1,43 +1,56 @@ module Report 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 = [] + mutex = Thread::Mutex.new 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'] - schools.each do |school| - academic_years.each do |academic_year| - subcategories.each do |subcategory| - respondents = Respondent.find_by(school:, academic_year:) - next if respondents.nil? - - response_rate = subcategory.response_rate(school:, academic_year:) - next unless response_rate.meets_student_threshold? || response_rate.meets_teacher_threshold? - - score = subcategory.score(school:, academic_year:) - zone = subcategory.zone(school:, academic_year:).type.to_s.capitalize - - row = [response_rate, subcategory, school, academic_year] - - all_grades = respondents.counts_by_grade.keys - grades = "#{all_grades.first}-#{all_grades.last}" - data << [school.district.name, - school.name, - school.dese_id, - academic_year.range, - grades, - subcategory.subcategory_id, - student_score(row:), - student_zone(row:), - teacher_score(row:), - teacher_zone(row:), - admin_score(row:), - admin_zone(row:), - score, - zone] + pool_size = 2 + jobs = Queue.new + schools.each { |school| jobs << school } + + workers = pool_size.times.map do + Thread.new do + while school = jobs.pop(true) + academic_years.each do |academic_year| + subcategories.each do |subcategory| + respondents = Respondent.find_by(school:, academic_year:) + next if respondents.nil? + + response_rate = subcategory.response_rate(school:, academic_year:) + next unless response_rate.meets_student_threshold? || response_rate.meets_teacher_threshold? + + score = subcategory.score(school:, academic_year:) + zone = subcategory.zone(school:, academic_year:).type.to_s.capitalize + + row = [response_rate, subcategory, school, academic_year] + + all_grades = respondents.counts_by_grade.keys + grades = "#{all_grades.first}-#{all_grades.last}" + mutex.synchronize do + data << [school.district.name, + school.name, + school.dese_id, + academic_year.range, + grades, + subcategory.subcategory_id, + student_score(row:), + student_zone(row:), + teacher_score(row:), + teacher_zone(row:), + admin_score(row:), + admin_zone(row:), + score, + zone] + end + end + end end + rescue ThreadError end end + workers.each(&:join) FileUtils.mkdir_p Rails.root.join('tmp', 'reports') filepath = Rails.root.join('tmp', 'reports', filename) write_csv(data:, filepath:)