dev_partners
Alec Zhou 6 years ago
commit face0cc8de

@ -4119,7 +4119,7 @@ end
sheet1[count_row, 5] = show_shixun_mirror(shixun)
sheet1[count_row, 6] = shixun.fork_identifier
sheet1[count_row, 7] = shixun.challenges.count
sheet1[count_row, 7] = Setting.server_url + shixun_path(shixun)
sheet1[count_row, 8] = Setting.server_url + shixun_path(shixun)
count_row += 1
end
book.write xls_report

@ -22,6 +22,9 @@ class School < ActiveRecord::Base
# banner图片信息
has_many :school_images, :dependent => :destroy
# 报表信息
has_many :school_daily_reports
# 视频附件
acts_as_attachable

@ -17,15 +17,17 @@ class Management::SchoolDataContrastService
def call
validate_parameter!
reports = SchoolDailyReport.select(select_columns)
reports = School.joins(:school_daily_reports).select(select_columns)
keyword = params[:keyword].try(:to_s).try(:strip)
if keyword.present?
reports = reports.where("school_name LIKE :keyword OR school_id LIKE :keyword", keyword: "%#{keyword}%")
reports = reports.where("schools.name LIKE :keyword OR schools.id LIKE :keyword", keyword: "%#{keyword}%")
end
count = reports.count('distinct(school_id)')
reports = reports.group(:school_id).limit(PAGE_SIZE).offset(offset)
count = reports.count('distinct(schools.id)')
sql = query_report_sql(reports.group('schools.id').to_sql)
reports = SchoolDailyReport.find_by_sql(sql)
[count, reports]
end
@ -54,8 +56,13 @@ class Management::SchoolDataContrastService
end
def select_columns
"school_id, school_name,"\
"schools.id school_id, schools.name school_name,"\
"(SUM(IF(date BETWEEN '#{format_date(params[:begin_date])}' AND '#{format_date(params[:end_date])}', #{contrast_column}, 0))) total,"\
"(SUM(IF(date BETWEEN '#{format_date(params[:other_begin_date])}' AND '#{format_date(params[:other_end_date])}', #{contrast_column}, 0))) other_total"\
end
def query_report_sql(from_sql)
"SELECT reports.*, (other_total - total) increase, (IF(other_total - total = 0, 0.0, round((other_total - total) / IF(total = 0, 1, total), 5))) percentage "\
"FROM (#{from_sql}) reports ORDER BY percentage #{sort_direction} LIMIT #{PAGE_SIZE} OFFSET #{offset}"
end
end

@ -14,12 +14,12 @@ class Management::SchoolDataGrowService
end
def call
reports = query_reports.group(:school_id)
reports = query_reports.group('schools.id')
count = reports.count.count
reports = reports.select(
'school_id, school_name,'\
'schools.id school_id, schools.name school_name,'\
'SUM(teacher_increase_count) teacher_increase_count,'\
'SUM(student_increase_count) student_increase_count,'\
'SUM(course_increase_count) course_increase_count,'\
@ -28,7 +28,7 @@ class Management::SchoolDataGrowService
)
reports = custom_sort(reports, params[:sort_by], params[:sort_direction])
reports = reports.limit(PAGE_SIZE).offset(offset)
reports = reports.order('school_id asc').limit(PAGE_SIZE).offset(offset)
[count, reports]
end
@ -47,11 +47,18 @@ class Management::SchoolDataGrowService
private
def query_reports
reports = SchoolDailyReport.where(date: query_date)
date = query_date
date_condition = if date.is_a?(Range)
"sdr.date BETWEEN '#{date.min.strftime('%Y-%m-%d')}' AND '#{date.max.strftime('%Y-%m-%d')}'"
else
"sdr.date = '#{date.strftime('%Y-%m-%d')}'"
end
reports = School.joins("LEFT JOIN school_daily_reports sdr ON sdr.school_id = schools.id AND #{date_condition}")
keyword = params[:keyword].try(:to_s).try(:strip)
if keyword.present?
reports = reports.where("school_name LIKE :keyword OR school_id LIKE :keyword", keyword: "%#{keyword}%")
reports = reports.where("schools.name LIKE :keyword OR schools.id LIKE :keyword", keyword: "%#{keyword}%")
end
reports

