From 6b2d0ffb63b3bb65e9df86c56cea9adaf42e0613 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Thu, 13 Feb 2020 16:12:10 +0800 Subject: [PATCH] 1 --- lib/tasks/statistic_subject_info.rake | 38 ++++++++++++++------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/lib/tasks/statistic_subject_info.rake b/lib/tasks/statistic_subject_info.rake index ccca9d28b..44c3d2cf7 100644 --- a/lib/tasks/statistic_subject_info.rake +++ b/lib/tasks/statistic_subject_info.rake @@ -11,24 +11,26 @@ namespace :subjects do 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 |subject| - 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 == 50 || subject == s.last - sql = "REPLACE INTO subject_records(#{column_value}) VALUES #{str.uniq.join(",")}" - puts sql - ActiveRecord::Base.connection.execute sql - str = [] + finish: ((Parallel.worker_number+1)/4) * subjects.count) do |s| + s.each do |subject| + 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 == 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