From ec8b0472283baed4dbcadf1f9f8d8aed626809ac Mon Sep 17 00:00:00 2001 From: p31729568 Date: Fri, 31 May 2019 08:45:31 +0800 Subject: [PATCH] add shixun homework and shixun evaluate count to school daily report --- .../school_data_contrast_service.rb | 2 +- .../management/school_data_grow_service.rb | 5 ++ .../statistic_school_daily_report_task.rb | 16 +++- .../schools/_data_grow_list.html.erb | 10 ++- .../schools/_statistics_list.html.erb | 6 +- .../managements/schools/statistics.html.erb | 4 +- config/locales/school_daily_reports/zh.yml | 4 +- ...655_add_columns_to_school_daily_reports.rb | 6 ++ lib/tasks/statistic_school_daily_report.rake | 75 ++++++++++++++++++- 9 files changed, 114 insertions(+), 14 deletions(-) create mode 100644 db/migrate/20190530081655_add_columns_to_school_daily_reports.rb diff --git a/app/services/management/school_data_contrast_service.rb b/app/services/management/school_data_contrast_service.rb index 7637ec4a..0df85f4d 100644 --- a/app/services/management/school_data_contrast_service.rb +++ b/app/services/management/school_data_contrast_service.rb @@ -4,7 +4,7 @@ class Management::SchoolDataContrastService PAGE_SIZE = 20 CONTRAST_COLUMN_LIST = %w( 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 attr_reader :params, :sort_direction, :contrast_column diff --git a/app/services/management/school_data_grow_service.rb b/app/services/management/school_data_grow_service.rb index d9818d52..44fc45e1 100644 --- a/app/services/management/school_data_grow_service.rb +++ b/app/services/management/school_data_grow_service.rb @@ -7,6 +7,7 @@ class Management::SchoolDataGrowService sort_columns :teacher_increase_count, :student_increase_count, :course_increase_count, :shixun_increase_count, :active_user_count, + :shixun_homework_count, :shixun_evaluate_count, default_by: :teacher_increase_count, default_direction: :desc def initialize(params) @@ -24,6 +25,8 @@ class Management::SchoolDataGrowService 'SUM(student_increase_count) student_increase_count,'\ 'SUM(course_increase_count) course_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' ) @@ -40,6 +43,8 @@ class Management::SchoolDataGrowService 'SUM(student_increase_count) student_increase_count,'\ 'SUM(course_increase_count) course_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' ).first end diff --git a/app/tasks/statistic_school_daily_report_task.rb b/app/tasks/statistic_school_daily_report_task.rb index 138db4d5..440d4a97 100644 --- a/app/tasks/statistic_school_daily_report_task.rb +++ b/app/tasks/statistic_school_daily_report_task.rb @@ -16,14 +16,26 @@ class StatisticSchoolDailyReportTask # 新增实训 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 + # 新增实训作业数 + 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 = { school_id: school.id, school_name: school.name, teacher_increase_count: teacher_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 } SchoolDailyReport.create!(create_params) diff --git a/app/views/managements/schools/_data_grow_list.html.erb b/app/views/managements/schools/_data_grow_list.html.erb index 8f502979..51371f4e 100644 --- a/app/views/managements/schools/_data_grow_list.html.erb +++ b/app/views/managements/schools/_data_grow_list.html.erb @@ -9,7 +9,9 @@ 新增教师<%= @grow_summary.teacher_increase_count || 0 %>人, 新增学生<%= @grow_summary.student_increase_count || 0 %>人, 新增课堂<%= @grow_summary.course_increase_count || 0 %>个, - 新增实训作业<%= @grow_summary.shixun_increase_count || 0 %>个, + 新增实训<%= @grow_summary.shixun_increase_count || 0 %>个, + 新增实训作业<%= @grow_summary.shixun_homework_count || 0 %>个, + 新增实训评测<%= @grow_summary.shixun_evaluate_count || 0 %>个, 活跃用户<%= @grow_summary.active_user_count || 0 %>个 @@ -22,7 +24,9 @@ - + + + @@ -36,6 +40,8 @@ + + <% end %> diff --git a/app/views/managements/schools/_statistics_list.html.erb b/app/views/managements/schools/_statistics_list.html.erb index 559ff05c..efea22e8 100644 --- a/app/views/managements/schools/_statistics_list.html.erb +++ b/app/views/managements/schools/_statistics_list.html.erb @@ -9,9 +9,9 @@ - - - + + + diff --git a/app/views/managements/schools/statistics.html.erb b/app/views/managements/schools/statistics.html.erb index d4eff134..1e11c068 100644 --- a/app/views/managements/schools/statistics.html.erb +++ b/app/views/managements/schools/statistics.html.erb @@ -21,8 +21,8 @@ 学生总人数<%= @student_total %>人, 课堂总数<%= @course_total %>个, 正在进行课堂总数<%= @active_course_total %>个, - 创建实训数<%= @shixun_total %>个, - 评测次数<%= @shixun_evaluate_total %>个, + 实训总数<%= @shixun_total %>个, + 实训评测总数<%= @shixun_evaluate_total %>个, 实训作业总数<%= @shixun_homework_total %>个, 其它作业总数<%= @other_homework_total %>个, diff --git a/config/locales/school_daily_reports/zh.yml b/config/locales/school_daily_reports/zh.yml index 2db8a429..c622a21f 100644 --- a/config/locales/school_daily_reports/zh.yml +++ b/config/locales/school_daily_reports/zh.yml @@ -3,5 +3,7 @@ zh: teacher_increase_count: 新增教师 student_increase_count: 新增学生 course_increase_count: 新增课堂 - shixun_increase_count: 新增实训作业 + shixun_increase_count: 新增实训 + shixun_homework_count: 新增实训作业 + shixun_evaluate_count: 新增实训评测 active_user_count: 活跃用户 \ No newline at end of file diff --git a/db/migrate/20190530081655_add_columns_to_school_daily_reports.rb b/db/migrate/20190530081655_add_columns_to_school_daily_reports.rb new file mode 100644 index 00000000..d3571f02 --- /dev/null +++ b/db/migrate/20190530081655_add_columns_to_school_daily_reports.rb @@ -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 diff --git a/lib/tasks/statistic_school_daily_report.rake b/lib/tasks/statistic_school_daily_report.rake index 9be09ad6..88d4881e 100644 --- a/lib/tasks/statistic_school_daily_report.rake +++ b/lib/tasks/statistic_school_daily_report.rake @@ -31,11 +31,12 @@ namespace :school_daily_report do 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['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 batch_create_school_daily_reports!(data) @@ -69,6 +70,62 @@ namespace :school_daily_report do 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 100 end @@ -88,7 +145,7 @@ namespace :school_daily_report do def build_insert_report_sql(arr) 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")' 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 WHERE ue.school_id = schools.id AND sx.created_at BETWEEN "#{begin_date}" AND "#{end_date}" ) 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 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}"
<%= sort_tag('新增教师', name: 'teacher_increase_count', path: school_data_grow_managements_path) %> <%= sort_tag('新增学生', name: 'student_increase_count', path: school_data_grow_managements_path) %> <%= sort_tag('新增课堂', name: 'course_increase_count', path: school_data_grow_managements_path) %><%= sort_tag('新增实训作业', name: 'shixun_increase_count', path: school_data_grow_managements_path) %><%= sort_tag('新增实训', name: 'shixun_increase_count', path: school_data_grow_managements_path) %><%= sort_tag('新增实训作业', name: 'shixun_homework_count', path: school_data_grow_managements_path) %><%= sort_tag('新增实训评测', name: 'shixun_evaluate_count', path: school_data_grow_managements_path) %> <%= sort_tag('活跃用户', name: 'active_user_count', path: school_data_grow_managements_path) %>
<%= report.student_increase_count.to_i %> <%= report.course_increase_count.to_i %> <%= report.shixun_increase_count.to_i %><%= report.shixun_homework_count.to_i %><%= report.shixun_evaluate_count.to_i %> <%= report.active_user_count.to_i %>
<%= sort_tag('学生总人数', name: 'student_count', path: school_report_managements_path) %> <%= sort_tag('课堂总数', name: 'course_count', path: school_report_managements_path) %> <%= sort_tag('正在进行课堂数', name: 'active_course_count', path: school_report_managements_path) %><%= sort_tag('创建实训数', name: 'shixun_count', path: school_report_managements_path) %>评测次数<%= sort_tag('实训总数', name: 'shixun_count', path: school_report_managements_path) %>实训评测总数 <%= sort_tag('实训作业总数', name: 'homework_count', path: school_report_managements_path) %> <%= sort_tag('其它作业总数', name: 'other_homework_count', path: school_report_managements_path) %> <%= sort_tag('动态时间', name: 'nearly_course_time', path: school_report_managements_path) %>