add shixun homework and shixun evaluate count to school daily report

pre_develop
p31729568 6 years ago
parent 15a2f16453
commit ec8b047228

@ -4,7 +4,7 @@ class Management::SchoolDataContrastService
PAGE_SIZE = 20 PAGE_SIZE = 20
CONTRAST_COLUMN_LIST = %w( CONTRAST_COLUMN_LIST = %w(
teacher_increase_count student_increase_count course_increase_count teacher_increase_count student_increase_count course_increase_count
shixun_increase_count active_user_count shixun_increase_count active_user_count shixun_homework_count shixun_evaluate_count
).freeze ).freeze
attr_reader :params, :sort_direction, :contrast_column attr_reader :params, :sort_direction, :contrast_column

@ -7,6 +7,7 @@ class Management::SchoolDataGrowService
sort_columns :teacher_increase_count, :student_increase_count, sort_columns :teacher_increase_count, :student_increase_count,
:course_increase_count, :shixun_increase_count, :active_user_count, :course_increase_count, :shixun_increase_count, :active_user_count,
:shixun_homework_count, :shixun_evaluate_count,
default_by: :teacher_increase_count, default_direction: :desc default_by: :teacher_increase_count, default_direction: :desc
def initialize(params) def initialize(params)
@ -24,6 +25,8 @@ class Management::SchoolDataGrowService
'SUM(student_increase_count) student_increase_count,'\ 'SUM(student_increase_count) student_increase_count,'\
'SUM(course_increase_count) course_increase_count,'\ 'SUM(course_increase_count) course_increase_count,'\
'SUM(shixun_increase_count) shixun_increase_count,'\ 'SUM(shixun_increase_count) shixun_increase_count,'\
'SUM(shixun_homework_count) shixun_homework_count,'\
'SUM(shixun_evaluate_count) shixun_evaluate_count,'\
'SUM(active_user_count) active_user_count' 'SUM(active_user_count) active_user_count'
) )
@ -40,6 +43,8 @@ class Management::SchoolDataGrowService
'SUM(student_increase_count) student_increase_count,'\ 'SUM(student_increase_count) student_increase_count,'\
'SUM(course_increase_count) course_increase_count,'\ 'SUM(course_increase_count) course_increase_count,'\
'SUM(shixun_increase_count) shixun_increase_count,'\ 'SUM(shixun_increase_count) shixun_increase_count,'\
'SUM(shixun_homework_count) shixun_homework_count,'\
'SUM(shixun_evaluate_count) shixun_evaluate_count,'\
'SUM(active_user_count) active_user_count' 'SUM(active_user_count) active_user_count'
).first ).first
end end

@ -16,14 +16,26 @@ class StatisticSchoolDailyReportTask
# 新增实训 # 新增实训
shixun_count = Shixun.joins(creator: :user_extensions) shixun_count = Shixun.joins(creator: :user_extensions)
.where('user_extensions.school_id = ?', school.id) .where(user_extensions: { identity: User::TEACHER, school_id: school.id })
.where(created_at: yesterday).count .where(created_at: yesterday).count
# 新增实训作业数
shixun_homework_count = HomeworkCommon.joins(:course).where(courses: { school_id: school.id })
.where(homework_type: 4, created_at: yesterday).count
# 新增实训评测数量
shixun_evaluate_count = Output.joins(game: { challenge: { shixun: { creator: :user_extensions } } })
.where(user_extensions: { identity: User::TEACHER, school_id: school.id })
.where(created_at: yesterday).count
# 无有效数据时不记录 # 无有效数据时不记录
next if [teacher_count, student_count, course_count, shixun_count, active_user_count].all?(&:zero?) data = [teacher_count, student_count, course_count, shixun_count, active_user_count,
shixun_homework_count, shixun_evaluate_count]
next if data.all?(&:zero?)
create_params = { create_params = {
school_id: school.id, school_name: school.name, teacher_increase_count: teacher_count, school_id: school.id, school_name: school.name, teacher_increase_count: teacher_count,
student_increase_count: student_count, course_increase_count: course_count, student_increase_count: student_count, course_increase_count: course_count,
shixun_homework_count: shixun_homework_count, shixun_evaluate_count: shixun_evaluate_count,
shixun_increase_count: shixun_count, active_user_count: active_user_count, date: current_date shixun_increase_count: shixun_count, active_user_count: active_user_count, date: current_date
} }
SchoolDailyReport.create!(create_params) SchoolDailyReport.create!(create_params)

