Adjustreact
daiao 5 years ago
parent aa5438bad8
commit 662ffbbea1

@ -11,7 +11,7 @@ class Subjects::ShixunUsedInfoService < ApplicationService
position = stage.position position = stage.position
shixuns = stage.shixuns.includes(myshixuns: :games, homework_commons: :course) shixuns = stage.shixuns.includes(myshixuns: :games, homework_commons: :course)
shixuns.find_in_batches(batch_size: 1000) do |s| shixuns.find_in_batches(batch_size: 1000) do |s|
Parallel.each_with_index(s, in_processes: 2) do |shixun, index| s.each_with_index do |shixun, index|
stage = "#{position}-#{index+1}" stage = "#{position}-#{index+1}"
name = shixun.name name = shixun.name
myshixuns = shixun.myshixuns myshixuns = shixun.myshixuns

@ -12,7 +12,7 @@ namespace :subjects do
"school_used_count, created_at, updated_at" "school_used_count, created_at, updated_at"
subjects.find_in_batches(batch_size: 50) do |s| subjects.find_in_batches(batch_size: 50) do |s|
str = [] str = []
Parallel.each_with_index(s, in_processes: 4) do |subject, index| Parallel.each_with_index(s, in_processes: 6) do |subject, index|
puts("---------------------data_statistic: #{subject.id}") puts("---------------------data_statistic: #{subject.id}")
Rails.logger.info("---------------------data_statistic: #{subject.id}") Rails.logger.info("---------------------data_statistic: #{subject.id}")
data = Subjects::DataStatisticService.new(subject) data = Subjects::DataStatisticService.new(subject)
@ -80,12 +80,11 @@ namespace :subjects do
puts("---------------------shixun_info_statistic_begin") puts("---------------------shixun_info_statistic_begin")
Rails.logger.info("---------------------shixun_info_statistic_begin") Rails.logger.info("---------------------shixun_info_statistic_begin")
subjects = Subject.where(status: 2, hidden: 0) subjects = Subject.where(status: 2, hidden: 0)
str = ""
buffer_size = 0
column_value = "subject_id, shixun_id, stage, shixun_name, challenge_count, course_count, " + column_value = "subject_id, shixun_id, stage, shixun_name, challenge_count, course_count, " +
"school_count, used_count, passed_count, evaluate_count, passed_ave_time, created_at, updated_at" "school_count, used_count, passed_count, evaluate_count, passed_ave_time, created_at, updated_at"
subjects.find_in_batches(batch_size: 50) do |s| subjects.find_in_batches(batch_size: 50) do |s|
Parallel.each_with_index(s, in_processes: 4) do |subject| str = []
Parallel.each_with_index(s, in_processes: 6) do |subject|
puts("---------------------shixun_info_statistic: #{subject.id}") puts("---------------------shixun_info_statistic: #{subject.id}")
Rails.logger.info("---------------------shixun_info_statistic: #{subject.id}") Rails.logger.info("---------------------shixun_info_statistic: #{subject.id}")
data = Subjects::ShixunUsedInfoService.call(subject) data = Subjects::ShixunUsedInfoService.call(subject)
@ -96,21 +95,20 @@ namespace :subjects do
"#{key[:course_count]}, #{key[:school_count]}, #{key[:used_count]}, #{key[:passed_count]}, " + "#{key[:course_count]}, #{key[:school_count]}, #{key[:used_count]}, #{key[:passed_count]}, " +
"#{key[:evaluate_count]}, #{key[:passed_ave_time]}, " + "#{key[:evaluate_count]}, #{key[:passed_ave_time]}, " +
"'#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')") "'#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')")
buffer_size += 1 if str.size == 1000
if buffer_size == 1000 || key == data.last sql = "REPLACE INTO subject_shixun_infos(#{column_value}) VALUES #{str.uniq.join(",")}"
sql = "REPLACE INTO subject_shixun_infos(#{column_value}) VALUES #{str}" str_c = str
puts sql puts sql
ActiveRecord::Base.connection.execute sql ActiveRecord::Base.connection.execute sql
str = "" str -= str_c
buffer_size = 0
end end
end end
end end
end if str.size > 0
if buffer_size > 0 sql = "REPLACE INTO subject_shixun_infos(#{column_value}) VALUES #{str.uniq.join(",")}"
sql = "REPLACE INTO subject_shixun_infos(#{column_value}) VALUES #{str}" puts sql
puts sql ActiveRecord::Base.connection.execute sql
ActiveRecord::Base.connection.execute sql end
end end
puts("---------------------shixun_info_statistic_end") puts("---------------------shixun_info_statistic_end")
Rails.logger.info("---------------------shixun_info_statistic_end") Rails.logger.info("---------------------shixun_info_statistic_end")

Loading…
Cancel
Save