Class: Lookout::PropertiesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/lookout/properties_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#period

Methods included from Rangeable

#period

Methods included from CompareMode

#compare_mode?, #comparison_mode, included

Instance Method Details

#indexObject



7
8
9
10
11
12
13
14
15
16
# File 'app/controllers/lookout/properties_controller.rb', line 7

def index
  # Auto-select 'url' property if it exists, otherwise first property
  if @options.any?
    # Prefer 'url' property
    url_key = @options.find { |key, value| value == 'url' }&.first
    default_key = url_key || @options.keys.first
    
    redirect_to property_path(id: default_key, **request.query_parameters), status: :see_other
  end
end

#showObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/lookout/properties_controller.rb', line 18

def show
  value = Base64.urlsafe_decode64(params[:id])
  json_extract = Lookout::DatabaseAdapter.json_extract_text("properties", value)
  coalesce_expr = "COALESCE(#{json_extract}, '(none)')"

  # Handle percentage calculation differently for SQLite vs PostgreSQL
  percentage_calc = if Lookout::DatabaseAdapter.postgresql?
    "(COUNT(DISTINCT visit_id)/COUNT(*)::numeric) * 100"
  else
    "(CAST(COUNT(DISTINCT visit_id) AS REAL) / COUNT(*)) * 100"
  end

  @properties = event_query
    .select(
      "#{coalesce_expr} AS label",
      "COUNT(*) AS events_count",
      "COUNT(DISTINCT visit_id) AS unique_visitors_count",
      "#{percentage_calc} as percentage"
    )
    .group(coalesce_expr)
    .order(Arel.sql "COUNT(*) desc")
end