Adjustreact
daiao 5 years ago
parent 5491643af9
commit 6b2d0ffb63

@ -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

Loading…
Cancel
Save