diff --git a/app/tasks/statistic_school_daily_report_task.rb b/app/tasks/statistic_school_daily_report_task.rb index 5d0db197..c533c55c 100644 --- a/app/tasks/statistic_school_daily_report_task.rb +++ b/app/tasks/statistic_school_daily_report_task.rb @@ -25,13 +25,12 @@ class StatisticSchoolDailyReportTask .where(homework_type: 4, created_at: yesterday).count # 新增实训评测数量 - shixun_evaluate_count = Output.joins(game: :challenge) - .joins('LEFT JOIN homework_commons_shixuns hcs ON hcs.shixun_id = challenges.shixun_id') + shixun_evaluate_count = EvaluateRecord.joins('LEFT JOIN homework_commons_shixuns hcs ON hcs.shixun_id = evaluate_records.shixun_id') .joins('LEFT JOIN homework_commons hc ON hcs.homework_common_id = hc.id AND hc.homework_type = 4') - .joins('LEFT JOIN members ON members.user_id = games.user_id') + .joins('LEFT JOIN members ON members.user_id = evaluate_records.user_id') .joins('LEFT JOIN courses ON members.course_id = courses.id AND hc.course_id = courses.id') .where(courses: { school_id: school.id }) - .where(created_at: yesterday).count + .where(created_at: yesterday).reorder(nil).count # 无有效数据时不记录 data = [teacher_count, student_count, course_count, shixun_count, active_user_count, diff --git a/lib/tasks/statistic_school_daily_report.rake b/lib/tasks/statistic_school_daily_report.rake index 1dbb591e..2d487ff6 100644 --- a/lib/tasks/statistic_school_daily_report.rake +++ b/lib/tasks/statistic_school_daily_report.rake @@ -70,55 +70,106 @@ 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| + # 统计学校历史每天实训作业新增数 + desc 'statistic school shixun homework count everyday' + task :statistic_shixun_homework_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} ~") - 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) + begin_date = (date + 5.hour).strftime('%Y-%m-%d %H:%M:%S') + end_date = (date + 1.day + 5.hour).strftime('%Y-%m-%d %H:%M:%S') + + homework_count_map = HomeworkCommon.joins('LEFT JOIN courses cs ON homework_commons.course_id = cs.id') + .where('cs.school_id IS NOT NULL') + .where(homework_type: 4, created_at: begin_date...end_date).reorder(nil) + .group('cs.school_id').count + + ActiveRecord::Base.transaction do + exist_school_ids = SchoolDailyReport.where(date: date_str).pluck(:school_id) - 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 + updated_school_ids = homework_count_map.keys & exist_school_ids # 需要更新的 + + updated_reports = [] + created_reports = [] + homework_count_map.each do |school_id, homework_count| + if updated_school_ids.include?(school_id) + updated_reports << "WHEN #{school_id} THEN #{homework_count}" + else + created_reports << "(#{school_id}, '#{date_str}', #{homework_count})" 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 + + if updated_reports.present? + # 这样更新快些 + SchoolDailyReport.connection.execute("UPDATE school_daily_reports SET shixun_homework_count = CASE school_id #{updated_reports.join(' ')} END "\ + "WHERE school_id IN (#{updated_school_ids.join(',')}) AND date = '#{date_str}'") + end + + if created_reports.present? + SchoolDailyReport.connection.execute("INSERT INTO school_daily_reports(school_id, date, shixun_homework_count) VALUES#{created_reports.join(',')}") end end + + custom_logger("Statistic complete! date: #{date_str}") + + date += 1.day + end + end + + # 统计学校历史每天实训评测新增数 + desc 'statistic school shixun evaluate count everyday' + task :statistic_shixun_evaluate_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') + + custom_logger("Start statistic => Date: #{date_str} ~") + + begin_date = (date + 5.hour).strftime('%Y-%m-%d %H:%M:%S') + end_date = (date + 1.day + 5.hour).strftime('%Y-%m-%d %H:%M:%S') + + evaluate_count_map = EvaluateRecord.joins('LEFT JOIN homework_commons_shixuns hcs ON hcs.shixun_id = evaluate_records.shixun_id') + .joins('LEFT JOIN homework_commons hc ON hcs.homework_common_id = hc.id AND hc.homework_type = 4') + .joins('LEFT JOIN members ON members.user_id = evaluate_records.user_id') + .joins('LEFT JOIN courses ON members.course_id = courses.id AND hc.course_id = courses.id') + .where(created_at: begin_date...end_date).where('courses.school_id IS NOT NULL') + .reorder(nil).group('courses.school_id').count + + ActiveRecord::Base.transaction do + exist_school_ids = SchoolDailyReport.where(date: date_str).pluck(:school_id) + + updated_school_ids = evaluate_count_map.keys & exist_school_ids # 需要更新的 + + updated_reports = [] + created_reports = [] + evaluate_count_map.each do |school_id, evaluate_count| + if updated_school_ids.include?(school_id) + updated_reports << "WHEN #{school_id} THEN #{evaluate_count}" + else + created_reports << "(#{school_id}, '#{date_str}', #{evaluate_count})" + end + end + + if updated_reports.present? + # 这样更新快些 + SchoolDailyReport.connection.execute("UPDATE school_daily_reports SET shixun_evaluate_count = CASE school_id #{updated_reports.join(' ')} END "\ + "WHERE school_id IN (#{updated_school_ids.join(',')}) AND date = '#{date_str}'") + end + + if created_reports.present? + SchoolDailyReport.connection.execute("INSERT INTO school_daily_reports(school_id, date, shixun_evaluate_count) VALUES#{created_reports.join(',')}") + end + end + custom_logger("Statistic complete! date: #{date_str}") date += 1.day @@ -181,14 +232,12 @@ namespace :school_daily_report do 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 members ON members.user_id = gs.user_id - LEFT JOIN challenges cls ON gs.challenge_id = cls.id - LEFT JOIN homework_commons_shixuns hcs ON hcs.shixun_id = cls.shixun_id + SELECT COUNT(*) FROM evaluate_records er + LEFT JOIN members ON members.user_id = er.user_id + LEFT JOIN homework_commons_shixuns hcs ON hcs.shixun_id = er.shixun_id LEFT JOIN homework_commons hc ON hcs.homework_common_id = hc.id LEFT JOIN courses ON hc.course_id = courses.id AND members.course_id = courses.id - WHERE courses.school_id = schools.id AND os.created_at BETWEEN "#{begin_date}" AND "#{end_date}" + WHERE courses.school_id = schools.id AND er.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