From 637f0da6fe48489d210f3e10dc1b9cca91fc5bff Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Wed, 15 Jan 2020 13:43:42 +0800 Subject: [PATCH 01/18] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/statistic_subject_info.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tasks/statistic_subject_info.rake b/lib/tasks/statistic_subject_info.rake index 68d75293d..4c1ca58a0 100644 --- a/lib/tasks/statistic_subject_info.rake +++ b/lib/tasks/statistic_subject_info.rake @@ -117,7 +117,7 @@ namespace :subjects do task user_info_statistic: :environment do puts("---------------------user_info_statistic_begin") Rails.logger.info("---------------------user_info_statistic_begin") - subjects = Subject.where(status: 2) + subjects = Subject.where(status: 2, id: 303) str = "" buffer_size = 0 column_value = "user_id, subject_id, username, passed_myshixun_count, passed_games_count, " + From 85f50da302820983909a4b85a84b548aa35ae904 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Wed, 15 Jan 2020 14:16:53 +0800 Subject: [PATCH 02/18] =?UTF-8?q?=E8=BD=AC=E4=B9=89=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/subjects/user_used_info_service.rb | 2 +- lib/tasks/statistic_subject_info.rake | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/services/subjects/user_used_info_service.rb b/app/services/subjects/user_used_info_service.rb index 52d6be005..e4442a2f4 100644 --- a/app/services/subjects/user_used_info_service.rb +++ b/app/services/subjects/user_used_info_service.rb @@ -9,7 +9,7 @@ class Subjects::UserUsedInfoService < ApplicationService def call users_info = [] - users = User.includes(myshixuns: :games).where(myshixuns: {shixun_id: shixun_ids}, games: {status: 2}) + users = User.includes(myshixuns: :games).where(myshixuns: {shixun_id: shixun_ids}, games: {status: 2}, users: {is_test: false}) users.find_each do |user| myshixuns = user.myshixuns.select{|m| shixun_ids.include?(m.shixun_id)} name = "#{user.lastname}#{user.firstname}" diff --git a/lib/tasks/statistic_subject_info.rake b/lib/tasks/statistic_subject_info.rake index 4c1ca58a0..475cb0e8c 100644 --- a/lib/tasks/statistic_subject_info.rake +++ b/lib/tasks/statistic_subject_info.rake @@ -128,7 +128,7 @@ namespace :subjects do data.each do |key| next if key[:user_id].nil? str += ", " unless str.empty? - str += ("(#{key[:user_id]}, #{subject.id}, '#{key[:name]}', '#{key[:passed_myshixun_count]}', " + + str += ("(#{key[:user_id]}, #{subject.id}, ''#{key[:name].gsub(/'/, '"')}', #{key[:passed_myshixun_count]}, " + "#{key[:passed_games_count]}, #{key[:code_line_count]}, #{key[:evaluate_count]}, #{key[:cost_time]}, " + "'#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')") buffer_size += 1 From 747bad462e4b2d4b52a21e11064b6a4687ff7999 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Wed, 15 Jan 2020 14:19:43 +0800 Subject: [PATCH 03/18] 1 --- lib/tasks/statistic_subject_info.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tasks/statistic_subject_info.rake b/lib/tasks/statistic_subject_info.rake index 475cb0e8c..9a6ec041b 100644 --- a/lib/tasks/statistic_subject_info.rake +++ b/lib/tasks/statistic_subject_info.rake @@ -128,7 +128,7 @@ namespace :subjects do data.each do |key| next if key[:user_id].nil? str += ", " unless str.empty? - str += ("(#{key[:user_id]}, #{subject.id}, ''#{key[:name].gsub(/'/, '"')}', #{key[:passed_myshixun_count]}, " + + str += ("(#{key[:user_id]}, #{subject.id}, '#{key[:name].gsub(/'/, '"')}', #{key[:passed_myshixun_count]}, " + "#{key[:passed_games_count]}, #{key[:code_line_count]}, #{key[:evaluate_count]}, #{key[:cost_time]}, " + "'#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')") buffer_size += 1 From b857a71af9e5cea56e67cfe3d4f59703b2dcddf3 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Wed, 15 Jan 2020 14:24:08 +0800 Subject: [PATCH 04/18] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BB=9F=E8=AE=A1?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/statistic_subject_info.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tasks/statistic_subject_info.rake b/lib/tasks/statistic_subject_info.rake index 9a6ec041b..180766cd7 100644 --- a/lib/tasks/statistic_subject_info.rake +++ b/lib/tasks/statistic_subject_info.rake @@ -117,7 +117,7 @@ namespace :subjects do task user_info_statistic: :environment do puts("---------------------user_info_statistic_begin") Rails.logger.info("---------------------user_info_statistic_begin") - subjects = Subject.where(status: 2, id: 303) + subjects = Subject.where(status: 2) str = "" buffer_size = 0 column_value = "user_id, subject_id, username, passed_myshixun_count, passed_games_count, " + From 5a3345be470341434b245ed991b872fa78a21d92 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Wed, 15 Jan 2020 15:02:51 +0800 Subject: [PATCH 05/18] =?UTF-8?q?=E6=8F=90=E9=AB=98=E5=B9=B6=E8=A1=8C?= =?UTF-8?q?=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 2 ++ .../subjects/course_used_info_service.rb | 24 +++++++++-------- .../subjects/user_used_info_service.rb | 27 +++++++++++-------- lib/tasks/statistic_subject_info.rake | 4 +-- 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/Gemfile b/Gemfile index 3bfe246a4..859090feb 100644 --- a/Gemfile +++ b/Gemfile @@ -109,3 +109,5 @@ gem 'request_store' # 敏感词汇 gem 'harmonious_dictionary', '~> 0.0.1' + +gem 'parallel', '~> 1.19', '>= 1.19.1' diff --git a/app/services/subjects/course_used_info_service.rb b/app/services/subjects/course_used_info_service.rb index 2561f3aa4..5ae5abe1c 100644 --- a/app/services/subjects/course_used_info_service.rb +++ b/app/services/subjects/course_used_info_service.rb @@ -21,17 +21,19 @@ class Subjects::CourseUsedInfoService < ApplicationService # choice_shixun_num: 选用该课程实训的个数(去重) # choice_shixun_frequency: 选用该课程实训的次数 course_info = [] - schools.map do |school| - name = school.name - course_count = school.course_count - student_count = school.courses.joins(:course_members).where(course_members: {role: 4, course_id: course_ids}).size - shixun_ids = school.courses.joins(homework_commons: :homework_commons_shixun) - .where(homework_commons: {id: homework_common_ids}) - .pluck("homework_commons_shixuns.shixun_id") - choice_shixun_frequency = shixun_ids.size - choice_shixun_num = shixun_ids.uniq.size - course_info << {school_id: school.id, school_name: name, course_count: course_count, student_count: student_count, - choice_shixun_num: choice_shixun_num, choice_shixun_frequency: choice_shixun_frequency} + schools.find_in_batches do |s| + Parallel.each(s) do |school| + name = school.name + course_count = school.course_count + student_count = school.courses.joins(:course_members).where(course_members: {role: 4, course_id: course_ids}).size + shixun_ids = school.courses.joins(homework_commons: :homework_commons_shixun) + .where(homework_commons: {id: homework_common_ids}) + .pluck("homework_commons_shixuns.shixun_id") + choice_shixun_frequency = shixun_ids.size + choice_shixun_num = shixun_ids.uniq.size + course_info << {school_id: school.id, school_name: name, course_count: course_count, student_count: student_count, + choice_shixun_num: choice_shixun_num, choice_shixun_frequency: choice_shixun_frequency} + end end course_info end diff --git a/app/services/subjects/user_used_info_service.rb b/app/services/subjects/user_used_info_service.rb index e4442a2f4..82fc5316b 100644 --- a/app/services/subjects/user_used_info_service.rb +++ b/app/services/subjects/user_used_info_service.rb @@ -1,6 +1,8 @@ class Subjects::UserUsedInfoService < ApplicationService + require 'parallel' attr_reader :subject, :shixun_ids + def initialize(subject) @subject = subject @shixun_ids = subject.shixuns.pluck(:id) @@ -10,18 +12,21 @@ class Subjects::UserUsedInfoService < ApplicationService def call users_info = [] users = User.includes(myshixuns: :games).where(myshixuns: {shixun_id: shixun_ids}, games: {status: 2}, users: {is_test: false}) - users.find_each do |user| - myshixuns = user.myshixuns.select{|m| shixun_ids.include?(m.shixun_id)} - name = "#{user.lastname}#{user.firstname}" - passed_myshixun_count = myshixuns.select{|m| m.status == 1}.size - passed_games_count = myshixuns.map{|m| m.games.select{|g| g.status == 2}.size }.size - code_line_count = 0 - evaluate_count = myshixuns.map{|m| m.output_times }.sum - cost_time = myshixuns.map{|m|m.total_cost_time }.sum - users_info << {user_id: user.id, name: name, passed_myshixun_count: passed_myshixun_count, - passed_games_count: passed_games_count, code_line_count: code_line_count, - evaluate_count: evaluate_count, cost_time: cost_time} + users.find_in_batches do |u| + Parallel.each(u, in_processes: 5) do |user| + myshixuns = user.myshixuns.select{|m| shixun_ids.include?(m.shixun_id)} + name = "#{user.lastname}#{user.firstname}" + passed_myshixun_count = myshixuns.select{|m| m.status == 1}.size + passed_games_count = myshixuns.map{|m| m.games.select{|g| g.status == 2}.size }.size + code_line_count = 0 + evaluate_count = myshixuns.map{|m| m.output_times }.sum + cost_time = myshixuns.map{|m|m.total_cost_time }.sum + users_info << {user_id: user.id, name: name, passed_myshixun_count: passed_myshixun_count, + passed_games_count: passed_games_count, code_line_count: code_line_count, + evaluate_count: evaluate_count, cost_time: cost_time} + end end + users_info end diff --git a/lib/tasks/statistic_subject_info.rake b/lib/tasks/statistic_subject_info.rake index 180766cd7..30516e28f 100644 --- a/lib/tasks/statistic_subject_info.rake +++ b/lib/tasks/statistic_subject_info.rake @@ -51,7 +51,7 @@ namespace :subjects do puts("---------------------course_info_statistic: #{subject.id}") Rails.logger.info("---------------------course_info_statistic: #{subject.id}") data = Subjects::CourseUsedInfoService.call(subject) - data.each do |key| + Parallel.map(data) do |key| next if key[:school_id].nil? str += ", " unless str.empty? str += ("(#{subject.id}, #{key[:school_id]}, '#{key[:school_name]}', #{key[:course_count]}, " + @@ -125,7 +125,7 @@ namespace :subjects do subjects.find_each(batch_size: 50) do |subject| puts("---------------------user_info_statistic: #{subject.id}") data = Subjects::UserUsedInfoService.call(subject) - data.each do |key| + Parallel.each(data) do |key| next if key[:user_id].nil? str += ", " unless str.empty? str += ("(#{key[:user_id]}, #{subject.id}, '#{key[:name].gsub(/'/, '"')}', #{key[:passed_myshixun_count]}, " + From cf7b74f33625cbd878c9d3b94cb451ee0aae407d Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Wed, 15 Jan 2020 15:07:42 +0800 Subject: [PATCH 06/18] =?UTF-8?q?=E6=8F=90=E9=AB=98=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/subjects/user_used_info_service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/subjects/user_used_info_service.rb b/app/services/subjects/user_used_info_service.rb index 82fc5316b..3aa341722 100644 --- a/app/services/subjects/user_used_info_service.rb +++ b/app/services/subjects/user_used_info_service.rb @@ -13,7 +13,7 @@ class Subjects::UserUsedInfoService < ApplicationService users_info = [] users = User.includes(myshixuns: :games).where(myshixuns: {shixun_id: shixun_ids}, games: {status: 2}, users: {is_test: false}) users.find_in_batches do |u| - Parallel.each(u, in_processes: 5) do |user| + Parallel.each(u, in_threads: 10) do |user| myshixuns = user.myshixuns.select{|m| shixun_ids.include?(m.shixun_id)} name = "#{user.lastname}#{user.firstname}" passed_myshixun_count = myshixuns.select{|m| m.status == 1}.size From e4d1e53dcfaa9a02e69f03feb0f3a4b16ff7ddf4 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Wed, 15 Jan 2020 15:12:05 +0800 Subject: [PATCH 07/18] =?UTF-8?q?=E8=BF=9B=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/subjects/user_used_info_service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/subjects/user_used_info_service.rb b/app/services/subjects/user_used_info_service.rb index 3aa341722..82fc5316b 100644 --- a/app/services/subjects/user_used_info_service.rb +++ b/app/services/subjects/user_used_info_service.rb @@ -13,7 +13,7 @@ class Subjects::UserUsedInfoService < ApplicationService users_info = [] users = User.includes(myshixuns: :games).where(myshixuns: {shixun_id: shixun_ids}, games: {status: 2}, users: {is_test: false}) users.find_in_batches do |u| - Parallel.each(u, in_threads: 10) do |user| + Parallel.each(u, in_processes: 5) do |user| myshixuns = user.myshixuns.select{|m| shixun_ids.include?(m.shixun_id)} name = "#{user.lastname}#{user.firstname}" passed_myshixun_count = myshixuns.select{|m| m.status == 1}.size From 8112e0ca6ee4d343bb856ffb6c963277d712ef49 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Wed, 15 Jan 2020 15:14:20 +0800 Subject: [PATCH 08/18] =?UTF-8?q?=E5=B9=B6=E8=A1=8C=E8=B7=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/statistic_subject_info.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tasks/statistic_subject_info.rake b/lib/tasks/statistic_subject_info.rake index 30516e28f..253cac85a 100644 --- a/lib/tasks/statistic_subject_info.rake +++ b/lib/tasks/statistic_subject_info.rake @@ -125,7 +125,7 @@ namespace :subjects do subjects.find_each(batch_size: 50) do |subject| puts("---------------------user_info_statistic: #{subject.id}") data = Subjects::UserUsedInfoService.call(subject) - Parallel.each(data) do |key| + Parallel.each(data, in_processes: 5) do |key| next if key[:user_id].nil? str += ", " unless str.empty? str += ("(#{key[:user_id]}, #{subject.id}, '#{key[:name].gsub(/'/, '"')}', #{key[:passed_myshixun_count]}, " + From fdba605467be3644e880a1d7cfd742dc52e970ea Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Wed, 15 Jan 2020 15:43:11 +0800 Subject: [PATCH 09/18] =?UTF-8?q?=E8=AF=BE=E7=A8=8B=E7=BB=9F=E8=AE=A1?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/subjects/user_used_info_service.rb | 4 ++-- lib/tasks/statistic_subject_info.rake | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/services/subjects/user_used_info_service.rb b/app/services/subjects/user_used_info_service.rb index 82fc5316b..c736b39b9 100644 --- a/app/services/subjects/user_used_info_service.rb +++ b/app/services/subjects/user_used_info_service.rb @@ -12,8 +12,8 @@ class Subjects::UserUsedInfoService < ApplicationService def call users_info = [] users = User.includes(myshixuns: :games).where(myshixuns: {shixun_id: shixun_ids}, games: {status: 2}, users: {is_test: false}) - users.find_in_batches do |u| - Parallel.each(u, in_processes: 5) do |user| + users.find_in_batches(batch_size: 500) do |u| + Parallel.each(u, in_processes: 10) do |user| myshixuns = user.myshixuns.select{|m| shixun_ids.include?(m.shixun_id)} name = "#{user.lastname}#{user.firstname}" passed_myshixun_count = myshixuns.select{|m| m.status == 1}.size diff --git a/lib/tasks/statistic_subject_info.rake b/lib/tasks/statistic_subject_info.rake index 253cac85a..bec200181 100644 --- a/lib/tasks/statistic_subject_info.rake +++ b/lib/tasks/statistic_subject_info.rake @@ -125,7 +125,7 @@ namespace :subjects do subjects.find_each(batch_size: 50) do |subject| puts("---------------------user_info_statistic: #{subject.id}") data = Subjects::UserUsedInfoService.call(subject) - Parallel.each(data, in_processes: 5) do |key| + Parallel.each(data, in_processes: 10) do |key| next if key[:user_id].nil? str += ", " unless str.empty? str += ("(#{key[:user_id]}, #{subject.id}, '#{key[:name].gsub(/'/, '"')}', #{key[:passed_myshixun_count]}, " + From 535f77ba7764072c3df4da6193122bb2c57352d2 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Wed, 15 Jan 2020 15:55:59 +0800 Subject: [PATCH 10/18] =?UTF-8?q?=E9=9A=90=E8=97=8F=E7=9A=84=E4=B8=8D?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/statistic_subject_info.rake | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/tasks/statistic_subject_info.rake b/lib/tasks/statistic_subject_info.rake index bec200181..92f5b1683 100644 --- a/lib/tasks/statistic_subject_info.rake +++ b/lib/tasks/statistic_subject_info.rake @@ -4,7 +4,7 @@ namespace :subjects do task data_statistic: :environment do puts("---------------------data_statistic_begin") Rails.logger.info("---------------------data_statistic_begin") - subjects = Subject.where(status: 2) + subjects = Subject.where(status: 2, hidden: 0) str = "" buffer_size = 0 column_value = "subject_id, study_count, course_study_count, initiative_study, passed_count, course_used_count, " + @@ -42,7 +42,7 @@ namespace :subjects do task course_info_statistic: :environment do puts("---------------------course_info_statistic_begin") Rails.logger.info("---------------------course_info_statistic_begin") - subjects = Subject.where(status: 2) + subjects = Subject.where(status: 2, hidden: 0) str = "" buffer_size = 0 column_value = "subject_id, school_id, school_name, course_count, student_count, choice_shixun_num, " + @@ -79,7 +79,7 @@ namespace :subjects do task shixun_info_statistic: :environment do puts("---------------------shixun_info_statistic_begin") Rails.logger.info("---------------------shixun_info_statistic_begin") - subjects = Subject.where(status: 2) + subjects = Subject.where(status: 2, hidden: 0) str = "" buffer_size = 0 column_value = "subject_id, shixun_id, stage, shixun_name, challenge_count, course_count, " + @@ -117,7 +117,7 @@ namespace :subjects do task user_info_statistic: :environment do puts("---------------------user_info_statistic_begin") Rails.logger.info("---------------------user_info_statistic_begin") - subjects = Subject.where(status: 2) + subjects = Subject.where(status: 2, hidden: 0) str = "" buffer_size = 0 column_value = "user_id, subject_id, username, passed_myshixun_count, passed_games_count, " + From 2e7d1c111d44323711608a62258cdc710a2e6044 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Wed, 15 Jan 2020 16:03:20 +0800 Subject: [PATCH 11/18] =?UTF-8?q?=E8=BF=90=E8=A1=8C=E9=80=9F=E5=BA=A6?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/statistic_subject_info.rake | 33 +++++++++++++++------------ 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/lib/tasks/statistic_subject_info.rake b/lib/tasks/statistic_subject_info.rake index 92f5b1683..4f7fc2991 100644 --- a/lib/tasks/statistic_subject_info.rake +++ b/lib/tasks/statistic_subject_info.rake @@ -122,21 +122,24 @@ namespace :subjects do buffer_size = 0 column_value = "user_id, subject_id, username, passed_myshixun_count, passed_games_count, " + "code_line_count, evaluate_count, cost_time, created_at, updated_at" - subjects.find_each(batch_size: 50) do |subject| - puts("---------------------user_info_statistic: #{subject.id}") - data = Subjects::UserUsedInfoService.call(subject) - Parallel.each(data, in_processes: 10) do |key| - next if key[:user_id].nil? - str += ", " unless str.empty? - str += ("(#{key[:user_id]}, #{subject.id}, '#{key[:name].gsub(/'/, '"')}', #{key[:passed_myshixun_count]}, " + - "#{key[:passed_games_count]}, #{key[:code_line_count]}, #{key[:evaluate_count]}, #{key[:cost_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 - sql = "REPLACE INTO subject_user_infos(#{column_value}) VALUES #{str}" - ActiveRecord::Base.connection.execute sql - str = "" - buffer_size = 0 + + subjects.find_in_batches(batch_size: 50) do |s| + Parallel.each(s, in_processes: 10) do |subject| + puts("---------------------user_info_statistic: #{subject.id}") + data = Subjects::UserUsedInfoService.call(subject) + Parallel.each(data, in_processes: 10) do |key| + next if key[:user_id].nil? + str += ", " unless str.empty? + str += ("(#{key[:user_id]}, #{subject.id}, '#{key[:name].gsub(/'/, '"')}', #{key[:passed_myshixun_count]}, " + + "#{key[:passed_games_count]}, #{key[:code_line_count]}, #{key[:evaluate_count]}, #{key[:cost_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 + sql = "REPLACE INTO subject_user_infos(#{column_value}) VALUES #{str}" + ActiveRecord::Base.connection.execute sql + str = "" + buffer_size = 0 + end end end end From 61002b67780eed2b227ca2e69356202da03520d8 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Wed, 15 Jan 2020 16:13:39 +0800 Subject: [PATCH 12/18] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/statistic_subject_info.rake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/tasks/statistic_subject_info.rake b/lib/tasks/statistic_subject_info.rake index 4f7fc2991..c9f0361fc 100644 --- a/lib/tasks/statistic_subject_info.rake +++ b/lib/tasks/statistic_subject_info.rake @@ -124,10 +124,10 @@ namespace :subjects do "code_line_count, evaluate_count, cost_time, created_at, updated_at" subjects.find_in_batches(batch_size: 50) do |s| - Parallel.each(s, in_processes: 10) do |subject| + Parallel.each(s, in_processes: 5) do |subject| puts("---------------------user_info_statistic: #{subject.id}") data = Subjects::UserUsedInfoService.call(subject) - Parallel.each(data, in_processes: 10) do |key| + data.each do |key| next if key[:user_id].nil? str += ", " unless str.empty? str += ("(#{key[:user_id]}, #{subject.id}, '#{key[:name].gsub(/'/, '"')}', #{key[:passed_myshixun_count]}, " + From 0bac3203880a40dd8befdd7dbfdfa069cf64e88e Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Wed, 15 Jan 2020 16:22:47 +0800 Subject: [PATCH 13/18] 1 --- app/services/subjects/user_used_info_service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/subjects/user_used_info_service.rb b/app/services/subjects/user_used_info_service.rb index c736b39b9..12c7f6ab7 100644 --- a/app/services/subjects/user_used_info_service.rb +++ b/app/services/subjects/user_used_info_service.rb @@ -13,7 +13,7 @@ class Subjects::UserUsedInfoService < ApplicationService users_info = [] users = User.includes(myshixuns: :games).where(myshixuns: {shixun_id: shixun_ids}, games: {status: 2}, users: {is_test: false}) users.find_in_batches(batch_size: 500) do |u| - Parallel.each(u, in_processes: 10) do |user| + Parallel.each(u, in_processes: 5) do |user| myshixuns = user.myshixuns.select{|m| shixun_ids.include?(m.shixun_id)} name = "#{user.lastname}#{user.firstname}" passed_myshixun_count = myshixuns.select{|m| m.status == 1}.size From d91153f5f31f3b7aae431c4c6e76ee10b963c0c7 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Wed, 15 Jan 2020 16:36:19 +0800 Subject: [PATCH 14/18] =?UTF-8?q?=E9=A2=98=E5=BA=93=E7=9A=84=E5=85=B3?= =?UTF-8?q?=E8=81=94=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/item_banks_controller.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/controllers/item_banks_controller.rb b/app/controllers/item_banks_controller.rb index a0abffe6c..86b1523c3 100644 --- a/app/controllers/item_banks_controller.rb +++ b/app/controllers/item_banks_controller.rb @@ -41,6 +41,9 @@ class ItemBanksController < ApplicationController def destroy ActiveRecord::Base.transaction do ApplyAction.where(container_type: "ItemBank", container_id: @item.id).destroy_all + if @item.item_type == "PROGRAM" + @item.container&.destroy! + end @item.destroy! render_ok end From b5e5432873c787e39a98fe6a31f90b7957aa0ee2 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Wed, 15 Jan 2020 16:42:29 +0800 Subject: [PATCH 15/18] =?UTF-8?q?=E5=86=85=E5=AD=98=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/subjects/user_used_info_service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/subjects/user_used_info_service.rb b/app/services/subjects/user_used_info_service.rb index 12c7f6ab7..9b76e3473 100644 --- a/app/services/subjects/user_used_info_service.rb +++ b/app/services/subjects/user_used_info_service.rb @@ -13,7 +13,7 @@ class Subjects::UserUsedInfoService < ApplicationService users_info = [] users = User.includes(myshixuns: :games).where(myshixuns: {shixun_id: shixun_ids}, games: {status: 2}, users: {is_test: false}) users.find_in_batches(batch_size: 500) do |u| - Parallel.each(u, in_processes: 5) do |user| + Parallel.each(u, in_processes: 3) do |user| myshixuns = user.myshixuns.select{|m| shixun_ids.include?(m.shixun_id)} name = "#{user.lastname}#{user.firstname}" passed_myshixun_count = myshixuns.select{|m| m.status == 1}.size From b29c2d8198273de06b52dd56bdd49be8c43cddd2 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Wed, 15 Jan 2020 17:04:22 +0800 Subject: [PATCH 16/18] dead parallel --- app/services/subjects/user_used_info_service.rb | 2 +- lib/tasks/statistic_subject_info.rake | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/services/subjects/user_used_info_service.rb b/app/services/subjects/user_used_info_service.rb index 9b76e3473..78dbfe8e9 100644 --- a/app/services/subjects/user_used_info_service.rb +++ b/app/services/subjects/user_used_info_service.rb @@ -13,7 +13,7 @@ class Subjects::UserUsedInfoService < ApplicationService users_info = [] users = User.includes(myshixuns: :games).where(myshixuns: {shixun_id: shixun_ids}, games: {status: 2}, users: {is_test: false}) users.find_in_batches(batch_size: 500) do |u| - Parallel.each(u, in_processes: 3) do |user| + Parallel.each(u, in_processes: 2) do |user| myshixuns = user.myshixuns.select{|m| shixun_ids.include?(m.shixun_id)} name = "#{user.lastname}#{user.firstname}" passed_myshixun_count = myshixuns.select{|m| m.status == 1}.size diff --git a/lib/tasks/statistic_subject_info.rake b/lib/tasks/statistic_subject_info.rake index c9f0361fc..f982cfc12 100644 --- a/lib/tasks/statistic_subject_info.rake +++ b/lib/tasks/statistic_subject_info.rake @@ -124,7 +124,7 @@ namespace :subjects do "code_line_count, evaluate_count, cost_time, created_at, updated_at" subjects.find_in_batches(batch_size: 50) do |s| - Parallel.each(s, in_processes: 5) do |subject| + Parallel.each(s, in_processes: 4) do |subject| puts("---------------------user_info_statistic: #{subject.id}") data = Subjects::UserUsedInfoService.call(subject) data.each do |key| From ed258312ae4ed378e74fc0ac1ff8e4c2e706a43e Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Wed, 15 Jan 2020 17:49:08 +0800 Subject: [PATCH 17/18] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subjects/shixun_used_info_service.rb | 31 ++--- lib/tasks/statistic_subject_info.rake | 115 ++++++++++-------- 2 files changed, 78 insertions(+), 68 deletions(-) diff --git a/app/services/subjects/shixun_used_info_service.rb b/app/services/subjects/shixun_used_info_service.rb index dfd8485e0..5fa99a120 100644 --- a/app/services/subjects/shixun_used_info_service.rb +++ b/app/services/subjects/shixun_used_info_service.rb @@ -10,20 +10,23 @@ class Subjects::ShixunUsedInfoService < ApplicationService stages.each do |stage| position = stage.position shixuns = stage.shixuns.includes(myshixuns: :games, homework_commons: :course) - shixuns.each_with_index do |shixun, index| - stage = "#{position}-#{index+1}" - name = shixun.name - myshixuns = shixun.myshixuns - challenge_count = shixun.challenges_count - course_count = shixun.homework_commons.map{|hc| hc.course_id}.uniq.size - school_count = shixun.homework_commons.map{|hc| hc.course&.school_id}.uniq.size - used_count = shixun.myshixuns_count - passed_count = myshixuns.select{|m| m.status == 1}.size - evaluate_count = myshixuns.map{|m| m.output_times }.sum - passed_ave_time = passed_count > 0 ? myshixuns.map{|m| m.total_cost_time}.sum : 0 - shixun_infos << {stage: stage, name: name, challenge_count: challenge_count, course_count: course_count, - school_count: school_count, used_count: used_count, passed_count: passed_count, - evaluate_count: evaluate_count, passed_ave_time: passed_ave_time, shixun_id: shixun.id} + shixuns.find_in_batches(batch_size: 1000) do |s| + Parallel.each_with_index(s, in_processes: 2) do |shixun, index| + stage = "#{position}-#{index+1}" + name = shixun.name + myshixuns = shixun.myshixuns + challenge_count = shixun.challenges_count + course_count = shixun.homework_commons.map{|hc| hc.course_id}.uniq.size + school_count = shixun.homework_commons.map{|hc| hc.course&.school_id}.uniq.size + used_count = shixun.myshixuns_count + passed_count = myshixuns.select{|m| m.status == 1}.size + evaluate_count = myshixuns.map{|m| m.output_times }.sum + passed_ave_time = passed_count > 0 ? myshixuns.map{|m| m.total_cost_time}.sum : 0 + shixun_infos << {stage: stage, name: name, challenge_count: challenge_count, course_count: course_count, + school_count: school_count, used_count: used_count, passed_count: passed_count, + evaluate_count: evaluate_count, passed_ave_time: passed_ave_time, shixun_id: shixun.id} + + end end end shixun_infos diff --git a/lib/tasks/statistic_subject_info.rake b/lib/tasks/statistic_subject_info.rake index f982cfc12..d42cb1b1c 100644 --- a/lib/tasks/statistic_subject_info.rake +++ b/lib/tasks/statistic_subject_info.rake @@ -9,25 +9,27 @@ namespace :subjects do buffer_size = 0 column_value = "subject_id, study_count, course_study_count, initiative_study, passed_count, course_used_count, " + "school_used_count, created_at, updated_at" - subjects.find_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 += ", " unless str.empty? - 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')}')") - buffer_size += 1 - if buffer_size == 1000 - sql = "REPLACE INTO subject_records(#{column_value}) VALUES #{str}" - puts sql - ActiveRecord::Base.connection.execute sql - str = "" - buffer_size = 0 + subjects.find_in_batches(batch_size: 50) do |s| + Parallel.each(s, in_processes: 4) 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 += ", " unless str.empty? + 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')}')") + buffer_size += 1 + if buffer_size == 1000 + sql = "REPLACE INTO subject_records(#{column_value}) VALUES #{str}" + puts sql + ActiveRecord::Base.connection.execute sql + str = "" + buffer_size = 0 + end end end if buffer_size > 0 @@ -47,23 +49,26 @@ namespace :subjects do buffer_size = 0 column_value = "subject_id, school_id, school_name, course_count, student_count, choice_shixun_num, " + "choice_shixun_frequency, created_at, updated_at" - subjects.find_each do |subject| - puts("---------------------course_info_statistic: #{subject.id}") - Rails.logger.info("---------------------course_info_statistic: #{subject.id}") - data = Subjects::CourseUsedInfoService.call(subject) - Parallel.map(data) do |key| - next if key[:school_id].nil? - str += ", " unless str.empty? - str += ("(#{subject.id}, #{key[:school_id]}, '#{key[:school_name]}', #{key[:course_count]}, " + - "#{key[:student_count]}, #{key[:choice_shixun_num]}, #{key[:choice_shixun_frequency]}, " + - "'#{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 - sql = "REPLACE INTO subject_course_records(#{column_value}) VALUES #{str}" - puts sql - ActiveRecord::Base.connection.execute sql - str = "" - buffer_size = 0 + + subjects.find_in_batches(batch_size: 50) do |s| + Parallel.each(s, in_processes: 4) do |subject| + puts("---------------------course_info_statistic: #{subject.id}") + Rails.logger.info("---------------------course_info_statistic: #{subject.id}") + data = Subjects::CourseUsedInfoService.call(subject) + Parallel.map(data) do |key| + next if key[:school_id].nil? + str += ", " unless str.empty? + str += ("(#{subject.id}, #{key[:school_id]}, '#{key[:school_name]}', #{key[:course_count]}, " + + "#{key[:student_count]}, #{key[:choice_shixun_num]}, #{key[:choice_shixun_frequency]}, " + + "'#{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 + sql = "REPLACE INTO subject_course_records(#{column_value}) VALUES #{str}" + puts sql + ActiveRecord::Base.connection.execute sql + str = "" + buffer_size = 0 + end end end end @@ -84,24 +89,26 @@ namespace :subjects do 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_each(batch_size: 100) do |subject| - puts("---------------------shixun_info_statistic: #{subject.id}") - Rails.logger.info("---------------------shixun_info_statistic: #{subject.id}") - data = Subjects::ShixunUsedInfoService.call(subject) - data.each do |key| - next if key[:shixun_id].nil? - str += ", " unless str.empty? - str += ("(#{subject.id}, #{key[:shixun_id]}, '#{key[:stage]}', '#{key[:name]}', #{key[:challenge_count]}, " + - "#{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 - sql = "REPLACE INTO subject_shixun_infos(#{column_value}) VALUES #{str}" - puts sql - ActiveRecord::Base.connection.execute sql - str = "" - buffer_size = 0 + subjects.find_each(batch_size: 50) do |s| + Parallel.each(s, in_processes: 4) do |subject| + puts("---------------------shixun_info_statistic: #{subject.id}") + Rails.logger.info("---------------------shixun_info_statistic: #{subject.id}") + data = Subjects::ShixunUsedInfoService.call(subject) + data.each do |key| + next if key[:shixun_id].nil? + str += ", " unless str.empty? + str += ("(#{subject.id}, #{key[:shixun_id]}, '#{key[:stage]}', '#{key[:name]}', #{key[:challenge_count]}, " + + "#{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 + sql = "REPLACE INTO subject_shixun_infos(#{column_value}) VALUES #{str}" + puts sql + ActiveRecord::Base.connection.execute sql + str = "" + buffer_size = 0 + end end end end From 96bc31e9d6f5d0e947ab7e854545af95ab1a52dc Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Wed, 15 Jan 2020 17:54:23 +0800 Subject: [PATCH 18/18] =?UTF-8?q?=E5=B9=B6=E8=A1=8C=E5=A4=84=E7=90=86?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/statistic_subject_info.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tasks/statistic_subject_info.rake b/lib/tasks/statistic_subject_info.rake index d42cb1b1c..259824174 100644 --- a/lib/tasks/statistic_subject_info.rake +++ b/lib/tasks/statistic_subject_info.rake @@ -89,7 +89,7 @@ namespace :subjects do 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_each(batch_size: 50) do |s| + subjects.find_in_batches(batch_size: 50) do |s| Parallel.each(s, in_processes: 4) do |subject| puts("---------------------shixun_info_statistic: #{subject.id}") Rails.logger.info("---------------------shixun_info_statistic: #{subject.id}")