课程数据统计

dev_jupyter
daiao 5 years ago
parent 74f44671f5
commit 04f8acca4f

@ -1,7 +1,7 @@
class CreateSubjectRecords < ActiveRecord::Migration[5.2] class CreateSubjectRecords < ActiveRecord::Migration[5.2]
def change def change
create_table :subject_records do |t| create_table :subject_records do |t|
t.references :subject, unique: true t.references :subject
t.integer :study_count, default: 0 t.integer :study_count, default: 0
t.integer :course_study_count, default: 0 t.integer :course_study_count, default: 0
t.integer :initiative_study, default: 0 t.integer :initiative_study, default: 0

@ -0,0 +1,6 @@
class AddIndexForSubjectRecords < ActiveRecord::Migration[5.2]
def change
remove_index :subject_records, :subject_id
add_index :subject_records, :subject_id, unique: true
end
end

@ -1,35 +1,40 @@
desc "统计实践课程的学习统计数据" desc "统计实践课程的学习统计数据"
namespace :subjects do namespace :subjects do
buffer_size = 1000
task data_statistic: :environment do task data_statistic: :environment do
puts("---------------------data_statistic_begin") puts("---------------------data_statistic_begin")
Rails.logger.info("---------------------data_statistic_begin") Rails.logger.info("---------------------data_statistic_begin")
subjects = Subject.where(status: 2) subjects = Subject.where(status: 2)
str = ""
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| subjects.find_each do |subject|
puts("---------------------data_statistic: #{subject.id}") puts("---------------------data_statistic: #{subject.id}")
Rails.logger.info("---------------------data_statistic: #{subject.id}") Rails.logger.info("---------------------data_statistic: #{subject.id}")
sr = SubjectRecord.find_or_create_by!(subject_id: subject.id) #sr = SubjectRecord.find_or_create_by!(subject_id: subject.id)
data = Subjects::DataStatisticService.new(subject) data = Subjects::DataStatisticService.new(subject)
study_count = data.study_count study_count = data.study_count
# 总人数没有变化的话,不同课堂之类的变化了 next if study_count == 0
course_study_count = (study_count == sr.study_count ? sr.course_study_count : data.course_study_count) course_study_count = data.course_study_count
passed_count = (study_count == sr.study_count ? sr.passed_count : data.passed_count) initiative_study = study_count - course_study_count
course_used_count = (study_count == sr.study_count ? sr.course_used_count : data.course_used_count) str += ", " unless str.empty?
school_used_count = (study_count == sr.study_count ? sr.school_used_count : data.school_used_count) str += ("(#{subject.id}, #{study_count}, #{course_study_count}, #{initiative_study}, " +
unless course_study_count == sr.course_study_count && "#{data.passed_count}, #{data.course_used_count}, #{data.school_used_count}, " +
passed_count == sr.passed_count && "'#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')")
course_used_count == sr.course_used_count && buffer_size += 1
school_used_count == sr.school_used_count if buffer_size == 1000
update_params = { sql = "REPLACE INTO subject_records(#{column_value}) VALUES #{str}"
study_count: study_count, puts sql
course_study_count: course_study_count, ActiveRecord::Base.connection.execute sql
initiative_study: (study_count - course_study_count), str = ""
passed_count: passed_count, buffer_size = 0
course_used_count: course_used_count, end
school_used_count: school_used_count end
} if buffer_size > 0
sr.update_attributes!(update_params) sql = "REPLACE INTO subject_records(#{column_value}) VALUES #{str}"
end puts sql
ActiveRecord::Base.connection.execute sql
end end
puts("---------------------data_statistic_end") puts("---------------------data_statistic_end")
Rails.logger.info("---------------------data_statistic_end") Rails.logger.info("---------------------data_statistic_end")

Loading…
Cancel
Save