From c389f0f2a620eab27356334da98f021f9e98d83d Mon Sep 17 00:00:00 2001 From: p31729568 Date: Fri, 31 May 2019 10:18:17 +0800 Subject: [PATCH] modify school active user count --- app/models/school_daily_active_user.rb | 3 +++ app/models/school_daily_report.rb | 2 ++ app/services/management/school_data_grow_service.rb | 5 ++++- app/tasks/statistic_school_daily_report_task.rb | 9 +++++++-- app/views/managements/schools/_data_grow_list.html.erb | 8 +++++--- .../20190531011258_create_school_daily_active_users.rb | 10 ++++++++++ 6 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 app/models/school_daily_active_user.rb create mode 100644 db/migrate/20190531011258_create_school_daily_active_users.rb diff --git a/app/models/school_daily_active_user.rb b/app/models/school_daily_active_user.rb new file mode 100644 index 00000000..0edd00fa --- /dev/null +++ b/app/models/school_daily_active_user.rb @@ -0,0 +1,3 @@ +class SchoolDailyActiveUser < ActiveRecord::Base + belongs_to :school_daily_report +end \ No newline at end of file diff --git a/app/models/school_daily_report.rb b/app/models/school_daily_report.rb index 494eacfd..6b24d8be 100644 --- a/app/models/school_daily_report.rb +++ b/app/models/school_daily_report.rb @@ -1,3 +1,5 @@ class SchoolDailyReport < ActiveRecord::Base belongs_to :school + + has_many :school_daily_active_users, dependent: :delete_all 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 44fc45e1..7afe87af 100644 --- a/app/services/management/school_data_grow_service.rb +++ b/app/services/management/school_data_grow_service.rb @@ -6,7 +6,7 @@ class Management::SchoolDataGrowService attr_reader :params sort_columns :teacher_increase_count, :student_increase_count, - :course_increase_count, :shixun_increase_count, :active_user_count, + :course_increase_count, :shixun_increase_count, :uniq_active_user_count, :shixun_homework_count, :shixun_evaluate_count, default_by: :teacher_increase_count, default_direction: :desc @@ -27,6 +27,7 @@ class Management::SchoolDataGrowService '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' ) @@ -45,6 +46,7 @@ class Management::SchoolDataGrowService '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' ).first end @@ -60,6 +62,7 @@ class Management::SchoolDataGrowService 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") keyword = params[:keyword].try(:to_s).try(:strip) if keyword.present? diff --git a/app/tasks/statistic_school_daily_report_task.rb b/app/tasks/statistic_school_daily_report_task.rb index 440d4a97..8e5103a3 100644 --- a/app/tasks/statistic_school_daily_report_task.rb +++ b/app/tasks/statistic_school_daily_report_task.rb @@ -9,7 +9,9 @@ class StatisticSchoolDailyReportTask student_count = users.where(created_on: yesterday, user_extensions: { identity: User::STUDENT }).count # 活跃用户 - active_user_count = users.where(last_login_on: yesterday).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 @@ -38,7 +40,10 @@ class StatisticSchoolDailyReportTask 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 } - SchoolDailyReport.create!(create_params) + report = SchoolDailyReport.create!(create_params) + 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 diff --git a/app/views/managements/schools/_data_grow_list.html.erb b/app/views/managements/schools/_data_grow_list.html.erb index 51371f4e..c69ca60b 100644 --- a/app/views/managements/schools/_data_grow_list.html.erb +++ b/app/views/managements/schools/_data_grow_list.html.erb @@ -12,7 +12,9 @@ 新增实训<%= @grow_summary.shixun_increase_count || 0 %>个, 新增实训作业<%= @grow_summary.shixun_homework_count || 0 %>个, 新增实训评测<%= @grow_summary.shixun_evaluate_count || 0 %>个, - 活跃用户<%= @grow_summary.active_user_count || 0 %>个 + 活跃用户 + <%= @grow_summary.uniq_active_user_count.to_i.zero? ? @grow_summary.active_user_count.to_i : @grow_summary.uniq_active_user_count.to_i %> + 个 @@ -27,7 +29,7 @@ - + @@ -42,7 +44,7 @@ - + <% end %> diff --git a/db/migrate/20190531011258_create_school_daily_active_users.rb b/db/migrate/20190531011258_create_school_daily_active_users.rb new file mode 100644 index 00000000..e03ef5e9 --- /dev/null +++ b/db/migrate/20190531011258_create_school_daily_active_users.rb @@ -0,0 +1,10 @@ +class CreateSchoolDailyActiveUsers < ActiveRecord::Migration + def change + create_table :school_daily_active_users do |t| + t.integer :school_daily_report_id + t.integer :user_id + end + + add_index :school_daily_active_users, :school_daily_report_id + end +end
<%= sort_tag('新增实训', name: 'shixun_increase_count', path: school_data_grow_managements_path) %> <%= sort_tag('新增实训作业', name: 'shixun_homework_count', path: school_data_grow_managements_path) %> <%= sort_tag('新增实训评测', name: 'shixun_evaluate_count', path: school_data_grow_managements_path) %><%= sort_tag('活跃用户', name: 'active_user_count', path: school_data_grow_managements_path) %><%= sort_tag('活跃用户', name: 'uniq_active_user_count', path: school_data_grow_managements_path) %>
<%= report.shixun_increase_count.to_i %> <%= report.shixun_homework_count.to_i %> <%= report.shixun_evaluate_count.to_i %><%= report.active_user_count.to_i %><%= report.uniq_active_user_count.to_i.zero? ? report.active_user_count.to_i : report.uniq_active_user_count.to_i %>