diff --git a/app/services/schools/school_statistic_service.rb b/app/services/schools/school_statistic_service.rb index a5082108d..5d8a30141 100644 --- a/app/services/schools/school_statistic_service.rb +++ b/app/services/schools/school_statistic_service.rb @@ -2,7 +2,7 @@ class Schools::SchoolStatisticService < ApplicationService attr_reader :school def initialize(school) - @school = school.includes(:courses, user_extensions: :user) + @school = school @user_extensions = school.user_extensions end @@ -26,14 +26,14 @@ class Schools::SchoolStatisticService < ApplicationService @user_extensions.map{|ue| ue.user.last_login_on&.between?(1.weeks.ago, Time.now)}.size end - # 活跃用户(近3个月有登录) + # 活跃用户(近1个月有登录) def acitve_user_1_months_count - @user_extensions.map{|ue| ue.user.last_login_on&.between?(1.months.ago, Time.now)}.size + @user_extensions.joins(:user).map{|ue| ue.user.last_login_on&.between?(1.months.ago, Time.now)}.size end # 活跃用户(近3个月有登录) def acitve_user_3_months_count - @user_extensions.map{|ue| ue.user.last_login_on&.between?(3.months.ago, Time.now)}.size + @user_extensions.map{|ue| ue.user&.last_login_on&.between?(3.months.ago, Time.now)}.size end # 活跃用户(进半年有登录记录) @@ -52,10 +52,15 @@ class Schools::SchoolStatisticService < ApplicationService end # 实训作业数目 - def hom_shixuns_count + def homw_shixuns_count @school.courses.joins(:homework_commons).where(homework_commons: {homework_type: 'practice'}).size end + # 其他类型作业 + def homw_other_count + @school.courses.joins(:homework_commons).where.not(homework_commons: {homework_type: 'practice'}).size + end + # 资源数 def sources_count @school.courses.joins(:attachments).size @@ -67,7 +72,7 @@ class Schools::SchoolStatisticService < ApplicationService end # 制作实训数 - def shixun_count + def shixuns_count @user_extensions.joins(user: :shixuns).size end @@ -83,17 +88,17 @@ class Schools::SchoolStatisticService < ApplicationService # 挑战的关卡数 def games_count - @user_extensions.joins("join games on games.user_id = user_extensions.user_id").where(games: {status: 0..2}) + @user_extensions.joins("join games on games.user_id = user_extensions.user_id").where(games: {status: 0..2}).size end # 通关的关卡数 def pass_games_count - @user_extensions.joins("join games on games.user_id = user_extensions.user_id").where(games: {status: 2}) + @user_extensions.joins("join games on games.user_id = user_extensions.user_id").where(games: {status: 2}).size end # 评测总数 - def evalute_count - @user_extensions.joins("join games on games.user_id = user_extensions.user_id").sum(:evalute_count) + def evaluate_count + @user_extensions.joins("join games on games.user_id = user_extensions.user_id").sum(:evaluate_count).to_i end diff --git a/db/migrate/20200306072044_add_index_for_sta_all.rb b/db/migrate/20200306072044_add_index_for_sta_all.rb new file mode 100644 index 000000000..f4e62c8a9 --- /dev/null +++ b/db/migrate/20200306072044_add_index_for_sta_all.rb @@ -0,0 +1,5 @@ +class AddIndexForStaAll < ActiveRecord::Migration[5.2] + def change + add_index :sta_alls, :school_id, :unique => true + end +end diff --git a/lib/tasks/school_statistic.rake b/lib/tasks/school_statistic.rake deleted file mode 100644 index 3d017ad17..000000000 --- a/lib/tasks/school_statistic.rake +++ /dev/null @@ -1,8 +0,0 @@ -#coding=utf-8 - -desc "同步高校数据" -namespace :school_statistic do - task sync_records: :environment do - - end -end diff --git a/lib/tasks/static_all.rake b/lib/tasks/static_all.rake index 4c878841e..01ecffcff 100644 --- a/lib/tasks/static_all.rake +++ b/lib/tasks/static_all.rake @@ -2,15 +2,23 @@ desc "统计每个学校使用数据" namespace :static_all do task :repo => :environment do - School.find_each(batch_size: 100) do |school| - User.joins(:user_extension).where(school_id: school.id) - - - report = StaAll.find_or_initialize_by(school_id: school.id) - - report.shixun_evaluate_count = evaluate_count - - report.save + school_alls = School.includes(:courses, user_extensions: :user).all + school_alls.find_in_batches(batch_size: 50) do |schools| + Parallel.each_with_index(schools, in_processes: 5) do |school, index| + puts("school_id: #{school.id}") + data = Schools::SchoolStatisticService.new(school) + sta_all = StaAll.find_or_initialize_by(school_id: school.id) + attrs = {tea_count: data.teacher_count, stu_count: data.student_count, courses_count: data.courses_count, + active_users_count: data.acitve_user_3_months_count, curr_courses_count: data.curr_courses_count, + homw_shixuns_count: data.homw_shixuns_count, homw_other_count: data.homw_other_count, + sources_count: data.sources_count, videos_count: data.videos_count, shixuns_count: data.shixuns_count, + myshixuns_count: data.myshixuns_count, mys_passed_count: data.pass_myshixun_count, + games_count: data.games_count, games_passed_count: data.pass_games_count, build_count: data.evaluate_count} + puts "sta_all: #{attrs}" + sta_all.assign_attributes(attrs) + sta_all.save! + end end + end end \ No newline at end of file