Class: RailsLocalAnalytics::DashboardController

Inherits:
ApplicationController show all
Defined in:
app/controllers/rails_local_analytics/dashboard_controller.rb

Constant Summary collapse

PER_PAGE_LIMIT =
1000

Instance Method Summary collapse

Methods inherited from ApplicationController

#root

Instance Method Details

#differenceObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/rails_local_analytics/dashboard_controller.rb', line 53

def difference
  case params.require(:type)
  when "site"
    @klass = TrackedRequestsByDaySite
  when "page"
    @klass = TrackedRequestsByDayPage
  end

  start_date = Date.parse(params.require(:start_date))
  end_date = Date.parse(params.require(:end_date))

  prev_start_date, prev_end_date = get_prev_dates(start_date, end_date)

  where_conditions = params.require(:conditions).permit(*@klass.display_columns)

  current_total = fetch_records(
    start_date,
    end_date,
    where_conditions: where_conditions,
    pluck_columns: ["SUM(total)"],
  ).first

  prev_total = fetch_records(
    prev_start_date,
    prev_end_date,
    where_conditions: where_conditions,
    pluck_columns: ["SUM(total)"],
  ).first

  if prev_total
    diff = current_total - prev_total
  else
    diff = current_total
  end

  render json: {difference: diff}
end

#indexObject



7
8
9
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
# File 'app/controllers/rails_local_analytics/dashboard_controller.rb', line 7

def index
  params[:type] ||= "page"

  case params[:type]
  when "site"
    @klass = TrackedRequestsByDaySite
  when "page"
    @klass = TrackedRequestsByDayPage
  else
    head 404
    return
  end

  if params[:group_by].present? && !@klass.display_columns.include?(params[:group_by])
    params[:group_by] = nil
  end

  if params[:start_date].present?
    @start_date = Date.parse(params[:start_date])
  else
    @start_date = Date.today
  end

  if params[:end_date]
    @end_date = Date.parse(params[:end_date])
  else
    @end_date = Date.today
  end

  if @end_date < @start_date
    @end_date = @start_date
  end

  @results = fetch_records(@start_date, @end_date)

  if @results.size < PER_PAGE_LIMIT
    prev_start_date, prev_end_date = get_prev_dates(@start_date, @end_date)

    @prev_period_results = fetch_records(prev_start_date, prev_end_date)

    if @prev_period_results.size >= PER_PAGE_LIMIT
      @prev_period_results = nil
    end
  end
end