@ -9,7 +9,9 @@
新增教师<span class="color-red"><%= @grow_summary.teacher_increase_count || 0 %></span>人, 新增教师<span class="color-red"><%= @grow_summary.teacher_increase_count || 0 %></span>人,
新增学生<span class="color-red"><%= @grow_summary.student_increase_count || 0 %></span>人, 新增学生<span class="color-red"><%= @grow_summary.student_increase_count || 0 %></span>人,
新增课堂<span class="color-red"><%= @grow_summary.course_increase_count || 0 %></span>个, 新增课堂<span class="color-red"><%= @grow_summary.course_increase_count || 0 %></span>个,
新增实训作业<span class="color-red"><%= @grow_summary.shixun_increase_count || 0 %></span>个, 新增实训<span class="color-red"><%= @grow_summary.shixun_increase_count || 0 %></span>个,
新增实训作业<span class="color-red"><%= @grow_summary.shixun_homework_count || 0 %></span>个,
新增实训评测<span class="color-red"><%= @grow_summary.shixun_evaluate_count || 0 %></span>个,
活跃用户<span class="color-red"><%= @grow_summary.active_user_count || 0 %></span>个 活跃用户<span class="color-red"><%= @grow_summary.active_user_count || 0 %></span>个
</div> </div>
<table class="edu-pop-table edu-txt-center" cellpadding="0" cellspacing="0" style="table-layout: fixed"> <table class="edu-pop-table edu-txt-center" cellpadding="0" cellspacing="0" style="table-layout: fixed">
@ -22,7 +24,9 @@
<th width="12%"><%= sort_tag('新增教师', name: 'teacher_increase_count', path: school_data_grow_managements_path) %></th> <th width="12%"><%= sort_tag('新增教师', name: 'teacher_increase_count', path: school_data_grow_managements_path) %></th>
<th width="12%"><%= sort_tag('新增学生', name: 'student_increase_count', path: school_data_grow_managements_path) %></th> <th width="12%"><%= sort_tag('新增学生', name: 'student_increase_count', path: school_data_grow_managements_path) %></th>
<th width="12%"><%= sort_tag('新增课堂', name: 'course_increase_count', path: school_data_grow_managements_path) %></th> <th width="12%"><%= sort_tag('新增课堂', name: 'course_increase_count', path: school_data_grow_managements_path) %></th>
<th width="12%"><%= sort_tag('新增实训作业', name: 'shixun_increase_count', path: school_data_grow_managements_path) %></th> <th width="12%"><%= sort_tag('新增实训', name: 'shixun_increase_count', path: school_data_grow_managements_path) %></th>
<th width="12%"><%= sort_tag('新增实训作业', name: 'shixun_homework_count', path: school_data_grow_managements_path) %></th>
<th width="12%"><%= sort_tag('新增实训评测', name: 'shixun_evaluate_count', path: school_data_grow_managements_path) %></th>
<th width="12%"><%= sort_tag('活跃用户', name: 'active_user_count', path: school_data_grow_managements_path) %></th> <th width="12%"><%= sort_tag('活跃用户', name: 'active_user_count', path: school_data_grow_managements_path) %></th>
</tr> </tr>
</thead> </thead>
@ -36,6 +40,8 @@
<td><%= report.student_increase_count.to_i %></td> <td><%= report.student_increase_count.to_i %></td>
<td><%= report.course_increase_count.to_i %></td> <td><%= report.course_increase_count.to_i %></td>
<td><%= report.shixun_increase_count.to_i %></td> <td><%= report.shixun_increase_count.to_i %></td>
<td><%= report.shixun_homework_count.to_i %></td>
<td><%= report.shixun_evaluate_count.to_i %></td>
<td><%= report.active_user_count.to_i %></td> <td><%= report.active_user_count.to_i %></td>
</tr> </tr>
<% end %> <% end %>

