From e1a8372b2bb5df5690d26f5d1fbeebbdacc66def Mon Sep 17 00:00:00 2001 From: p31729568 Date: Mon, 3 Jun 2019 08:29:52 +0800 Subject: [PATCH 01/12] management export school report xlsx add column --- app/services/management/school_report_service.rb | 6 +++--- app/views/managements/schools/statistics_xlsx.xlsx.axlsx | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/services/management/school_report_service.rb b/app/services/management/school_report_service.rb index 406460cc..9837c1d1 100644 --- a/app/services/management/school_report_service.rb +++ b/app/services/management/school_report_service.rb @@ -49,10 +49,10 @@ class Management::SchoolReportService active_course_map = courses.where(is_end: false).count shixun_map = Shixun.joins(creator: :user_extensions).where(user_extensions: { identity: User::TEACHER, school_id: ids }) - .group('school_id').count + .where(fork_from: nil).group('school_id').count evaluate_count_map = Game.joins(challenge: { shixun: { creator: :user_extensions } }) .where(user_extensions: { identity: User::TEACHER, school_id: ids }) - .group('school_id').sum(:evaluate_count) + .group('school_id').reorder(nil).sum(:evaluate_count) schools.map do |school| { @@ -88,7 +88,7 @@ class Management::SchoolReportService schools.joins('LEFT JOIN courses cs ON cs.school_id = schools.id AND cs.is_delete = 0') .select("#{base_query_column}, COUNT(*) course_count") when 'shixun_count' then - schools.joins(teacher_extensions: :user).joins('INNER JOIN shixuns sx ON sx.user_id = users.id') + schools.joins(teacher_extensions: :user).joins('INNER JOIN shixuns sx ON sx.user_id = users.id AND sx.fork_from IS NULL') .select("#{base_query_column}, COUNT(*) shixun_count") when 'shixun_evaluate_count' then schools.joins(teacher_extensions: :user).joins('INNER JOIN shixuns sx ON sx.user_id = users.id') diff --git a/app/views/managements/schools/statistics_xlsx.xlsx.axlsx b/app/views/managements/schools/statistics_xlsx.xlsx.axlsx index a451586d..96470019 100644 --- a/app/views/managements/schools/statistics_xlsx.xlsx.axlsx +++ b/app/views/managements/schools/statistics_xlsx.xlsx.axlsx @@ -1,11 +1,12 @@ wb = xlsx_package.workbook wb.add_worksheet(name: '统计总表') do |sheet| - sheet.add_row %w(ID 单位名称 教师总人数 学生总人数 课堂总数 正在进行课堂数 实训作业总数 其它作业总数 动态时间) + sheet.add_row %w(ID 单位名称 教师总人数 学生总人数 课堂总数 正在进行课堂数 总实训数 实训评测总数 实训作业总数 其它作业总数 动态时间) @schools.each do |school| sheet.add_row([ school[:id].to_s, school[:name].to_s, (school[:teacher_count] || 0).to_s, (school[:student_count] || 0).to_s, - (school[:course_count] || 0).to_s, (school[:active_course_count] || 0).to_s, (school[:homework_count] || 0).to_s, + (school[:course_count] || 0).to_s, (school[:active_course_count] || 0).to_s, + (school[:shixun_count] || 0).to_s,(school[:shixun_evaluate_count] || 0).to_s, (school[:homework_count] || 0).to_s, (school[:other_homework_count] || 0).to_s, format_time(school[:nearly_course_time]) ]) end From a720a5477bb10becbf35be1f9faa8c838bea0cfe Mon Sep 17 00:00:00 2001 From: p31729568 Date: Mon, 3 Jun 2019 09:54:22 +0800 Subject: [PATCH 02/12] remove "," --- app/views/managements/schools/statistics.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/managements/schools/statistics.html.erb b/app/views/managements/schools/statistics.html.erb index 1e11c068..1f0d2d36 100644 --- a/app/views/managements/schools/statistics.html.erb +++ b/app/views/managements/schools/statistics.html.erb @@ -24,7 +24,7 @@ 实训总数<%= @shixun_total %>个, 实训评测总数<%= @shixun_evaluate_total %>个, 实训作业总数<%= @shixun_homework_total %>个, - 其它作业总数<%= @other_homework_total %>个, + 其它作业总数<%= @other_homework_total %>个 From 41e897150b8309157e0a9f0e3652e0c9901ad8d0 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Mon, 3 Jun 2019 10:50:08 +0800 Subject: [PATCH 03/12] =?UTF-8?q?=E8=B6=85=E7=BA=A7=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=EF=BC=8C=E7=94=A8=E6=88=B7=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E7=9A=84=E6=9B=B4=E6=96=B0=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E7=9A=84=E9=87=91=E5=B8=81=E5=92=8C=E7=BB=8F=E9=AA=8C=E9=93=BE?= =?UTF-8?q?=E6=8E=A5=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/managements/update_user.html.erb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/managements/update_user.html.erb b/app/views/managements/update_user.html.erb index a2ad1369..4ef8e9e6 100644 --- a/app/views/managements/update_user.html.erb +++ b/app/views/managements/update_user.html.erb @@ -21,11 +21,11 @@ 粉丝 <%= @user_fanlist_count %> - + 金币 <%= @user.grade %> - + 经验值 <%= @user.experience %> From 319b3c1d0eeec23d0d43b6c8163b5693e97d1fc6 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Mon, 3 Jun 2019 11:18:45 +0800 Subject: [PATCH 04/12] fix management school grow data incorrent --- .../management/school_data_grow_service.rb | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/app/services/management/school_data_grow_service.rb b/app/services/management/school_data_grow_service.rb index 7afe87af..e86ba52e 100644 --- a/app/services/management/school_data_grow_service.rb +++ b/app/services/management/school_data_grow_service.rb @@ -21,14 +21,14 @@ class Management::SchoolDataGrowService reports = reports.select( '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,'\ - 'SUM(shixun_increase_count) shixun_increase_count,'\ - 'SUM(shixun_homework_count) shixun_homework_count,'\ - 'SUM(shixun_evaluate_count) shixun_evaluate_count,'\ - 'COUNT(distinct(au.user_id)) uniq_active_user_count,'\ - 'SUM(active_user_count) active_user_count' + 'SUM(distinct teacher_increase_count) teacher_increase_count,'\ + 'SUM(distinct student_increase_count) student_increase_count,'\ + 'SUM(distinct course_increase_count) course_increase_count,'\ + 'SUM(distinct shixun_increase_count) shixun_increase_count,'\ + 'SUM(distinct shixun_homework_count) shixun_homework_count,'\ + 'SUM(distinct shixun_evaluate_count) shixun_evaluate_count,'\ + 'COUNT(distinct au.user_id) uniq_active_user_count,'\ + 'SUM(distinct active_user_count) active_user_count' ) reports = custom_sort(reports, params[:sort_by], params[:sort_direction]) @@ -40,14 +40,14 @@ class Management::SchoolDataGrowService def grow_summary @_grow_summary ||= begin query_reports.select( - 'SUM(teacher_increase_count) teacher_increase_count,'\ - '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,'\ - 'COUNT(distinct(au.user_id)) uniq_active_user_count,'\ - 'SUM(active_user_count) active_user_count' + 'SUM(distinct teacher_increase_count) teacher_increase_count,'\ + 'SUM(distinct student_increase_count) student_increase_count,'\ + 'SUM(distinct course_increase_count) course_increase_count,'\ + 'SUM(distinct shixun_increase_count) shixun_increase_count,'\ + 'SUM(distinct shixun_homework_count) shixun_homework_count,'\ + 'SUM(distinct shixun_evaluate_count) shixun_evaluate_count,'\ + 'COUNT(distinct au.user_id) uniq_active_user_count,'\ + 'SUM(distinct active_user_count) active_user_count' ).first end end From 0669dcf907ec79533c9d8c80426ff856a158a0bb Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Mon, 3 Jun 2019 14:11:25 +0800 Subject: [PATCH 05/12] =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/mobile/api.rb | 5 +++++ app/views/layouts/base_edu.html.erb | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/api/mobile/api.rb b/app/api/mobile/api.rb index e93c374c..af1b770d 100644 --- a/app/api/mobile/api.rb +++ b/app/api/mobile/api.rb @@ -83,6 +83,9 @@ module Mobile end def memo_authenticate! + Rails.logger.info("#######current_user: ###{current_user}") + Rails.logger.info("#######session: ###{session[:user_id]}") + pass = (request.path.match(/memos\/\d+/).present? && !request.path.include?("reply")) || request.path.include?("get_memos_list") || request.path.include?("memos?page=") || request.path.match(/memos$/).present? @@ -94,6 +97,8 @@ module Mobile end def discusses_authenticate! + Rails.logger.info("#######current_user: ###{current_user}") + Rails.logger.info("#######session: ###{session[:user_id]}") pass = request.path.match(/discusses$/).present? || request.path.include?("discusses?page=") unless pass error!('401 Unauthorized', 401) unless current_user diff --git a/app/views/layouts/base_edu.html.erb b/app/views/layouts/base_edu.html.erb index 2638a1df..06fa90b9 100644 --- a/app/views/layouts/base_edu.html.erb +++ b/app/views/layouts/base_edu.html.erb @@ -24,7 +24,8 @@ <%= javascript_include_tag '/editormd/lib/marked.min.js', '/editormd/lib/prettify.min.js', '/editormd/lib/raphael.min.js', '/editormd/lib/underscore.min.js', '/editormd/lib/sequence-diagram.min.js', '/editormd/lib/flowchart.min.js', '/editormd/lib/jquery.flowchart.min.js', '/editormd/editormd.js' %> <%= yield :header_tags -%> - + <% Rails.logger.info("########----current_user: #{User.current.id}") %> + <% Rails.logger.info("########----session: #{session[:user_id]}") %> From 55ae6091a7cfe810ad640c6c297e932a1fe234e1 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Mon, 3 Jun 2019 14:17:54 +0800 Subject: [PATCH 06/12] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E9=80=80=E5=87=BA?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/mobile/api.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/api/mobile/api.rb b/app/api/mobile/api.rb index af1b770d..5897124e 100644 --- a/app/api/mobile/api.rb +++ b/app/api/mobile/api.rb @@ -34,7 +34,7 @@ module Mobile version 'v1', using: :path format :json content_type :json, "application/json;charset=UTF-8" - use ActionDispatch::Session::CookieStore + #use ActionDispatch::Session::CookieStore require 'digest' use Mobile::Middleware::ErrorHandler From a53d2d342310ab0a20cba8f81dd64ecaad0f0ca9 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Mon, 3 Jun 2019 15:06:50 +0800 Subject: [PATCH 07/12] 1 --- app/api/mobile/api.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/api/mobile/api.rb b/app/api/mobile/api.rb index 5897124e..95552667 100644 --- a/app/api/mobile/api.rb +++ b/app/api/mobile/api.rb @@ -34,7 +34,7 @@ module Mobile version 'v1', using: :path format :json content_type :json, "application/json;charset=UTF-8" - #use ActionDispatch::Session::CookieStore + use ActionDispatch::Session::CacheStore :expire_after => 8.hours, :key => '_educoder_session', :domain => :all require 'digest' use Mobile::Middleware::ErrorHandler @@ -139,7 +139,9 @@ module Mobile # Rails.logger.info("########### host is #{request.host}") - if (Rails.env.development? && session[:user_id].blank?) || (session[:user_id].blank? && request.host.include?("testbdweb")) || params[:action] == "privateGit" + if (Rails.env.development? && session[:user_id].blank?) || + (session[:user_id].blank? && request.host.include?("testbdweb")) || + params[:action] == "privateGit" session[:user_id] = 12 #116 end From c90f648219407f300c9d7d3cc90291163289f2bb Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Mon, 3 Jun 2019 15:13:27 +0800 Subject: [PATCH 08/12] 1 --- app/api/mobile/api.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/api/mobile/api.rb b/app/api/mobile/api.rb index 95552667..46914e9f 100644 --- a/app/api/mobile/api.rb +++ b/app/api/mobile/api.rb @@ -34,7 +34,7 @@ module Mobile version 'v1', using: :path format :json content_type :json, "application/json;charset=UTF-8" - use ActionDispatch::Session::CacheStore :expire_after => 8.hours, :key => '_educoder_session', :domain => :all + use ActionDispatch::Session::CookieStore, :expire_after => 8.hours, :key => '_educoder_session', :domain => :all require 'digest' use Mobile::Middleware::ErrorHandler From 4ebe247633ff5620d7ee1e2ee17f7ea90559b921 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Mon, 3 Jun 2019 17:05:38 +0800 Subject: [PATCH 09/12] modify management school report list && fix some bug --- .../managements/schools_controller.rb | 2 +- app/models/school.rb | 1 + app/models/school_report.rb | 3 + .../management/school_data_grow_service.rb | 82 ++++++++++++------- .../management/school_report_service.rb | 14 ++-- .../statistic_school_daily_report_task.rb | 8 +- app/tasks/statistic_school_report_task.rb | 20 +++++ .../schools/_statistics_list.html.erb | 8 +- .../20190603074122_create_school_reports.rb | 14 ++++ lib/tasks/school_report.rake | 8 ++ lib/tasks/statistic_school_daily_report.rake | 9 +- 11 files changed, 119 insertions(+), 50 deletions(-) create mode 100644 app/models/school_report.rb create mode 100644 app/tasks/statistic_school_report_task.rb create mode 100644 db/migrate/20190603074122_create_school_reports.rb create mode 100644 lib/tasks/school_report.rake diff --git a/app/controllers/managements/schools_controller.rb b/app/controllers/managements/schools_controller.rb index 4e9b41ed..74202ad5 100644 --- a/app/controllers/managements/schools_controller.rb +++ b/app/controllers/managements/schools_controller.rb @@ -79,6 +79,6 @@ class Managements::SchoolsController < Managements::BaseController @shixun_homework_total = HomeworkCommon.where(homework_type: 4).count @other_homework_total = HomeworkCommon.where(homework_type: [1, 3]).count @shixun_total = Shixun.count - @shixun_evaluate_total = Game.sum(:evaluate_count) + @shixun_evaluate_total = SchoolReport.sum(:shixun_evaluate_count) end end diff --git a/app/models/school.rb b/app/models/school.rb index 8342039e..d5090023 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -26,6 +26,7 @@ class School < ActiveRecord::Base # 报表信息 has_many :school_daily_reports + has_one :school_report has_many :teacher_extensions, conditions: "identity = #{User::TEACHER}", class_name: 'UserExtensions' has_many :student_extensions, conditions: "identity = #{User::STUDENT}", class_name: 'UserExtensions' diff --git a/app/models/school_report.rb b/app/models/school_report.rb new file mode 100644 index 00000000..1ff8674c --- /dev/null +++ b/app/models/school_report.rb @@ -0,0 +1,3 @@ +class SchoolReport < ActiveRecord::Base + belongs_to :school +end \ No newline at end of file diff --git a/app/services/management/school_data_grow_service.rb b/app/services/management/school_data_grow_service.rb index e86ba52e..4d1b248a 100644 --- a/app/services/management/school_data_grow_service.rb +++ b/app/services/management/school_data_grow_service.rb @@ -15,21 +15,24 @@ class Management::SchoolDataGrowService end def call - reports = query_reports.group('schools.id') + reports = School.where(nil) - count = reports.count.count + reports = search_filter(reports) + count = reports.count + + reports = reports.joins("LEFT JOIN (#{joins_school_daily_report_sql}) sdr ON sdr.school_id = schools.id") reports = reports.select( 'schools.id school_id, schools.name school_name,'\ - 'SUM(distinct teacher_increase_count) teacher_increase_count,'\ - 'SUM(distinct student_increase_count) student_increase_count,'\ - 'SUM(distinct course_increase_count) course_increase_count,'\ - 'SUM(distinct shixun_increase_count) shixun_increase_count,'\ - 'SUM(distinct shixun_homework_count) shixun_homework_count,'\ - 'SUM(distinct shixun_evaluate_count) shixun_evaluate_count,'\ - 'COUNT(distinct au.user_id) uniq_active_user_count,'\ - 'SUM(distinct active_user_count) active_user_count' - ) + 'sdr.teacher_increase_count,'\ + 'sdr.student_increase_count,'\ + 'sdr.course_increase_count,'\ + 'sdr.shixun_increase_count,'\ + 'sdr.shixun_homework_count,'\ + 'sdr.shixun_evaluate_count,'\ + 'sdr.uniq_active_user_count,'\ + 'sdr.active_user_count' + ).group('schools.id') reports = custom_sort(reports, params[:sort_by], params[:sort_direction]) reports = reports.order('school_id asc').limit(PAGE_SIZE).offset(offset) @@ -39,37 +42,54 @@ class Management::SchoolDataGrowService def grow_summary @_grow_summary ||= begin - query_reports.select( - 'SUM(distinct teacher_increase_count) teacher_increase_count,'\ - 'SUM(distinct student_increase_count) student_increase_count,'\ - 'SUM(distinct course_increase_count) course_increase_count,'\ - 'SUM(distinct shixun_increase_count) shixun_increase_count,'\ - 'SUM(distinct shixun_homework_count) shixun_homework_count,'\ - 'SUM(distinct shixun_evaluate_count) shixun_evaluate_count,'\ + reports = School.joins("LEFT JOIN school_daily_reports sdr ON sdr.school_id = schools.id AND #{date_condition_sql}") + .joins('LEFT JOIN school_daily_active_users au ON au.school_daily_report_id = sdr.id') + reports = search_filter(reports) + reports.select( + 'SUM(teacher_increase_count) teacher_increase_count,'\ + '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,'\ 'COUNT(distinct au.user_id) uniq_active_user_count,'\ - 'SUM(distinct active_user_count) active_user_count' + 'SUM(active_user_count) active_user_count' ).first end end private - def query_reports - 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}") - reports = reports.joins("LEFT JOIN school_daily_active_users au ON au.school_daily_report_id = sdr.id") + def search_filter(relations) keyword = params[:keyword].try(:to_s).try(:strip) if keyword.present? - reports = reports.where("schools.name LIKE :keyword OR schools.id LIKE :keyword", keyword: "%#{keyword}%") + relations = relations.where("schools.name LIKE :keyword OR schools.id LIKE :keyword", keyword: "%#{keyword}%") end - reports + relations + end + + def joins_school_daily_report_sql + SchoolDailyReport.joins('LEFT JOIN school_daily_active_users au ON au.school_daily_report_id = school_daily_reports.id') + .select('school_id, '\ + 'SUM(teacher_increase_count) teacher_increase_count,'\ + '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,'\ + 'COUNT(distinct au.user_id) uniq_active_user_count,'\ + 'SUM(active_user_count) active_user_count') + .where(date_condition_sql).group(:school_id).to_sql + end + + def date_condition_sql + date = query_date + if date.is_a?(Range) + "date BETWEEN '#{date.min.strftime('%Y-%m-%d')}' AND '#{date.max.strftime('%Y-%m-%d')}'" + else + "date = '#{date.strftime('%Y-%m-%d')}'" + end end def query_date diff --git a/app/services/management/school_report_service.rb b/app/services/management/school_report_service.rb index 9837c1d1..5df872fe 100644 --- a/app/services/management/school_report_service.rb +++ b/app/services/management/school_report_service.rb @@ -3,9 +3,8 @@ class Management::SchoolReportService attr_reader :params - # 因为实在太慢了,所以不开放 shixun_evaluate_count 排序 sort_columns :student_count, :teacher_count, :homework_count, :other_homework_count, - :course_count, :active_course_count, :nearly_course_time, :shixun_count, + :course_count, :active_course_count, :nearly_course_time, :shixun_count, :shixun_evaluate_count, default_by: :teacher_count, default_direction: :desc def initialize(params) @@ -50,9 +49,9 @@ class Management::SchoolReportService shixun_map = Shixun.joins(creator: :user_extensions).where(user_extensions: { identity: User::TEACHER, school_id: ids }) .where(fork_from: nil).group('school_id').count - evaluate_count_map = Game.joins(challenge: { shixun: { creator: :user_extensions } }) - .where(user_extensions: { identity: User::TEACHER, school_id: ids }) - .group('school_id').reorder(nil).sum(:evaluate_count) + + reports = SchoolReport.where(school_id: ids) + evaluate_count_map = reports.each_with_object({}) { |report, obj| obj[report.school_id] = report.shixun_evaluate_count } schools.map do |school| { @@ -91,10 +90,7 @@ class Management::SchoolReportService schools.joins(teacher_extensions: :user).joins('INNER JOIN shixuns sx ON sx.user_id = users.id AND sx.fork_from IS NULL') .select("#{base_query_column}, COUNT(*) shixun_count") when 'shixun_evaluate_count' then - schools.joins(teacher_extensions: :user).joins('INNER JOIN shixuns sx ON sx.user_id = users.id') - .joins('LEFT JOIN challenges cs ON cs.shixun_id = sx.id') - .joins('LEFT JOIN games ON games.challenge_id = cs.id') - .select("#{base_query_column}, SUM(games.evaluate_count) shixun_evaluate_count") + schools.joins(:school_report).select("#{base_query_column}, shixun_evaluate_count") when 'nearly_course_time' then schools.joins('LEFT JOIN courses cs ON cs.school_id = schools.id AND cs.is_delete = 0') .joins('LEFT JOIN course_activities acs ON acs.course_id = cs.id') diff --git a/app/tasks/statistic_school_daily_report_task.rb b/app/tasks/statistic_school_daily_report_task.rb index 8a038718..5d0db197 100644 --- a/app/tasks/statistic_school_daily_report_task.rb +++ b/app/tasks/statistic_school_daily_report_task.rb @@ -25,8 +25,12 @@ class StatisticSchoolDailyReportTask .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 }) + shixun_evaluate_count = Output.joins(game: :challenge) + .joins('LEFT JOIN homework_commons_shixuns hcs ON hcs.shixun_id = challenges.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 courses ON members.course_id = courses.id AND hc.course_id = courses.id') + .where(courses: { school_id: school.id }) .where(created_at: yesterday).count # 无有效数据时不记录 diff --git a/app/tasks/statistic_school_report_task.rb b/app/tasks/statistic_school_report_task.rb new file mode 100644 index 00000000..addaa4c5 --- /dev/null +++ b/app/tasks/statistic_school_report_task.rb @@ -0,0 +1,20 @@ +class StatisticSchoolReportTask + def call + School.find_each do |school| + evaluate_count = Game.joins(:challenge) + .joins('LEFT JOIN members ON members.user_id = games.user_id') + .joins('LEFT JOIN homework_commons_shixuns hcs ON hcs.shixun_id = challenges.shixun_id') + .joins('LEFT JOIN homework_commons hc ON hcs.homework_common_id = hc.id AND hc.homework_type = 4') + .joins('LEFT JOIN courses ON hc.course_id = courses.id AND members.course_id = courses.id') + .where(courses: { school_id: school.id }) + .sum(:evaluate_count) + + report = SchoolReport.find_or_initialize_by_school_id(school.id) + + report.school_name = school.name + report.shixun_evaluate_count = evaluate_count + + report.save + end + end +end diff --git a/app/views/managements/schools/_statistics_list.html.erb b/app/views/managements/schools/_statistics_list.html.erb index efea22e8..7fb6300d 100644 --- a/app/views/managements/schools/_statistics_list.html.erb +++ b/app/views/managements/schools/_statistics_list.html.erb @@ -3,15 +3,17 @@ 序号 ID - 单位名称 + 单位名称 <%= sort_tag('教师总人数', name: 'teacher_count', path: school_report_managements_path) %> <%= 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_evaluate_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) %> diff --git a/db/migrate/20190603074122_create_school_reports.rb b/db/migrate/20190603074122_create_school_reports.rb new file mode 100644 index 00000000..d025adce --- /dev/null +++ b/db/migrate/20190603074122_create_school_reports.rb @@ -0,0 +1,14 @@ +class CreateSchoolReports < ActiveRecord::Migration + def change + create_table :school_reports do |t| + t.integer :school_id + t.string :school_name + t.integer :shixun_evaluate_count + + t.timestamps + end + + add_index :school_reports, :school_id + add_index :school_reports, :shixun_evaluate_count + end +end diff --git a/lib/tasks/school_report.rake b/lib/tasks/school_report.rake new file mode 100644 index 00000000..a517123b --- /dev/null +++ b/lib/tasks/school_report.rake @@ -0,0 +1,8 @@ +#coding=utf-8 + +namespace :school_report do + desc 'statistic school report task' + task statistic: :environment do + StatisticSchoolReportTask.new.call + end +end diff --git a/lib/tasks/statistic_school_daily_report.rake b/lib/tasks/statistic_school_daily_report.rake index 88d4881e..1dbb591e 100644 --- a/lib/tasks/statistic_school_daily_report.rake +++ b/lib/tasks/statistic_school_daily_report.rake @@ -183,11 +183,12 @@ namespace :school_daily_report do ) 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 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}" + LEFT JOIN homework_commons_shixuns hcs ON hcs.shixun_id = cls.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}" ) shixun_evaluate_count, ( SELECT COUNT(*) FROM users u LEFT JOIN user_extensions ue ON ue.user_id = u.id From 2aec2cafb78c29784e9abeba588c0c27aa441182 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Mon, 3 Jun 2019 17:41:15 +0800 Subject: [PATCH 10/12] fix bug --- .../management/school_data_grow_service.rb | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/app/services/management/school_data_grow_service.rb b/app/services/management/school_data_grow_service.rb index 4d1b248a..f867ab45 100644 --- a/app/services/management/school_data_grow_service.rb +++ b/app/services/management/school_data_grow_service.rb @@ -46,14 +46,14 @@ class Management::SchoolDataGrowService .joins('LEFT JOIN school_daily_active_users au ON au.school_daily_report_id = sdr.id') reports = search_filter(reports) reports.select( - 'SUM(teacher_increase_count) teacher_increase_count,'\ - '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(distinct teacher_increase_count) teacher_increase_count,'\ + 'SUM(distinct student_increase_count) student_increase_count,'\ + 'SUM(distinct course_increase_count) course_increase_count,'\ + 'SUM(distinct shixun_increase_count) shixun_increase_count,'\ + 'SUM(distinct shixun_homework_count) shixun_homework_count,'\ + 'SUM(distinct shixun_evaluate_count) shixun_evaluate_count,'\ 'COUNT(distinct au.user_id) uniq_active_user_count,'\ - 'SUM(active_user_count) active_user_count' + 'SUM(distinct active_user_count) active_user_count' ).first end end @@ -72,14 +72,14 @@ class Management::SchoolDataGrowService def joins_school_daily_report_sql SchoolDailyReport.joins('LEFT JOIN school_daily_active_users au ON au.school_daily_report_id = school_daily_reports.id') .select('school_id, '\ - 'SUM(teacher_increase_count) teacher_increase_count,'\ - '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(distinct teacher_increase_count) teacher_increase_count,'\ + 'SUM(distinct student_increase_count) student_increase_count,'\ + 'SUM(distinct course_increase_count) course_increase_count,'\ + 'SUM(distinct shixun_increase_count) shixun_increase_count,'\ + 'SUM(distinct shixun_homework_count) shixun_homework_count,'\ + 'SUM(distinct shixun_evaluate_count) shixun_evaluate_count,'\ 'COUNT(distinct au.user_id) uniq_active_user_count,'\ - 'SUM(active_user_count) active_user_count') + 'SUM(distinct active_user_count) active_user_count') .where(date_condition_sql).group(:school_id).to_sql end From 0e94c25b8d81a971e80dbdfe34a885b7d281a198 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Tue, 4 Jun 2019 15:37:04 +0800 Subject: [PATCH 11/12] 1 --- app/services/games_service.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/services/games_service.rb b/app/services/games_service.rb index 22df565d..4d676bef 100644 --- a/app/services/games_service.rb +++ b/app/services/games_service.rb @@ -533,7 +533,6 @@ class GamesService # 更新评测次数 game.update_column(:evaluate_count, (game.evaluate_count.to_i + 1)) - shixun.increment!(:evaluate_count) # 清空代码评测信息 msg = game.run_code_message msg.update_attributes(:status => 0, :message => nil) if msg.present? From c9a38078e44ad575eeccda52339c58fcd29fb4f3 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Tue, 4 Jun 2019 15:48:40 +0800 Subject: [PATCH 12/12] fix management school summary info bug --- .../management/school_data_grow_service.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/services/management/school_data_grow_service.rb b/app/services/management/school_data_grow_service.rb index f867ab45..470a3367 100644 --- a/app/services/management/school_data_grow_service.rb +++ b/app/services/management/school_data_grow_service.rb @@ -46,14 +46,14 @@ class Management::SchoolDataGrowService .joins('LEFT JOIN school_daily_active_users au ON au.school_daily_report_id = sdr.id') reports = search_filter(reports) reports.select( - 'SUM(distinct teacher_increase_count) teacher_increase_count,'\ - 'SUM(distinct student_increase_count) student_increase_count,'\ - 'SUM(distinct course_increase_count) course_increase_count,'\ - 'SUM(distinct shixun_increase_count) shixun_increase_count,'\ - 'SUM(distinct shixun_homework_count) shixun_homework_count,'\ - 'SUM(distinct shixun_evaluate_count) shixun_evaluate_count,'\ + 'SUM(teacher_increase_count) teacher_increase_count,'\ + '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,'\ 'COUNT(distinct au.user_id) uniq_active_user_count,'\ - 'SUM(distinct active_user_count) active_user_count' + 'SUM(active_user_count) active_user_count' ).first end end