diff --git a/app/models/subject.rb b/app/models/subject.rb index 28c45e4ba..df9271e5f 100644 --- a/app/models/subject.rb +++ b/app/models/subject.rb @@ -25,6 +25,11 @@ class Subject < ApplicationRecord has_many :courses, -> { where("is_delete = 0").order("courses.created_at ASC") } has_many :laboratory_subjects, dependent: :destroy + # 学习统计 + has_one :subject_record, dependent: :destroy + has_many :subject_course_records, dependent: :destroy + has_many :subject_shixun_infos, dependent: :destroy + has_many :subject_user_infos, dependent: :destroy validates :name, length: { maximum: 60 } validates :description, length: { maximum: 8000 } diff --git a/app/models/subject_course_record.rb b/app/models/subject_course_record.rb new file mode 100644 index 000000000..2240c0eb3 --- /dev/null +++ b/app/models/subject_course_record.rb @@ -0,0 +1,3 @@ +class SubjectCourseRecord < ApplicationRecord + belongs_to :subject +end diff --git a/app/models/subject_record.rb b/app/models/subject_record.rb new file mode 100644 index 000000000..cdd5b15e4 --- /dev/null +++ b/app/models/subject_record.rb @@ -0,0 +1,3 @@ +class SubjectRecord < ApplicationRecord + belongs_to :subject +end diff --git a/app/models/subject_shixun_info.rb b/app/models/subject_shixun_info.rb new file mode 100644 index 000000000..6ce4ccb50 --- /dev/null +++ b/app/models/subject_shixun_info.rb @@ -0,0 +1,3 @@ +class SubjectShixunInfo < ApplicationRecord + belongs_to :subject +end diff --git a/app/models/subject_user_info.rb b/app/models/subject_user_info.rb new file mode 100644 index 000000000..4435a1aea --- /dev/null +++ b/app/models/subject_user_info.rb @@ -0,0 +1,3 @@ +class SubjectUserInfo < ApplicationRecord + belongs_to :subject +end diff --git a/app/services/subjects/course_used_info_service.rb b/app/services/subjects/course_used_info_service.rb index 5553c7222..01e0723eb 100644 --- a/app/services/subjects/course_used_info_service.rb +++ b/app/services/subjects/course_used_info_service.rb @@ -29,8 +29,8 @@ class Subjects::CourseUsedInfoService < ApplicationService .pluck("homework_commons.id") choice_shixun_num = homework_commons.uniq.size choice_shixun_frequency = homework_commons.size - {name: name, course_count: course_count, student_count: student_count, choice_shixun_num: choice_shixun_num, - choice_shixun_frequency: choice_shixun_frequency} + {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 # 默认按照选用实训的次数作为排序 course_info.sort{|x,y| y['choice_shixun_frequency'] <=> x['choice_shixun_frequency']} diff --git a/app/services/subjects/shixun_used_info_service.rb b/app/services/subjects/shixun_used_info_service.rb index f1c4a373d..62548b4ef 100644 --- a/app/services/subjects/shixun_used_info_service.rb +++ b/app/services/subjects/shixun_used_info_service.rb @@ -27,7 +27,7 @@ class Subjects::ShixunUsedInfoService < ApplicationService 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} + evaluate_count: evaluate_count, passed_ave_time: passed_ave_time, shixun_id: shixun.id} end shixun_infos end diff --git a/app/services/subjects/user_used_info_service.rb b/app/services/subjects/user_used_info_service.rb index 6e3738f42..3b1b969f0 100644 --- a/app/services/subjects/user_used_info_service.rb +++ b/app/services/subjects/user_used_info_service.rb @@ -15,10 +15,10 @@ class Subjects::UserUsedInfoService < ApplicationService 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 = "未完成" + 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 << {login: user.login, name: name, passed_myshixun_count: passed_myshixun_count, + 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 diff --git a/db/migrate/20200109062658_create_subject_records.rb b/db/migrate/20200109062658_create_subject_records.rb new file mode 100644 index 000000000..97741fd8c --- /dev/null +++ b/db/migrate/20200109062658_create_subject_records.rb @@ -0,0 +1,14 @@ +class CreateSubjectRecords < ActiveRecord::Migration[5.2] + def change + create_table :subject_records do |t| + t.references :subject, unique: true + t.integer :study_count, default: 0 + t.integer :course_study_count, default: 0 + t.integer :initiative_study, default: 0 + t.integer :passed_count, default: 0 + t.integer :course_used_count, default: 0 + t.integer :school_used_count, default: 0 + t.timestamps + end + end +end diff --git a/db/migrate/20200109070211_create_subject_course_records.rb b/db/migrate/20200109070211_create_subject_course_records.rb new file mode 100644 index 000000000..cd88e7e40 --- /dev/null +++ b/db/migrate/20200109070211_create_subject_course_records.rb @@ -0,0 +1,16 @@ +class CreateSubjectCourseRecords < ActiveRecord::Migration[5.2] + def change + create_table :subject_course_records do |t| + t.references :subject + t.references :school + t.string :school_name + t.integer :course_count, default: 0 + t.integer :student_count, default: 0 + t.integer :choice_shixun_num, default: 0 + t.integer :choice_shixun_frequency, default: 0 + t.timestamps + end + + add_index :subject_course_records, [:school_id, :subject_id], unique: true, name: "couse_and_school_index" + end +end diff --git a/db/migrate/20200109084427_create_subject_shixun_infos.rb b/db/migrate/20200109084427_create_subject_shixun_infos.rb new file mode 100644 index 000000000..8916b2129 --- /dev/null +++ b/db/migrate/20200109084427_create_subject_shixun_infos.rb @@ -0,0 +1,21 @@ +class CreateSubjectShixunInfos < ActiveRecord::Migration[5.2] + def change + create_table :subject_shixun_infos do |t| + t.references :subject + t.references :shixun + t.string :stage + t.string :shixun_name + t.integer :challenge_count, default: 0 + t.integer :course_count, default: 0 + t.integer :school_count, default: 0 + t.integer :used_count, default: 0 + t.integer :passed_count, default: 0 + t.integer :evaluate_count, default: 0 + t.integer :passed_ave_time, default: 0 + + t.timestamps + end + + add_index :subject_shixun_infos, [:shixun_id, :subject_id], unique: true + end +end diff --git a/db/migrate/20200109091016_create_subject_user_infos.rb b/db/migrate/20200109091016_create_subject_user_infos.rb new file mode 100644 index 000000000..7f2671a71 --- /dev/null +++ b/db/migrate/20200109091016_create_subject_user_infos.rb @@ -0,0 +1,18 @@ +class CreateSubjectUserInfos < ActiveRecord::Migration[5.2] + def change + create_table :subject_user_infos do |t| + t.references :user + t.references :subject + t.string :username + t.integer :passed_myshixun_count, default: 0 + t.integer :passed_games_count, default: 0 + t.integer :code_line_count, default: 0 + t.integer :evaluate_count, default: 0 + t.integer :cost_time, default: 0 + t.timestamps + end + + add_index :subject_user_infos, [:user_id, :subject_id], unique: true + + end +end diff --git a/lib/tasks/statistic_subject_info.rake b/lib/tasks/statistic_subject_info.rake new file mode 100644 index 000000000..dbce739f2 --- /dev/null +++ b/lib/tasks/statistic_subject_info.rake @@ -0,0 +1,103 @@ +desc "统计实践课程的学习统计数据" + +namespace :subjects do + task data_statistic: :environment do + puts("---------------------data_statistic_begin") + Rails.logger.info("---------------------data_statistic_begin") + subjects = Subject.where(status: 2) + subjects.find_each do |subject| + puts("---------------------data_statistic: #{subject.id}") + Rails.logger.info("---------------------data_statistic: #{subject.id}") + sr = SubjectRecord.find_or_create_by!(subject_id: subject.id) + data = Subjects::DataStatisticService.new(subject) + update_params = { + study_count: data.study_count, + course_study_count: data.course_study_count, + initiative_study: data.initiative_study, + passed_count: data.passed_count, + course_used_count: data.course_used_count, + school_used_count: data.school_used_count + } + sr.update_attributes!(update_params) + end + puts("---------------------data_statistic_end") + Rails.logger.info("---------------------data_statistic_end") + end + + task course_info_statistic: :environment do + puts("---------------------course_info_statistic_begin") + Rails.logger.info("---------------------course_info_statistic_begin") + subjects = Subject.where(status: 2) + subjects.find_each do |subject| + puts("---------------------course_info_statistic: #{subject.id}") + Rails.logger.info("---------------------course_info_statistic: #{subject.id}") + data = Subjects::DataStatisticService.call(subject) + data.each do |key| + scr = SubjectCourseRecord.find_or_create_by!(school_id: key[:school_id], subject_id: subject.id) + update_params = { + school_name: key[:school_name], + course_count: key[:course_count], + student_count: key[:student_count], + choice_shixun_num: key[:choice_shixun_num], + choice_shixun_frequency: key[:choice_shixun_frequency] + } + scr.update_attributes(update_params) + end + end + puts("---------------------course_info_statistic_end") + Rails.logger.info("---------------------course_info_statistic_end") + end + + task shixun_info_statistic: :environment do + puts("---------------------shixun_info_statistic_begin") + Rails.logger.info("---------------------shixun_info_statistic_begin") + subjects = Subject.where(status: 2) + subjects.find_each(batch_size: 100) do |subject| + data = Subjects::ShixunUsedInfoService.call(subject) + data.each do |key| + ssi = SubjectShixunInfo.find_or_create_by!(shixun_id: key[:shixun_id], subject_id: subject.id) + update_params = { + stage: key[:stage], + shixun_name: key[:name], + challenge_count: key[:challenge_count], + course_count: key[:course_count], + school_count: key[:school_count], + used_count: key[:used_count], + passed_count: key[:passed_count], + evaluate_count: key[:evaluate_count], + passed_ave_time: key[:passed_ave_time] + } + ssi.update_attributes(update_params) + end + end + puts("---------------------shixun_info_statistic_end") + Rails.logger.info("---------------------shixun_info_statistic_end") + end + + task user_info_statistic: :environment do + puts("---------------------user_info_statistic_begin") + Rails.logger.info("---------------------user_info_statistic_begin") + subjects = Subject.where(status: 2) + subjects.find_each(batch_size: 100) do |subject| + puts("---------------------user_info_statistic: #{subject.id}") + data = Subjects::UserUsedInfoService.call(subject) + data.each do |key| + sui = SubjectUserInfo.find_or_create_by!(user_id: key[:user_id], subject_id: subject.id) + update_params = { + username: key[:name], + passed_myshixun_count: key[:passed_myshixun_count], + passed_games_count: key[:passed_games_count], + code_line_count: key[:code_line_count], + evaluate_count: key[:evaluate_count], + cost_time: key[:cost_time] + } + sui.update_attributes(update_params) + end + end + puts("---------------------user_info_statistic_end") + Rails.logger.info("---------------------user_info_statistic_end") + end + + + + end diff --git a/spec/models/subject_course_record_spec.rb b/spec/models/subject_course_record_spec.rb new file mode 100644 index 000000000..bf2daffc8 --- /dev/null +++ b/spec/models/subject_course_record_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe SubjectCourseRecord, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/subject_record_spec.rb b/spec/models/subject_record_spec.rb new file mode 100644 index 000000000..6dd9f7b4d --- /dev/null +++ b/spec/models/subject_record_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe SubjectRecord, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/subject_shixun_info_spec.rb b/spec/models/subject_shixun_info_spec.rb new file mode 100644 index 000000000..83d4b4bf9 --- /dev/null +++ b/spec/models/subject_shixun_info_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe SubjectShixunInfo, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/subject_user_info_spec.rb b/spec/models/subject_user_info_spec.rb new file mode 100644 index 000000000..60c8a76e6 --- /dev/null +++ b/spec/models/subject_user_info_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe SubjectUserInfo, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end