@ -9,9 +9,9 @@
<th width="10%"><%= sort_tag('学生总人数', name: 'student_count', path: school_report_managements_path) %></th> <th width="10%"><%= sort_tag('学生总人数', name: 'student_count', path: school_report_managements_path) %></th>
<th width="10%"><%= sort_tag('课堂总数', name: 'course_count', path: school_report_managements_path) %></th> <th width="10%"><%= sort_tag('课堂总数', name: 'course_count', path: school_report_managements_path) %></th>
<th width="10%"><%= sort_tag('正在进行课堂数', name: 'active_course_count', path: school_report_managements_path) %></th> <th width="10%"><%= sort_tag('正在进行课堂数', name: 'active_course_count', path: school_report_managements_path) %></th>
<th width="10%"><%= sort_tag('创建实训数', name: 'shixun_count', path: school_report_managements_path) %></th> <th width="10%"><%= sort_tag('实训数', name: 'shixun_count', path: school_report_managements_path) %></th>
<!-- <th width="10%"><%#= sort_tag('评测次数', name: 'shixun_evaluate_count', path: school_report_managements_path) %></th>--> <!-- <th width="10%"><%#= sort_tag('实训评测总数', name: 'shixun_evaluate_count', path: school_report_managements_path) %></th>-->
<th width="10%">评测次数</th> <th width="10%">实训评测总数</th>
<th width="10%"><%= sort_tag('实训作业总数', name: 'homework_count', path: school_report_managements_path) %></th> <th width="10%"><%= sort_tag('实训作业总数', name: 'homework_count', path: school_report_managements_path) %></th>
<th width="10%"><%= sort_tag('其它作业总数', name: 'other_homework_count', path: school_report_managements_path) %></th> <th width="10%"><%= sort_tag('其它作业总数', name: 'other_homework_count', path: school_report_managements_path) %></th>
<th width="14%"><%= sort_tag('动态时间', name: 'nearly_course_time', path: school_report_managements_path) %></th> <th width="14%"><%= sort_tag('动态时间', name: 'nearly_course_time', path: school_report_managements_path) %></th>

@ -21,8 +21,8 @@
学生总人数<span class="color-red"><%= @student_total %></span>人, 学生总人数<span class="color-red"><%= @student_total %></span>人,
课堂总数<span class="color-red"><%= @course_total %></span>个, 课堂总数<span class="color-red"><%= @course_total %></span>个,
正在进行课堂总数<span class="color-red"><%= @active_course_total %></span>个, 正在进行课堂总数<span class="color-red"><%= @active_course_total %></span>个,
创建实训数<span class="color-red"><%= @shixun_total %></span>个, 实训数<span class="color-red"><%= @shixun_total %></span>个,
评测次数<span class="color-red"><%= @shixun_evaluate_total %></span>个, 实训评测总数<span class="color-red"><%= @shixun_evaluate_total %></span>个,
实训作业总数<span class="color-red"><%= @shixun_homework_total %></span>个, 实训作业总数<span class="color-red"><%= @shixun_homework_total %></span>个,
其它作业总数<span class="color-red"><%= @other_homework_total %></span>个, 其它作业总数<span class="color-red"><%= @other_homework_total %></span>个,
</div> </div>

@ -3,5 +3,7 @@ zh:
teacher_increase_count: 新增教师 teacher_increase_count: 新增教师
student_increase_count: 新增学生 student_increase_count: 新增学生
course_increase_count: 新增课堂 course_increase_count: 新增课堂
shixun_increase_count: 新增实训作业 shixun_increase_count: 新增实训
shixun_homework_count: 新增实训作业
shixun_evaluate_count: 新增实训评测
active_user_count: 活跃用户 active_user_count: 活跃用户

@ -0,0 +1,6 @@
class AddColumnsToSchoolDailyReports < ActiveRecord::Migration
def change
add_column :school_daily_reports, :shixun_homework_count, :integer, default: 0
add_column :school_daily_reports, :shixun_evaluate_count, :integer, default: 0
end
end

