From b95647c04789ceed686a477f853fa17ac14c99e5 Mon Sep 17 00:00:00 2001 From: Nidhi Sarvaiya Date: Fri, 17 May 2024 20:01:11 -0400 Subject: [PATCH 1/2] Update queries for monthly_unique_users_graph_data to fetch data from login_activity table --- app/controllers/health_controller.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/controllers/health_controller.rb b/app/controllers/health_controller.rb index 040a5fd41d..a31697be6a 100644 --- a/app/controllers/health_controller.rb +++ b/app/controllers/health_controller.rb @@ -40,18 +40,18 @@ def monthly_line_graph_data end def monthly_unique_users_graph_data - first_day_of_last_12_months = (12.months.ago.to_date..Date.current).select { |date| date.day == 1 }.map { |date| date.beginning_of_month } + first_day_of_last_12_months = (12.months.ago.to_date..Date.current).select { |date| date.day == 1 }.map { |date| date.beginning_of_month.strftime("%b %Y") } - monthly_counts_of_volunteers = User.where(type: "Volunteer").group_by_month(:current_sign_in_at, format: "%b %Y").count - monthly_counts_of_supervisors = User.where(type: "Supervisor").group_by_month(:current_sign_in_at, format: "%b %Y").count - monthly_counts_of_casa_admins = User.where(type: "CasaAdmin").group_by_month(:current_sign_in_at, format: "%b %Y").count + monthly_counts_of_volunteers = LoginActivity.joins("INNER JOIN users ON users.id = login_activities.user_id AND login_activities.user_type = 'User'").where(users: { type: "Volunteer" }, success: true).group_by_month(:created_at, format: "%b %Y").distinct.count(:user_id) + monthly_counts_of_supervisors = LoginActivity.joins("INNER JOIN users ON users.id = login_activities.user_id AND login_activities.user_type = 'User'").where(users: { type: "Supervisor" }, success: true).group_by_month(:created_at, format: "%b %Y").distinct.count(:user_id) + monthly_counts_of_casa_admins = LoginActivity.joins("INNER JOIN users ON users.id = login_activities.user_id AND login_activities.user_type = 'User'").where(users: { type: "CasaAdmin" }, success: true).group_by_month(:created_at, format: "%b %Y").distinct.count(:user_id) monthly_line_graph_combined_data = first_day_of_last_12_months.map do |month| [ - month.strftime("%b %Y"), - monthly_counts_of_volunteers[month.strftime("%b %Y")] || 0, - monthly_counts_of_supervisors[month.strftime("%b %Y")] || 0, - monthly_counts_of_casa_admins[month.strftime("%b %Y")] || 0 + month, + monthly_counts_of_volunteers[month] || 0, + monthly_counts_of_supervisors[month] || 0, + monthly_counts_of_casa_admins[month] || 0 ] end From 9e1fab26f7ad4d588bd161eee020b8dcaa250944 Mon Sep 17 00:00:00 2001 From: Nidhi Sarvaiya Date: Fri, 17 May 2024 23:17:52 -0400 Subject: [PATCH 2/2] Updated specs for monthly_unique_users_graph_data health chart --- app/controllers/health_controller.rb | 6 +++--- spec/requests/health_spec.rb | 22 +++++++++++++--------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/app/controllers/health_controller.rb b/app/controllers/health_controller.rb index a31697be6a..aa587b2562 100644 --- a/app/controllers/health_controller.rb +++ b/app/controllers/health_controller.rb @@ -42,9 +42,9 @@ def monthly_line_graph_data def monthly_unique_users_graph_data first_day_of_last_12_months = (12.months.ago.to_date..Date.current).select { |date| date.day == 1 }.map { |date| date.beginning_of_month.strftime("%b %Y") } - monthly_counts_of_volunteers = LoginActivity.joins("INNER JOIN users ON users.id = login_activities.user_id AND login_activities.user_type = 'User'").where(users: { type: "Volunteer" }, success: true).group_by_month(:created_at, format: "%b %Y").distinct.count(:user_id) - monthly_counts_of_supervisors = LoginActivity.joins("INNER JOIN users ON users.id = login_activities.user_id AND login_activities.user_type = 'User'").where(users: { type: "Supervisor" }, success: true).group_by_month(:created_at, format: "%b %Y").distinct.count(:user_id) - monthly_counts_of_casa_admins = LoginActivity.joins("INNER JOIN users ON users.id = login_activities.user_id AND login_activities.user_type = 'User'").where(users: { type: "CasaAdmin" }, success: true).group_by_month(:created_at, format: "%b %Y").distinct.count(:user_id) + monthly_counts_of_volunteers = LoginActivity.joins("INNER JOIN users ON users.id = login_activities.user_id AND login_activities.user_type = 'User'").where(users: {type: "Volunteer"}, success: true).group_by_month(:created_at, format: "%b %Y").distinct.count(:user_id) + monthly_counts_of_supervisors = LoginActivity.joins("INNER JOIN users ON users.id = login_activities.user_id AND login_activities.user_type = 'User'").where(users: {type: "Supervisor"}, success: true).group_by_month(:created_at, format: "%b %Y").distinct.count(:user_id) + monthly_counts_of_casa_admins = LoginActivity.joins("INNER JOIN users ON users.id = login_activities.user_id AND login_activities.user_type = 'User'").where(users: {type: "CasaAdmin"}, success: true).group_by_month(:created_at, format: "%b %Y").distinct.count(:user_id) monthly_line_graph_combined_data = first_day_of_last_12_months.map do |month| [ diff --git a/spec/requests/health_spec.rb b/spec/requests/health_spec.rb index be6e5da402..2bd8afe57f 100644 --- a/spec/requests/health_spec.rb +++ b/spec/requests/health_spec.rb @@ -75,14 +75,19 @@ describe "GET #monthly_unique_users_graph_data" do it "returns monthly unique users data for volunteers, supervisors, and admins in the last year" do - create(:user, type: "Volunteer", current_sign_in_at: 11.months.ago) - create(:user, type: "Volunteer", current_sign_in_at: 11.months.ago) - create(:user, type: "Supervisor", current_sign_in_at: 11.months.ago) - create(:user, type: "CasaAdmin", current_sign_in_at: 11.months.ago) - create(:user, type: "Volunteer", current_sign_in_at: 10.months.ago) - create(:user, type: "Volunteer", current_sign_in_at: 9.months.ago) - create(:user, type: "Supervisor", current_sign_in_at: 9.months.ago) - create(:user, type: "CasaAdmin", current_sign_in_at: 9.months.ago) + volunteer1 = create(:user, type: "Volunteer") + volunteer2 = create(:user, type: "Volunteer") + supervisor = create(:user, type: "Supervisor") + casa_admin = create(:user, type: "CasaAdmin") + + create(:login_activity, user: volunteer1, created_at: 11.months.ago, success: true) + create(:login_activity, user: volunteer2, created_at: 11.months.ago, success: true) + create(:login_activity, user: supervisor, created_at: 11.months.ago, success: true) + create(:login_activity, user: casa_admin, created_at: 11.months.ago, success: true) + create(:login_activity, user: volunteer1, created_at: 10.months.ago, success: true) + create(:login_activity, user: volunteer2, created_at: 9.months.ago, success: true) + create(:login_activity, user: supervisor, created_at: 9.months.ago, success: true) + create(:login_activity, user: casa_admin, created_at: 9.months.ago, success: true) get monthly_unique_users_graph_data_health_index_path expect(response).to have_http_status(:ok) @@ -95,7 +100,6 @@ expect(chart_data[0]).to eq([11.months.ago.strftime("%b %Y"), 2, 1, 1]) expect(chart_data[1]).to eq([10.months.ago.strftime("%b %Y"), 1, 0, 0]) expect(chart_data[2]).to eq([9.months.ago.strftime("%b %Y"), 1, 1, 1]) - expect(chart_data[3]).to eq([8.months.ago.strftime("%b %Y"), 0, 0, 0]) end end end