Class: Lookout::GoalsPresenter

Inherits:
Object
  • Object
show all
Defined in:
app/presenters/lookout/goals_presenter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_query) ⇒ GoalsPresenter

Returns a new instance of GoalsPresenter.



4
5
6
7
# File 'app/presenters/lookout/goals_presenter.rb', line 4

def initialize(event_query)
  @event_query = event_query
  @goals = nil
end

Instance Attribute Details

#goalsObject (readonly)

Returns the value of attribute goals.



3
4
5
# File 'app/presenters/lookout/goals_presenter.rb', line 3

def goals
  @goals
end

Instance Method Details

#as_jsonObject



61
62
63
64
65
66
# File 'app/presenters/lookout/goals_presenter.rb', line 61

def as_json
  {
    steps: @steps.as_json,
    total: total
  }
end

#buildObject

this is a dumpster fire



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/presenters/lookout/goals_presenter.rb', line 10

def build
  if Lookout.config.goals.none?
    @goals = []
    return self
  end

  queries = {
    totals: @event_query.select("count(distinct(#{Lookout.event.table_name}.visit_id)) as unique_visits, '_internal_total_visits_' as name, count(distinct #{Lookout.event.table_name}.id) as total_events, 0 as sort_order")
  }
  selects = ["SELECT unique_visits, name, total_events, sort_order, 0 as cr, '' as goal_id from totals"]
  last_goal = nil
  map = {}.with_indifferent_access

  Lookout.config.goals.each_with_index do |goal, index|
    queries[goal.id] = @event_query.select(
      [
        "count(distinct(#{Lookout.event.table_name}.visit_id)) as unique_visits" ,
        "'#{goal.id}' as name",
        "count(distinct #{Lookout.event.table_name}.id) as total_events",
        "#{index + 1} as sort_order",
        "'#{goal.id}' as goal_id"
      ]
    ).merge(goal.event_query.call).group("#{Lookout.event.table_name}.name")
    # Cast 0 to decimal/real depending on database
    zero_decimal = Lookout::DatabaseAdapter.postgresql? ? "0::decimal" : "0.0"
    selects << ["SELECT unique_visits, name, total_events, sort_order, #{zero_decimal} as cr, '#{goal.id}' as goal_id from #{goal.id}"]
    map[goal.id] = goal
    last_goal = goal
  end

  # activerecord quirk / with bug
  select = selects.join(" UNION ").delete_suffix(" from #{last_goal.id}")
  select = select.delete_prefix("SELECT ")
  steps = ::Ahoy::Event.with(
    queries,
    ).select(select).from("#{last_goal.id}").order("sort_order asc").index_by(&:name)
  totals = steps.delete("_internal_total_visits_")

  @goals = steps.keys.collect do |name|
    step = steps[name]
    step.name = map[name].title
    step.cr = ((step.total_events.to_d / totals.total_events.to_d) * 100).round(2)
    step
  end
  self
end

#to_jsonObject



68
69
70
# File 'app/presenters/lookout/goals_presenter.rb', line 68

def to_json
  as_json.to_json
end

#total_visitorsObject



57
58
59
# File 'app/presenters/lookout/goals_presenter.rb', line 57

def total_visitors
  @total_visitors ||= @event_query.select(:visit_id).distinct.count
end