diff --git a/app/controllers/subjects_controller.rb b/app/controllers/subjects_controller.rb index 7df9aae89..eb12c4f0d 100644 --- a/app/controllers/subjects_controller.rb +++ b/app/controllers/subjects_controller.rb @@ -456,6 +456,25 @@ class SubjectsController < ApplicationController end end + def statistics_new + # data = Subjects::DataStatisticService.new(@subject) + # Rails.logger.info("study_count: #{data.study_count}") + # Rails.logger.info("course_study_count: #{ data.course_study_count}") + # Rails.logger.info("passed_count: #{data.passed_count}") + # Rails.logger.info("course_used_count: #{data.course_used_count}") + # Rails.logger.info("school_used_count: #{data.school_used_count}") + + # data_1 = Subjects::CourseUsedInfoService.call(@subject) + # Rails.logger.info("study_count: #{data_1}") + + # data_2 = Subjects::ShixunUsedInfoService.call(@subject) + # Rails.logger.info("study_count: #{data_2}") + + data_3 = Subjects::UserUsedInfoService.call(@subject) + Rails.logger.info("study_count: #{data_3}") + render_ok() + end + def shixun_report end diff --git a/app/models/myshixun.rb b/app/models/myshixun.rb index 54d068be6..8d2012381 100644 --- a/app/models/myshixun.rb +++ b/app/models/myshixun.rb @@ -5,7 +5,7 @@ class Myshixun < ApplicationRecord has_one :shixun_modify, :dependent => :destroy belongs_to :user - belongs_to :user_extension + belongs_to :user_extension, foreign_key: :user_id belongs_to :shixun, counter_cache: true has_one :last_executable_task, -> { where(status: [0, 1]).reorder(created_at: :asc) }, class_name: 'Game' @@ -22,7 +22,7 @@ class Myshixun < ApplicationRecord end def output_times - games.pluck(:evaluate_count).sum.to_i + games.map(&:evaluate_count).sum.to_i end def repo_path diff --git a/app/services/subjects/course_used_info_service.rb b/app/services/subjects/course_used_info_service.rb index f60c4a6ca..5553c7222 100644 --- a/app/services/subjects/course_used_info_service.rb +++ b/app/services/subjects/course_used_info_service.rb @@ -13,7 +13,7 @@ class Subjects::CourseUsedInfoService < ApplicationService .where(homework_commons_shixuns: {shixun_id: @shixun_ids}) course_ids = homework_commons.pluck(:course_id) homework_common_ids = homework_commons.pluck("homework_commons.id") - schools = School.joins(courses: [:course_members, :homework_commons]) + schools = School.joins(:courses).where(courses: {id: course_ids}).select("schools.*, count(courses.id) course_count") # name:将该课程使用到课堂的单位 # course_count: 将该课程使用到课堂的数量 @@ -23,11 +23,11 @@ class Subjects::CourseUsedInfoService < ApplicationService course_info = schools.map do |school| name = school.name - course_count = school.where(courses: {id: course_ids}).count - student_count = school.where(course_members: {role: 4}).select("course_members.user_id").distinct.size - homework_commons = school.where(homework_commons: {id: homework_common_ids}) - .select("homework_commons.id") - choice_shixun_num = homework_commons.distinct.size + course_count = school.course_count + student_count = school.courses.joins(:course_members).where(course_members: {role: 4, course_id: course_ids}) + homework_commons = school.courses.joins(:homework_commons).where(homework_commons: {id: homework_common_ids}) + .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} diff --git a/app/services/subjects/data_statistic_service.rb b/app/services/subjects/data_statistic_service.rb index 9dc2db09a..25a83aa26 100644 --- a/app/services/subjects/data_statistic_service.rb +++ b/app/services/subjects/data_statistic_service.rb @@ -10,7 +10,7 @@ class Subjects::DataStatisticService < ApplicationService # 文案解释:学习该课程的全部人数(学习总人数=课堂学习人数+自主学习人数) # 开发备注:只要点击该课程的任何一个实训(生成了tpi),都算一个学习人数;(去重,一个人数计算1次) def study_count - @shixuns.joins(:myshixuns).select("myshixuns.user_id").distinct.size + @shixuns.joins(:myshixuns).pluck("myshixuns.user_id").uniq.size end # 课堂学习人数: @@ -18,7 +18,7 @@ class Subjects::DataStatisticService < ApplicationService # 开发备注:只要通过课堂进入,并点击了实训(生成了tpi),则算一个可以学习人数;(去重,一个人数计算1次) def course_study_count # 实训作业 - sw_user_ids = StudentWork.where.not(myshixun_id: nil).joins(homework_common: :homework_commons_shixuns) + sw_user_ids = StudentWork.where.not(myshixun_id: nil).joins(homework_common: :homework_commons_shixun) .where(homework_commons_shixuns: {shixun_id: @shixun_ids}).pluck("student_works.user_id") # 试卷的实训题 esa_user_ids = ExerciseShixunAnswer.joins(exercise_shixun_challenge: :shixun) @@ -37,7 +37,7 @@ class Subjects::DataStatisticService < ApplicationService # 文案解释: # 通关该课程所有实训的人数(去重。一个人数计算1次) def passed_count - @shixuns.includes(:myshixuns).where(myshixuns: {status: 1}).select("myshixuns.user_id").distinct.size + @shixuns.includes(:myshixuns).where(myshixuns: {status: 1}).pluck("myshixuns.user_id").uniq.size end # 使用课堂数: @@ -65,7 +65,7 @@ class Subjects::DataStatisticService < ApplicationService private def school_ids - @shixuns.joins(myshixuns: :user_extension).select("user_extensions.school_id").distinct + @shixuns.joins(myshixuns: :user_extension).pluck("user_extensions.school_id").uniq end diff --git a/app/services/subjects/shixun_used_info_service.rb b/app/services/subjects/shixun_used_info_service.rb index 1dda483d4..f1c4a373d 100644 --- a/app/services/subjects/shixun_used_info_service.rb +++ b/app/services/subjects/shixun_used_info_service.rb @@ -17,15 +17,22 @@ class Subjects::ShixunUsedInfoService < ApplicationService school_count = shixun.homework_commons_shixuns.select{|hcs| hcs.homework_common.course.school_id}.uniq.size used_count = shixun.myshixuns_count passed_count = shixun.myshixuns.select{|m| m.status == 1}.size - evaluate_count = shixun.myshixuns.map{|m| m.games.sum(:evaluate_count).to_i}.sum - passed_ave_time = (passed_count > 0) ? - (shixun.myshixuns.map{|m| m.status == 1 && m.games.sum(:cost_time).to_i}.sum / passed_count) : 0 + evaluate_count = shixun.myshixuns.map{|m| m.output_times }.sum + passed_ave_time = passed_count > 0 ? shixun.myshixuns.map{|m| m.total_cost_time}.sum : 0 + #evaluate_count = shixun.myshixuns.joins(:games).select("SUM(games.evaluate_count) evaluate_count").first.evaluate_count + # passed_ave_time = (passed_count > 0) ? + # (shixun.myshixuns.joins(:games) + # .where(myshixuns: {status: 1}) + # .select("SUM(games.cost_time) cost_time").first.cost_time / passed_count) : 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} end shixun_infos end + + end end diff --git a/app/services/subjects/user_used_info_service.rb b/app/services/subjects/user_used_info_service.rb index 4d5f54f9e..6e3738f42 100644 --- a/app/services/subjects/user_used_info_service.rb +++ b/app/services/subjects/user_used_info_service.rb @@ -1,4 +1,4 @@ -class Subject::UserUsedInfoService < ApplicationService +class Subjects::UserUsedInfoService < ApplicationService attr_reader :subject, :shixun_ids def initialize(subject) @@ -8,21 +8,19 @@ class Subject::UserUsedInfoService < ApplicationService end def call - users = User.joins(:myshixuns).where(myshixuns: {shixun_id: shixun_ids}).group(:user_id) - users_info = [] - users.includes(:myshixuns, :games).each do |user| - games = user.games - name = "#{user.lastname}#{user.firstname}" - passed_myshixun_count = user.myshixuns.select{|m| m.status == 1}.size - passed_games_count = games.select{|g| g.status == 2}.size + users = User.includes(myshixuns: :games).where(myshixuns: {shixun_id: shixun_ids}, games: {status: 2}) + users.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 = "未完成" - evaluate_count = games.sum(evaluate_count) - cost_time = games.sum(:cost_time) - user_login = user.login - users_info << {login: user_login, name: name, passed_myshixun_count: passed_myshixun_count, + 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, passed_games_count: passed_games_count, code_line_count: code_line_count, - evaluate_count: evaluate_count, cost_time: cost_time} + evaluate_count: evaluate_count, cost_time: cost_time} end users_info end diff --git a/config/routes.rb b/config/routes.rb index a65a7f4c1..39e9aca18 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -394,6 +394,7 @@ Rails.application.routes.draw do get 'cancel_publish' get 'cancel_has_publish' get 'statistics' + get 'statistics_new' get 'shixun_report' get 'school_report' post 'update_attr'