From 662ffbbea133cbfc8d505a7c2ff64b924a524c83 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Thu, 13 Feb 2020 18:38:01 +0800 Subject: [PATCH] 1 --- .../subjects/shixun_used_info_service.rb | 2 +- lib/tasks/statistic_subject_info.rake | 26 +++++++++---------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/app/services/subjects/shixun_used_info_service.rb b/app/services/subjects/shixun_used_info_service.rb index 5fa99a120..bd145e622 100644 --- a/app/services/subjects/shixun_used_info_service.rb +++ b/app/services/subjects/shixun_used_info_service.rb @@ -11,7 +11,7 @@ class Subjects::ShixunUsedInfoService < ApplicationService position = stage.position shixuns = stage.shixuns.includes(myshixuns: :games, homework_commons: :course) 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}" name = shixun.name myshixuns = shixun.myshixuns diff --git a/lib/tasks/statistic_subject_info.rake b/lib/tasks/statistic_subject_info.rake index cf9d7c32e..ca390868b 100644 --- a/lib/tasks/statistic_subject_info.rake +++ b/lib/tasks/statistic_subject_info.rake @@ -12,7 +12,7 @@ namespace :subjects do "school_used_count, created_at, updated_at" subjects.find_in_batches(batch_size: 50) do |s| 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}") Rails.logger.info("---------------------data_statistic: #{subject.id}") data = Subjects::DataStatisticService.new(subject) @@ -80,12 +80,11 @@ namespace :subjects do puts("---------------------shixun_info_statistic_begin") Rails.logger.info("---------------------shixun_info_statistic_begin") subjects = Subject.where(status: 2, hidden: 0) - str = "" - buffer_size = 0 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" 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}") Rails.logger.info("---------------------shixun_info_statistic: #{subject.id}") 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[:evaluate_count]}, #{key[:passed_ave_time]}, " + "'#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')") - buffer_size += 1 - if buffer_size == 1000 || key == data.last - sql = "REPLACE INTO subject_shixun_infos(#{column_value}) VALUES #{str}" + if str.size == 1000 + sql = "REPLACE INTO subject_shixun_infos(#{column_value}) VALUES #{str.uniq.join(",")}" + str_c = str puts sql ActiveRecord::Base.connection.execute sql - str = "" - buffer_size = 0 + str -= str_c end end end - end - if buffer_size > 0 - sql = "REPLACE INTO subject_shixun_infos(#{column_value}) VALUES #{str}" - puts sql - ActiveRecord::Base.connection.execute sql + if str.size > 0 + sql = "REPLACE INTO subject_shixun_infos(#{column_value}) VALUES #{str.uniq.join(",")}" + puts sql + ActiveRecord::Base.connection.execute sql + end end puts("---------------------shixun_info_statistic_end") Rails.logger.info("---------------------shixun_info_statistic_end")