|
|
|
@ -5,40 +5,14 @@ namespace :subjects do
|
|
|
|
|
puts("---------------------data_statistic_begin")
|
|
|
|
|
Rails.logger.info("---------------------data_statistic_begin")
|
|
|
|
|
subjects = Subject.where(status: 2, hidden: 0)
|
|
|
|
|
if ENV['subject_id'].present?
|
|
|
|
|
subjects = subjects.where(id:ENV['subject_id'])
|
|
|
|
|
end
|
|
|
|
|
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|
|
|
|
|
|
# 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}")
|
|
|
|
|
# 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 "####str: #{str}"
|
|
|
|
|
# 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
|
|
|
|
|
# end
|
|
|
|
|
# end
|
|
|
|
|
# end
|
|
|
|
|
#
|
|
|
|
|
# end
|
|
|
|
|
subjects.find_in_batches(batch_size: 50) do |s|
|
|
|
|
|
str = []
|
|
|
|
|
Parallel.each_with_index(s, in_threads: 4) do |subject, index|
|
|
|
|
|
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)
|
|
|
|
@ -67,38 +41,37 @@ namespace :subjects do
|
|
|
|
|
puts("---------------------course_info_statistic_begin")
|
|
|
|
|
Rails.logger.info("---------------------course_info_statistic_begin")
|
|
|
|
|
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, " +
|
|
|
|
|
"choice_shixun_frequency, created_at, updated_at"
|
|
|
|
|
|
|
|
|
|
if ENV['subject_id'].present?
|
|
|
|
|
subjects = subjects.where(id:ENV['subject_id'])
|
|
|
|
|
end
|
|
|
|
|
subjects.find_in_batches(batch_size: 50) do |s|
|
|
|
|
|
Parallel.each(in_processes: 4) do |subject|
|
|
|
|
|
str = []
|
|
|
|
|
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)
|
|
|
|
|
data.each do |key|
|
|
|
|
|
next if key[:school_id].nil?
|
|
|
|
|
str += ", " unless str.empty?
|
|
|
|
|
str += ("(#{subject.id}, #{key[:school_id]}, '#{key[:school_name]}', #{key[:course_count]}, " +
|
|
|
|
|
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 || key == data.last
|
|
|
|
|
sql = "REPLACE INTO subject_course_records(#{column_value}) VALUES #{str}"
|
|
|
|
|
if str.size == 1000
|
|
|
|
|
sql = "REPLACE INTO subject_course_records(#{column_value}) VALUES #{str.uniq.join(",")}"
|
|
|
|
|
str_c = str
|
|
|
|
|
puts sql
|
|
|
|
|
ActiveRecord::Base.connection.execute sql
|
|
|
|
|
str = ""
|
|
|
|
|
buffer_size = 0
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
str -= str_c
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
if buffer_size > 0
|
|
|
|
|
sql = "REPLACE INTO subject_course_records(#{column_value}) VALUES #{str}"
|
|
|
|
|
if str.size > 0
|
|
|
|
|
sql = "REPLACE INTO subject_course_records(#{column_value}) VALUES #{str.uniq.join(",")}"
|
|
|
|
|
puts sql
|
|
|
|
|
ActiveRecord::Base.connection.execute sql
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
puts("---------------------course_info_statistic_end")
|
|
|
|
|
Rails.logger.info("---------------------course_info_statistic_end")
|
|
|
|
|
end
|
|
|
|
|