From fd8f6014a191b4eeb8632858cf5da1002ed16032 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Thu, 13 Feb 2020 16:52:21 +0800 Subject: [PATCH] 1 --- lib/tasks/statistic_subject_info.rake | 58 ++++++++++++++------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/lib/tasks/statistic_subject_info.rake b/lib/tasks/statistic_subject_info.rake index fb0532177..5f5adf1d4 100644 --- a/lib/tasks/statistic_subject_info.rake +++ b/lib/tasks/statistic_subject_info.rake @@ -8,10 +8,10 @@ namespace :subjects do column_value = "subject_id, study_count, course_study_count, initiative_study, passed_count, course_used_count, " + "school_used_count, created_at, updated_at" Parallel.in_processes(4) do |i| - str = [] 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 |s| + str = [] s.each do |subject| puts("---------------------data_statistic: #{subject.id}") Rails.logger.info("---------------------data_statistic: #{subject.id}") @@ -24,40 +24,44 @@ namespace :subjects do "#{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 "####str: #{str}" - if str.size == 50 || subject == s.last + puts "####str: #{str.size}" + puts "#####{subject == s.last}" + if subject == s.last + put "------------------------------------------------" sql = "REPLACE INTO subject_records(#{column_value}) VALUES #{str.uniq.join(",")}" puts sql ActiveRecord::Base.connection.execute sql - str = [] end end end end - # 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 + subjects.find_in_batches(batch_size: 50) do |s| + Parallel.in_processes(4) do |i| + + end + 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 @@ -77,7 +81,7 @@ namespace :subjects do "choice_shixun_frequency, created_at, updated_at" subjects.find_in_batches(batch_size: 50) do |s| - Parallel.each(s, in_processes: 4) do |subject| + Parallel.each(in_processes: 4) do |subject| puts("---------------------course_info_statistic: #{subject.id}") Rails.logger.info("---------------------course_info_statistic: #{subject.id}") data = Subjects::CourseUsedInfoService.call(subject)