Skip to content

Commit

Permalink
Merge pull request #5724 from sarvaiyanidhi/ns_fix_mau_chart
Browse files Browse the repository at this point in the history
Fix Line Chart: MAU (Monthly Active Users) by User Type
  • Loading branch information
compwron committed May 19, 2024
2 parents 582f436 + 9e1fab2 commit 06b5fd4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
16 changes: 8 additions & 8 deletions app/controllers/health_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 13 additions & 9 deletions spec/requests/health_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

0 comments on commit 06b5fd4

Please sign in to comment.