Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update returning users data dates #749

Merged
merged 18 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/data/high_fail_questions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def high_fail_questions
total_count = question_attempts[[module_name, question_name]]
fail_rate = fail_count / total_count

if fail_rate > average_fail_rate
if fail_rate >= average_fail_rate
high_fail_questions[[module_name, question_name]] = fail_rate.to_f
end
end
Expand Down
8 changes: 4 additions & 4 deletions app/models/data/module_overview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def dashboard
mods.map do |mod|
{
module_name: mod.name,
total_users: User.count,
total_users: User.not_closed.registration_complete.count,
not_started: not_started(mod),
started: started(mod),
in_progress: in_progress(mod),
Expand Down Expand Up @@ -62,15 +62,15 @@ def true_false_count(mod)
end

def not_started(mod)
User.all.map { |u| u.module_time_to_completion[mod.name] }.count(&:nil?)
User.not_closed.registration_complete.all.map { |u| u.module_time_to_completion[mod.name] }.count(&:nil?)
end

def in_progress(mod)
User.all.map { |u| u.module_time_to_completion[mod.name] }.compact.count(&:zero?)
User.not_closed.all.map { |u| u.module_time_to_completion[mod.name] }.compact.count(&:zero?)
end

def completed(mod)
User.all.map { |u| u.module_time_to_completion[mod.name] }.compact.count(&:positive?)
User.not_closed.all.map { |u| u.module_time_to_completion[mod.name] }.compact.count(&:positive?)
end

def started(mod)
Expand Down
34 changes: 10 additions & 24 deletions app/models/data/returning_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ class ReturningUsers
class << self
# @return [Array<String>]
def column_names
[
'Weekly Returning Users',
'Monthly Returning Users',
'Bimonthly Returning Users',
'Quarterly Returning Users',
]
['Weekly Returning Users',
'Monthly Returning Users',
'Quarterly Returning Users']
end

# @return [Array<Hash{Symbol => Mixed}>]
Expand All @@ -19,44 +16,33 @@ def dashboard
{
weekly: weekly,
monthly: monthly,
bimonthly: bimonthly,
quarterly: quarterly,
},
]
end

private

# @param previous [Range]
# @param current [Range]
# @param [Range<Time>] time_range
# @return [Integer]
def count(previous:, current:)
previous_visits = Ahoy::Visit.where(started_at: previous)
current_visits = Ahoy::Visit.where(started_at: current)

previous_visits.where(user_id: current_visits.pluck(:user_id)).distinct.count(:user_id)
def visits_count(time_range)
visits = Ahoy::Visit.where(started_at: time_range)
visits.group(:user_id).having('count(user_id) > 1').count.size
end

# @return [Integer]
def weekly
count(previous: 2.weeks.ago..1.week.ago, current: 1.week.ago..Time.zone.now)
visits_count(Time.zone.today.last_week.beginning_of_week..Time.zone.today.last_week.end_of_week)
end

# @return [Integer]
def monthly
count(previous: 2.months.ago..1.month.ago, current: 1.month.ago..Time.zone.now)
end

# @return [Integer]
def bimonthly
count(previous: 4.months.ago..2.months.ago, current: 2.months.ago..Time.zone.now)
visits_count(Time.zone.today.last_month.beginning_of_month..Time.zone.today.last_month.end_of_month)
end

# @return [Integer]
def quarterly
current = Time.zone.today.beginning_of_quarter
previous = current - 3.months
count(previous: previous..current, current: current..Time.zone.today)
visits_count(Time.zone.today.beginning_of_quarter..Time.zone.now)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/data/user_module_completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def dashboard
{
module_name: mod.name,
completed_count: completed_count,
completed_percentage: (completed_count / User.count.to_f),
completed_percentage: (completed_count / User.registration_complete.count.to_f),
}
end
end
Expand All @@ -29,7 +29,7 @@ def dashboard
# @param mod_name [String]
# @return [Integer]
def module_count(mod_name)
User.all.count { |user| user.module_completed?(mod_name) }
User.registration_complete.count { |user| user.module_completed?(mod_name) }
peterdavidhamilton marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/data/user_module_completion_count.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def module_count
# @param count [Integer]
# @return [Integer]
def completed_count(count)
User.all.count { |user| user.modules_completed.eql?(count) }
User.registration_complete.count { |user| user.modules_completed.eql?(count) }
end
end
end
Expand Down
23 changes: 9 additions & 14 deletions spec/models/data/returning_users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@

RSpec.describe Data::ReturningUsers do
let(:headers) do
[
'Weekly Returning Users',
'Monthly Returning Users',
'Bimonthly Returning Users',
'Quarterly Returning Users',
]
['Weekly Returning Users',
'Monthly Returning Users',
'Quarterly Returning Users']
end

let(:rows) do
[
{
weekly: 1,
monthly: 1,
bimonthly: 1,
quarterly: 1,
},
]
Expand All @@ -24,13 +20,12 @@
let(:user) { create :user, :registered }

before do
Ahoy::Visit.create! id: 1, user_id: user.id, started_at: Time.zone.now
Ahoy::Visit.create! id: 9, user_id: user.id, started_at: 1.day.ago
Ahoy::Visit.create! id: 2, user_id: user.id, started_at: 1.week.ago
Ahoy::Visit.create! id: 3, user_id: user.id, started_at: 2.weeks.ago
Ahoy::Visit.create! id: 4, user_id: user.id, started_at: 1.month.ago
Ahoy::Visit.create! id: 5, user_id: user.id, started_at: 2.months.ago
Ahoy::Visit.create! id: 8, user_id: user.id, started_at: 3.months.ago.beginning_of_quarter
Ahoy::Visit.create!(id: 1, user_id: 1, started_at: Time.zone.today.last_week)
Ahoy::Visit.create!(id: 2, user_id: 1, started_at: Time.zone.today.last_week)
Ahoy::Visit.create!(id: 3, user_id: 1, started_at: Time.zone.today.last_month)
Ahoy::Visit.create!(id: 4, user_id: 1, started_at: Time.zone.today.last_month)
Ahoy::Visit.create!(id: 5, user_id: 1, started_at: Time.zone.today.beginning_of_quarter)
Ahoy::Visit.create!(id: 6, user_id: 1, started_at: Time.zone.today.beginning_of_quarter)
end

it_behaves_like 'a data export model'
Expand Down
1 change: 1 addition & 0 deletions spec/models/data/user_module_completion_count_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
end

before do
create :user, :confirmed
create :user, :registered, module_time_to_completion: { alpha: 1 }
create :user, :registered, module_time_to_completion: { alpha: 1, charlie: 1 }
create :user, :registered, module_time_to_completion: { alpha: 0 }
Expand Down
1 change: 1 addition & 0 deletions spec/models/data/user_module_completion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
end

before do
create :user, :confirmed
create :user, :registered
create :user, :registered, module_time_to_completion: { alpha: 0 }
create :user, :registered, module_time_to_completion: { alpha: 1 }
Expand Down
Loading