diff --git a/lib/tasks/statistic_subject_info.rake b/lib/tasks/statistic_subject_info.rake index d39e1fe86..ccca9d28b 100644 --- a/lib/tasks/statistic_subject_info.rake +++ b/lib/tasks/statistic_subject_info.rake @@ -7,9 +7,11 @@ namespace :subjects do subjects = Subject.where(status: 2, hidden: 0) column_value = "subject_id, study_count, course_study_count, initiative_study, passed_count, course_used_count, " + "school_used_count, created_at, updated_at" - subjects.find_in_batches(batch_size: 50) do |s| + Parallel.in_processes(4) do |i| str = [] - Parallel.each_with_index(s, in_processes: 4) do |subject, index| + puts("Parallel.worker_number: #{Parallel.worker_number}") + subjects.find_in_batches(start: (Parallel.worker_number/4) * subjects.count, + finish: ((Parallel.worker_number+1)/4) * subjects.count) do |subject| puts("---------------------data_statistic: #{subject.id}") Rails.logger.info("---------------------data_statistic: #{subject.id}") data = Subjects::DataStatisticService.new(subject) @@ -22,15 +24,40 @@ namespace :subjects do "'#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')") puts "index: #{index}; worker_number: #{Parallel.worker_number}" puts "####str: #{str}" - if str.size == 100 || subject == s.last + if str.size == 50 || subject == s.last sql = "REPLACE INTO subject_records(#{column_value}) VALUES #{str.uniq.join(",")}" puts sql ActiveRecord::Base.connection.execute sql str = [] end end + end - if buffer_size > 0 + + # subjects.find_in_batches(batch_size: 50) do |s| + # str = [] + # Parallel.each_with_index(s, in_processes: 4) do |subject, index| + # puts("---------------------data_statistic: #{subject.id}") + # Rails.logger.info("---------------------data_statistic: #{subject.id}") + # data = Subjects::DataStatisticService.new(subject) + # study_count = data.study_count + # next if study_count == 0 + # course_study_count = data.course_study_count + # initiative_study = study_count - course_study_count + # str << ("(#{subject.id}, #{study_count}, #{course_study_count}, #{initiative_study}, " + + # "#{data.passed_count}, #{data.course_used_count}, #{data.school_used_count}, " + + # "'#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')") + # puts "index: #{index}; worker_number: #{Parallel.worker_number}" + # puts "####str: #{str}" + # if str.size == 100 || subject == s.last + # sql = "REPLACE INTO subject_records(#{column_value}) VALUES #{str.uniq.join(",")}" + # puts sql + # ActiveRecord::Base.connection.execute sql + # str = [] + # end + # end + # end + if str.size > 0 sql = "REPLACE INTO subject_records(#{column_value}) VALUES #{str.uniq.join(",")}" puts sql ActiveRecord::Base.connection.execute sql