Adjustreact
daiao 5 years ago
parent 4a7eeb8c90
commit fd8f6014a1

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

Loading…
Cancel
Save