@ -31,11 +31,12 @@ namespace :school_daily_report do
data = reports.map do |report| data = reports.map do |report|
# 无效数据不记录 # 无效数据不记录
next if %w(teacher_count student_count course_count shixun_count active_user_count).all? { |key| report[key].zero? } next if %w(teacher_count student_count course_count shixun_count shixun_homework_count shixun_evaluate_count active_user_count).all? { |key| report[key].zero? }
[ [
report['id'], report['name'], report['teacher_count'], report['student_count'], report['course_count'], report['id'], report['name'], report['teacher_count'], report['student_count'], report['course_count'],
report['shixun_count'], report['active_user_count'], date_str, current_datetime, current_datetime report['shixun_count'], report['shixun_homework_count'], report['shixun_evaluate_count'],
report['active_user_count'], date_str, current_datetime, current_datetime
] ]
end.compact end.compact
batch_create_school_daily_reports!(data) batch_create_school_daily_reports!(data)
@ -69,6 +70,62 @@ namespace :school_daily_report do
end end
end end
desc 'statistic old shixun homework count and shixun evaluate count'
task :statistic_shixun_info_count, [:date] => :environment do |_, args|
date = Time.zone.parse(args[:date]).beginning_of_day
current_date = (Time.zone.now - 5.hour).beginning_of_day
custom_logger("statistic range: #{date}..#{current_date}")
while current_date > date
date_str = date.strftime('%Y-%m-%d')
# 检查当天数据是否已经统计
school_count = School.count
query_times = school_count % query_size == 0 ? school_count / query_size : (school_count / query_size) + 1
custom_logger("Start statistic => Date: #{date_str}, school count: #{school_count}, insert times: #{query_times} ~")
query_times.times do |index|
sql = school_daily_report_sql(date, query_size, index * query_size)
reports = School.find_by_sql(sql)
if SchoolDailyReport.exists?(date: date)
data = []
reports.each do |report|
next if %w(teacher_count student_count course_count shixun_count shixun_homework_count shixun_evaluate_count active_user_count).all? { |key| report[key].zero? }
record = SchoolDailyReport.where(school_id: report['id'], date: date_str).first
if record.present?
record.update_attributes(shixun_homework_count: report['shixun_homework_count'], shixun_evaluate_count: report['shixun_evaluate_count'])
else
data << [
report['id'], report['name'], report['teacher_count'], report['student_count'], report['course_count'],
report['shixun_count'], report['shixun_homework_count'], report['shixun_evaluate_count'],
report['active_user_count'], date_str, current_datetime, current_datetime
]
end
end
else
data = reports.map do |report|
# 无效数据不记录
next if %w(teacher_count student_count course_count shixun_count shixun_homework_count shixun_evaluate_count active_user_count).all? { |key| report[key].zero? }
[
report['id'], report['name'], report['teacher_count'], report['student_count'], report['course_count'],
report['shixun_count'], report['shixun_homework_count'], report['shixun_evaluate_count'],
report['active_user_count'], date_str, current_datetime, current_datetime
]
end.compact
batch_create_school_daily_reports!(data) if data.present?
end
end
custom_logger("Statistic complete! date: #{date_str}")
date += 1.day
end
end
def query_size def query_size
100 100
end end
@ -88,7 +145,7 @@ namespace :school_daily_report do
def build_insert_report_sql(arr) def build_insert_report_sql(arr)
prefix = 'INSERT INTO school_daily_reports(school_id, school_name, teacher_increase_count, student_increase_count, '\ prefix = 'INSERT INTO school_daily_reports(school_id, school_name, teacher_increase_count, student_increase_count, '\
'course_increase_count, shixun_increase_count, active_user_count, date, created_at, updated_at) VALUES' 'course_increase_count, shixun_increase_count, shixun_homework_count, shixun_evaluate_count, active_user_count, date, created_at, updated_at) VALUES'
# [[1,2], [3,4]] => ['"1", "2"', '"3", "4"'] => '("1", "2"),("3", "4")' # [[1,2], [3,4]] => ['"1", "2"', '"3", "4"'] => '("1", "2"),("3", "4")'
values = '(' + arr.map { |item| '"' + item.join('","') + '"' }.join('),(') + ')' values = '(' + arr.map { |item| '"' + item.join('","') + '"' }.join('),(') + ')'
@ -120,6 +177,18 @@ namespace :school_daily_report do
LEFT JOIN user_extensions ue ON ue.user_id = u.id LEFT JOIN user_extensions ue ON ue.user_id = u.id
WHERE ue.school_id = schools.id AND sx.created_at BETWEEN "#{begin_date}" AND "#{end_date}" WHERE ue.school_id = schools.id AND sx.created_at BETWEEN "#{begin_date}" AND "#{end_date}"
) shixun_count, ( ) shixun_count, (
SELECT COUNT(*) FROM homework_commons hc
LEFT JOIN courses cs ON hc.course_id = cs.id
WHERE cs.school_id = schools.id AND hc.homework_type = 4 AND hc.created_at BETWEEN "#{begin_date}" AND "#{end_date}"
) shixun_homework_count, (
SELECT COUNT(*) FROM outputs os
LEFT JOIN games gs ON os.game_id = gs.id
LEFT JOIN challenges cls ON gs.challenge_id = cls.id
LEFT JOIN shixuns sx ON cls.shixun_id = sx.id
LEFT JOIN users u ON sx.user_id = u.id
INNER JOIN user_extensions ue ON ue.user_id = u.id
WHERE ue.school_id = schools.id AND ue.identity = #{User::TEACHER} AND os.created_at BETWEEN "#{begin_date}" AND "#{end_date}"
) shixun_evaluate_count, (
SELECT COUNT(*) FROM users u SELECT COUNT(*) FROM users u
LEFT JOIN user_extensions ue ON ue.user_id = u.id LEFT JOIN user_extensions ue ON ue.user_id = u.id
WHERE ue.school_id = schools.id AND u.last_login_on BETWEEN "#{begin_date}" AND "#{end_date}" WHERE ue.school_id = schools.id AND u.last_login_on BETWEEN "#{begin_date}" AND "#{end_date}"

Loading…
Cancel
Save