diff --git a/app/api/mobile/api.rb b/app/api/mobile/api.rb index e93c374c..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::CookieStore + use ActionDispatch::Session::CookieStore, :expire_after => 8.hours, :key => '_educoder_session', :domain => :all require 'digest' use Mobile::Middleware::ErrorHandler @@ -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 @@ -134,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 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/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? diff --git a/app/services/management/school_data_grow_service.rb b/app/services/management/school_data_grow_service.rb index 7afe87af..470a3367 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(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' - ) + '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( + 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,'\ + 'COUNT(distinct au.user_id) uniq_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(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') + .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 406460cc..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) @@ -49,10 +48,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 - 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) + .where(fork_from: nil).group('school_id').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| { @@ -88,13 +87,10 @@ 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') - .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/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]}") %> 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 @@