手工统计

dev_local_lzdx
xiaoxiaoqiong 4 years ago
parent 5253c55c73
commit f7093d1dd5

@ -0,0 +1,72 @@
class StatisticSchoolDailyReport1Task
def call
School.find_each do |school|
# 新增教师和学生
users = User.joins(:user_extensions)
.where(user_extensions: {school_id: school.id})
teacher_count = users.where(created_on: yesterday, user_extensions: {identity: User::TEACHER}).count
student_count = users.where(created_on: yesterday, user_extensions: {identity: User::STUDENT}).count
professional_count = users.where(created_on: yesterday, user_extensions: {identity: User::ENTERPRISE}).count
# 活跃用户
active_user_ids = users.where(last_login_on: yesterday).pluck(:id)
active_user_count = active_user_ids.size
# 新增课堂
course_count = school.courses.where(created_at: yesterday).count
# 新增实训
shixun_count = Shixun.joins(creator: :user_extensions)
.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 = 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 course_members ON course_members.user_id = evaluate_records.user_id')
.joins('LEFT JOIN courses ON course_members.course_id = courses.id AND hc.course_id = courses.id')
.where(courses: {school_id: school.id})
.where(created_at: yesterday).reorder(nil).count
# shixun_evaluate_count = 0
# 无有效数据时不记录
data = [teacher_count, student_count, professional_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, professional_increase_count: professional_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
}
report = SchoolDailyReport.create!(create_params)
if active_user_ids.present?
values = '(' + active_user_ids.join(", #{report.id}),(") + ", #{report.id})"
user_sql = "INSERT INTO school_daily_active_users(user_id, school_daily_report_id) VALUES#{values}"
SchoolDailyActiveUser.connection.execute(user_sql)
end
end
end
private
def current_date
@_current_date ||= Time.parse("2021-05-20") - 1.day
end
def yesterday
@_yesterday ||= begin
# 每日凌晨2点为节点
end_time = Time.parse("2021-05-20") + 2.hour
begin_time = end_time - 1.day
begin_time..end_time
end
end
end

@ -0,0 +1,72 @@
class StatisticSchoolDailyReport2Task
def call
School.find_each do |school|
# 新增教师和学生
users = User.joins(:user_extensions)
.where(user_extensions: {school_id: school.id})
teacher_count = users.where(created_on: yesterday, user_extensions: {identity: User::TEACHER}).count
student_count = users.where(created_on: yesterday, user_extensions: {identity: User::STUDENT}).count
professional_count = users.where(created_on: yesterday, user_extensions: {identity: User::ENTERPRISE}).count
# 活跃用户
active_user_ids = users.where(last_login_on: yesterday).pluck(:id)
active_user_count = active_user_ids.size
# 新增课堂
course_count = school.courses.where(created_at: yesterday).count
# 新增实训
shixun_count = Shixun.joins(creator: :user_extensions)
.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 = 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 course_members ON course_members.user_id = evaluate_records.user_id')
.joins('LEFT JOIN courses ON course_members.course_id = courses.id AND hc.course_id = courses.id')
.where(courses: {school_id: school.id})
.where(created_at: yesterday).reorder(nil).count
# shixun_evaluate_count = 0
# 无有效数据时不记录
data = [teacher_count, student_count, professional_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, professional_increase_count: professional_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
}
report = SchoolDailyReport.create!(create_params)
if active_user_ids.present?
values = '(' + active_user_ids.join(", #{report.id}),(") + ", #{report.id})"
user_sql = "INSERT INTO school_daily_active_users(user_id, school_daily_report_id) VALUES#{values}"
SchoolDailyActiveUser.connection.execute(user_sql)
end
end
end
private
def current_date
@_current_date ||= Time.parse("2021-05-21") - 1.day
end
def yesterday
@_yesterday ||= begin
# 每日凌晨2点为节点
end_time = Time.parse("2021-05-21") + 2.hour
begin_time = end_time - 1.day
begin_time..end_time
end
end
end

@ -57,13 +57,13 @@ class StatisticSchoolDailyReportTask
private
def current_date
@_current_date ||= Time.parse("2021-05-20") - 1.day
@_current_date ||= Time.zone.now.beginning_of_day - 1.day
end
def yesterday
@_yesterday ||= begin
# 每日凌晨2点为节点
end_time = Time.parse("2021-05-20") + 2.hour
end_time = Time.zone.now.beginning_of_day + 2.hour
begin_time = end_time - 1.day
begin_time..end_time
end

@ -6,6 +6,13 @@ namespace :school_daily_report do
StatisticSchoolDailyReportTask.new.call
end
task daily_statistic19: :environment do
StatisticSchoolDailyReport1Task.new.call
end
task daily_statistic20: :environment do
StatisticSchoolDailyReport2Task.new.call
end
desc 'statistic school daily report data before now'
task :statistic, [:date] => :environment do |_, args|
date = Time.zone.parse(args[:date]).beginning_of_day

Loading…
Cancel
Save