优化后台学校统计查询

dev_aliyun2
anke1460 5 years ago
parent 3b3b640f49
commit 027a78c692

@ -0,0 +1,18 @@
class StatisticStudentworkJob < ApplicationJob
queue_as :default
def perform(*args)
School.find_in_batches(batch_size: 50) do |school|
Parallel.each_with_index(school, in_processes: 5) do |s|
student_work = StudentWork.find_by_sql("SELECT count(*) AS count
FROM student_works
JOIN user_extensions AS u ON u.user_id = student_works.user_id AND u.school_id = #{s.id}")
report = SchoolReport.find_or_initialize_by(school_id: s.id)
report.school_name = s.name
report.student_work_count = student_work.first['count']
report.save
end
end
end
end

@ -4,8 +4,8 @@ class Admins::SchoolBaseStatisticService < ApplicationService
attr_reader :params
sort_columns :student_count, :teacher_count, :course_count, :course_group_count,
:attachment_count, :video_count, :normal_work_count, :shixun_work_count, :evaluate_count,
:student_work_count, :exercise_count, default_direction: :desc
:attachment_count, :video_count, :normal_work_count, :shixun_work_count, :shixun_evaluate_count,
:student_works_num, :exercise_count, default_direction: :desc
def initialize(params)
@params = params
@ -32,25 +32,23 @@ class Admins::SchoolBaseStatisticService < ApplicationService
def package_other_data(schools)
ids = schools.map(&:id)
student_count = CourseMember.course_students.joins(course: :school).group(:school_id).where("role= 'STUDENT' AND courses.is_delete = false AND schools.id in (?)", ids).count("distinct user_id")
student_count = UserExtension.where(school_id: ids, identity: :student).group(:school_id).count
teachers = UserExtension.where(school_id: ids, identity: :teacher).group(:school_id)
teacher_count = teachers.count
courses = Course.where(is_delete: 0, school_id: ids).group(:school_id)
course_count= courses.count
course_group_count = courses.joins(:course_groups).count
attachment_count = courses.joins(:attachments).count
video_count = teachers.joins(user: :videos).where("videos.delete_state IS NOT NULL").count
video_count = teachers.joins(user: :videos).where("videos.delete_state IS NULL").count
homeworks = HomeworkCommon.joins(:course).where(courses: { school_id: ids }).where("courses.is_delete = false")
homeworks = HomeworkCommon.joins("JOIN user_extensions ON homework_commons.user_id = user_extensions.user_id").where(user_extensions: {school_id: ids})
shixun_work_count = homeworks.where(homework_type: 4).group(:school_id).count
normal_work_count = homeworks.where(homework_type: 1).group(:school_id).count
student_work_count = homeworks.joins(:student_works).group(:school_id).count
evaluate_count = EvaluateRecord.unscoped.joins('JOIN homework_commons_shixuns hcs ON hcs.shixun_id = evaluate_records.shixun_id
JOIN homework_commons hc ON hcs.homework_common_id = hc.id AND hc.homework_type = 4
JOIN course_members ON course_members.user_id = evaluate_records.user_id
JOIN courses ON course_members.course_id = courses.id AND hc.course_id = courses.id')
.where(courses: { school_id: ids })
.group(:school_id).count
reports = SchoolReport.where(school_id: ids)
evaluate_count = reports.each_with_object({}) { |report, obj| obj[report.school_id] = report.shixun_evaluate_count }
student_work_count = reports.each_with_object({}) { |report, obj| obj[report.school_id] = report.student_work_count }
exercise_count = Exercise.joins(:course).where(courses: { school_id: ids }).group(:school_id).count
schools.map do |school|
@ -66,7 +64,7 @@ class Admins::SchoolBaseStatisticService < ApplicationService
normal_work_count: normal_work_count[school.id],
shixun_work_count: shixun_work_count[school.id],
student_work_count: student_work_count[school.id],
evaluate_count: evaluate_count[school.id],
shixun_evaluate_count: evaluate_count[school.id],
exercise_count: exercise_count[school.id]
}
end
@ -81,9 +79,8 @@ class Admins::SchoolBaseStatisticService < ApplicationService
schools.joins('LEFT JOIN user_extensions ue ON ue.school_id = schools.id AND ue.identity = 0')
.select("#{base_query_column}, COUNT(*) teacher_count")
when 'student_count' then
schools.joins("LEFT JOIN courses ue ON ue.school_id = schools.id AND ue.is_delete = FALSE
LEFT JOIN course_members ON course_members.course_id = ue.id AND course_members.role = 'STUDENT'")
.select("#{base_query_column}, COUNT(distinct user_id) student_count")
schools.joins('LEFT JOIN user_extensions ue ON ue.school_id = schools.id AND ue.identity = 1')
.select("#{base_query_column}, COUNT(*) student_count")
when 'course_count' then
schools.joins('LEFT JOIN courses ON courses.school_id = schools.id AND courses.is_delete = false')
.select("#{base_query_column}, COUNT(*) course_count")
@ -100,26 +97,19 @@ class Admins::SchoolBaseStatisticService < ApplicationService
LEFT JOIN videos ON videos.user_id = ue.user_id AND videos.delete_state IS NOT NULL")
.select("#{base_query_column}, COUNT(*) video_count")
when 'normal_work_count' then
schools.joins("LEFT JOIN courses ON courses.school_id = schools.id
LEFT JOIN homework_commons ON homework_commons.course_id = courses.id AND homework_commons.homework_type = 0")
schools.joins("LEFT JOIN user_extensions ON user_extensions.school_id = schools.id
LEFT JOIN homework_commons ON homework_commons.user_id = user_extensions.user_id AND homework_commons.homework_type = 0")
.select("#{base_query_column}, COUNT(*) normal_work_count")
when 'shixun_work_count' then
schools.joins("LEFT JOIN courses ON courses.school_id = schools.id
LEFT JOIN homework_commons ON homework_commons.course_id = courses.id AND homework_commons.homework_type = 4")
schools.joins("LEFT JOIN user_extensions ON user_extensions.school_id = schools.id
LEFT JOIN homework_commons ON homework_commons.user_id = user_extensions.user_id AND homework_commons.homework_type = 4")
.select("#{base_query_column}, COUNT(*) shixun_work_count")
when 'student_work_count' then
schools.joins("LEFT JOIN courses ON courses.school_id = schools.id
LEFT JOIN homework_commons ON homework_commons.course_id = courses.id
LEFT JOIN student_works ON student_works.homework_common_id = homework_commons.id")
schools.joins("LEFT JOIN school_reports ON school_reports.school_id = schools.id")
.select("#{base_query_column}, COUNT(*) student_work_count")
when 'evaluate_count' then
schools.joins('
LEFT JOIN courses ON courses.school_id = schools.id AND courses.is_delete = false
LEFT JOIN course_members ON course_members.course_id = courses.id
LEFT JOIN evaluate_records ON course_members.user_id = evaluate_records.user_id
LEFT JOIN homework_commons_shixuns hcs ON hcs.shixun_id = evaluate_records.shixun_id
LEFT JOIN homework_commons hc ON hcs.homework_common_id = hc.id AND hc.homework_type = 4')
.select("#{base_query_column}, COUNT(*) evaluate_count")
when 'shixun_evaluate_count' then
schools.joins('LEFT JOIN school_reports ON school_reports.school_id = schools.id')
.select("#{base_query_column}, COUNT(*) shixun_evaluate_count")
when 'exercise_count' then
schools.joins('LEFT JOIN courses cs ON cs.school_id = schools.id AND cs.is_delete = 0
LEFT JOIN exercises ON exercises.course_id = cs.id')

@ -12,7 +12,7 @@
<th width="8%"><%= sort_tag('普通作业', name: 'normal_work_count', path: admins_school_base_statistics_path) %></th>
<th width="8%"><%= sort_tag('实训作业', name: 'shixun_work_count', path: admins_school_base_statistics_path) %></th>
<th width="8%"><%= sort_tag('作业文件', name: 'student_work_count', path: admins_school_base_statistics_path) %></th>
<th width="8%"><%= sort_tag('评测次数', name: 'evaluate_count', path: admins_school_base_statistics_path) %></th>
<th width="8%"><%= sort_tag('评测次数', name: 'shixun_evaluate_count', path: admins_school_base_statistics_path) %></th>
<th width="8%"><%= sort_tag('在线试卷', name: 'exercise_count', path: admins_school_base_statistics_path) %></th>
</tr>
</thead>
@ -34,7 +34,7 @@
<td><%= statistic[:normal_work_count].to_i %></td>
<td><%= statistic[:shixun_work_count].to_i %></td>
<td><%= statistic[:student_work_count].to_i %></td>
<td><%= statistic[:evaluate_count].to_i %></td>
<td><%= statistic[:shixun_evaluate_count].to_i %></td>
<td><%= statistic[:exercise_count].to_i %></td>
</tr>

@ -0,0 +1,5 @@
class AddStatisticToSchoolReport < ActiveRecord::Migration[5.2]
def change
add_column :school_reports, :student_work_count, :integer, default: 0
end
end

@ -0,0 +1,7 @@
desc "同步学院或者单位学生作业数"
namespace :sync_student_work do
task outpus_count: :environment do
StatisticStudentworkJob.perform_later
end
end
Loading…
Cancel
Save