@ -26,29 +26,16 @@ class Management::SchoolReportService
private
def select_columns_sql
<<-SQL
schools.id, schools.name,
(
SELECT COUNT(*) FROM user_extensions ue
WHERE ue.school_id = schools.id AND ue.identity = #{User::STUDENT}
) student_count,
(
SELECT COUNT(*) FROM user_extensions ue
WHERE ue.school_id = schools.id AND ue.identity = #{User::TEACHER}
) teacher_count,
(
SELECT COUNT(*) FROM homework_commons hc
LEFT JOIN courses ON courses.id = hc.course_id
WHERE courses.school_id = schools.id AND hc.homework_type = 4
) homework_count,
(
SELECT COUNT(*) FROM homework_commons hc
LEFT JOIN courses ON courses.id = hc.course_id
WHERE courses.school_id = schools.id AND hc.homework_type IN (1,3)
) other_homework_count,
(SELECT COUNT(*) FROM courses cs WHERE cs.school_id = schools.id) course_count ,
(SELECT MAX(cs.updated_at) FROM courses cs WHERE cs.school_id = schools.id) nearly_course_time ,
(SELECT COUNT(*) FROM courses acs WHERE acs.school_id = schools.id AND acs.is_end = false) active_course_count
SQL
"schools.id, schools.name,"\
"(SELECT COUNT(*) FROM user_extensions ue WHERE ue.school_id = schools.id AND ue.identity = #{User::STUDENT}) student_count,"\
"(SELECT COUNT(*) FROM user_extensions ue WHERE ue.school_id = schools.id AND ue.identity = #{User::TEACHER}) teacher_count,"\
"(SELECT COUNT(*) FROM homework_commons hc LEFT JOIN courses ON courses.id = hc.course_id "\
"WHERE courses.school_id = schools.id AND hc.homework_type = 4) homework_count,"\
"(SELECT COUNT(*) FROM homework_commons hc LEFT JOIN courses ON courses.id = hc.course_id "\
"WHERE courses.school_id = schools.id AND hc.homework_type IN (1,3)) other_homework_count,"\
"(SELECT COUNT(*) FROM courses cs WHERE cs.school_id = schools.id) course_count ,"\
"(SELECT MAX(ca.updated_at) FROM course_activities ca LEFT JOIN courses cs ON cs.id = ca.course_id "\
"WHERE cs.school_id = schools.id) nearly_course_time ,"\
"(SELECT COUNT(*) FROM courses acs WHERE acs.school_id = schools.id AND acs.is_end = false) active_course_count"
end
end

@ -18,6 +18,8 @@ class StatisticSchoolDailyReportTask
shixun_count = Shixun.joins(creator: :user_extensions)
.where('user_extensions.school_id = ?', school.id)
.where(created_at: yesterday).count
# 无有效数据时不记录
next if [teacher_count, student_count, course_count, shixun_count, active_user_count].all?(&:zero?)
create_params = {
school_id: school.id, school_name: school.name, teacher_increase_count: teacher_count,

@ -24,7 +24,7 @@
<th width="22%">时段一<br><%= "#{params[:begin_date]}至#{params[:end_date]}" %></th>
<th width="22%">时段二<br><%= "#{params[:other_begin_date]}至#{params[:other_end_date]}" %></th>
<th width="20%" colspan="2">
变化情况
<%= sort_tag('变化情况', name: 'percentage', path: school_data_contrast_managements_path) %>
<br> 新 增 数 | 新增百分比)
</th>
</tr>

@ -31,11 +31,11 @@
<td><%= (@obj_pages.page - 1) * @obj_pages.per_page + index + 1 %></td>
<td><%= report.school_id %></td>
<td class="edu-txt-left"><%= report.school_name %></td>
<td><%= report.teacher_increase_count %></td>
<td><%= report.student_increase_count %></td>
<td><%= report.course_increase_count %></td>
<td><%= report.shixun_increase_count %></td>
<td><%= report.active_user_count %></td>
<td><%= report.teacher_increase_count.to_i %></td>
<td><%= report.student_increase_count.to_i %></td>
<td><%= report.course_increase_count.to_i %></td>
<td><%= report.shixun_increase_count.to_i %></td>
<td><%= report.active_user_count.to_i %></td>
</tr>
<% end %>
</tbody>

@ -81,7 +81,7 @@
<td><%= user_query.blank? ? "--" : user_query.first[1] %></td>
<td><%= game.try(:end_time).nil? ? "--" : format_time(game.end_time) %></td>
<td><%= game.consumes_time %></td>
<td><%= game.status == 2 ? (game.final_score.to_i < 0 ? 0 : game.challenge.score.to_i) : 0 %> / <%= game.challenge.st == 1 ? game.challenge.choose_score : game.challenge.score %></td>
<td><%= game.status == 2 ? (game.final_score.to_i < 0 ? 0 : game.challenge.choose_score.to_i) : 0 %> / <%= game.challenge.choose_score %></td>
</tr>
<% end %>
</tbody>

@ -30,11 +30,14 @@ namespace :school_daily_report do
reports = School.find_by_sql(sql)
data = reports.map do |report|
# 无效数据不记录
next if %w(teacher_count student_count course_count shixun_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
]
end
end.compact
batch_create_school_daily_reports!(data)
end
custom_logger("Statistic complete! date: #{date_str}")
@ -43,9 +46,27 @@ namespace :school_daily_report do
end
end
desc 'clear school daily report data'
task clear: :environment do
SchoolDailyReport.destroy_all
desc 'clear useless school daily report data'
task clear_useless_data: :environment do
condition = {
teacher_increase_count: 0, student_increase_count: 0, course_increase_count: 0,
shixun_increase_count: 0, active_user_count: 0
}
reports = SchoolDailyReport.where(condition)
# 因为数据量太大直接删除全部的话mysql会报错(Lock wait timeout exceeded)
min_date = reports.minimum(:date).to_time
now_date = Time.zone.now
custom_logger("Useless data total: #{reports.count}")
while min_date < now_date
custom_logger("Delete record in date range: #{min_date.strftime('%Y-%m-%d')} .. #{(min_date + 1.months).strftime('%Y-%m-%d')}) ~")
reports.where(date: min_date..(min_date + 1.months)).delete_all
custom_logger("Delete Success!")
min_date += 1.months
end
end
def query_size

Loading…
Cancel
Save