Class: Analytic::Dashboard

Inherits:
Object
  • Object
show all
Defined in:
app/models/analytic/dashboard.rb

Constant Summary collapse

PAGES_LIMIT =
8

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(period:) ⇒ Dashboard

Returns a new instance of Dashboard.

Parameters:

  • period (String)

    today, yesterday, week, month, year



11
12
13
# File 'app/models/analytic/dashboard.rb', line 11

def initialize(period:)
  @period = period
end

Instance Attribute Details

#periodString (readonly)

Returns:

  • (String)


8
9
10
# File 'app/models/analytic/dashboard.rb', line 8

def period
  @period
end

Instance Method Details

#currentPeriod

Returns:



52
53
54
# File 'app/models/analytic/dashboard.rb', line 52

def current
  @current ||= Period.new(range:)
end

#durationInterval

Returns:

  • (Interval)


75
76
77
78
79
80
81
82
83
# File 'app/models/analytic/dashboard.rb', line 75

def duration
  @duration ||=
    case @period
    when '24h' then 24.hours
    when '7d' then 7.days
    when '4w' then 4.weeks
    when '12m' then 12.months
    end
end

#nameString

Returns:

  • (String)


43
44
45
46
47
48
49
# File 'app/models/analytic/dashboard.rb', line 43

def name
  if @period
    "#{@period.capitalize} | Dashboard"
  else
    'Dashboard'
  end
end

#priorPeriod

Returns:



57
58
59
60
61
62
# File 'app/models/analytic/dashboard.rb', line 57

def prior
  return unless range?
  return @prior if defined?(@prior)

  @prior ||= Period.new(range: ((range.min - duration)..(range.max - duration)))
end

#rangeRange<Time>

Returns:

  • (Range<Time>)


70
71
72
# File 'app/models/analytic/dashboard.rb', line 70

def range
  (now - duration)..now if range?
end

#range?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'app/models/analytic/dashboard.rb', line 65

def range?
  @period.present?
end

#sessionsStat

Returns:



34
35
36
37
38
39
40
# File 'app/models/analytic/dashboard.rb', line 34

def sessions
  Stat.new(
    current: current.distinct_sessions_count,
    prior: prior&.distinct_sessions_count,
    name: 'Sessions'
  )
end

#viewsStat

Returns:



16
17
18
19
20
21
22
# File 'app/models/analytic/dashboard.rb', line 16

def views
  Stat.new(
    current: current.count,
    prior: prior&.count,
    name: 'Views'
  )
end

#visitorsStat

Returns:



25
26
27
28
29
30
31
# File 'app/models/analytic/dashboard.rb', line 25

def visitors
  Stat.new(
    current: current.distinct_visitors_count,
    prior: prior&.distinct_visitors_count,
    name: 'Visitors'
  )
end