|
|
@ -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}"
|
|
|
|