Class: OnboardOnRails::StatsCalculator

Inherits:
Object
  • Object
show all
Defined in:
app/services/onboard_on_rails/stats_calculator.rb

Instance Method Summary collapse

Constructor Details

#initialize(tour) ⇒ StatsCalculator

Returns a new instance of StatsCalculator.



3
4
5
# File 'app/services/onboard_on_rails/stats_calculator.rb', line 3

def initialize(tour)
  @tour = tour
end

Instance Method Details

#ab_breakdownObject



28
29
30
31
32
33
34
35
36
37
# File 'app/services/onboard_on_rails/stats_calculator.rb', line 28

def ab_breakdown
  return [] if @tour.ab_test_id.blank?
  @tour.completions.group(:ab_group).select(
    "ab_group", "COUNT(*) as total",
    "COUNT(CASE WHEN status = 'completed' THEN 1 END) as completed_count"
  ).map do |row|
    { group: row.ab_group, total: row.total, completed: row.completed_count,
      rate: row.total > 0 ? (row.completed_count.to_f / row.total * 100).round(1) : 0 }
  end
end

#drop_off_per_stepObject



21
22
23
24
25
26
# File 'app/services/onboard_on_rails/stats_calculator.rb', line 21

def drop_off_per_step
  @tour.steps.order(:position).map do |step|
    dropped = @tour.completions.where(step: step).where(status: %w[dismissed]).count
    { step_id: step.id, title: step.title, position: step.position, dropped: dropped }
  end
end

#summaryObject



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/services/onboard_on_rails/stats_calculator.rb', line 7

def summary
  completions = @tour.completions
  total = completions.count
  completed = completions.completed.count
  dismissed = completions.dismissed.count
  {
    total_started: total,
    completed: completed,
    dismissed: dismissed,
    in_progress: total - completed - dismissed,
    completion_rate: total > 0 ? (completed.to_f / total * 100).round(1) : 0
  }
end