Class: RailsPerformance::Reports::BaseReport

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_performance/reports/base_report.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, group: nil, sort: nil, title: nil) ⇒ BaseReport

Returns a new instance of BaseReport.



6
7
8
9
10
11
12
13
# File 'lib/rails_performance/reports/base_report.rb', line 6

def initialize(db, group: nil, sort: nil, title: nil)
  @db     = db
  @group  = group
  @sort   = sort
  @title  = title

  set_defaults
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



4
5
6
# File 'lib/rails_performance/reports/base_report.rb', line 4

def db
  @db
end

#groupObject (readonly)

Returns the value of attribute group.



4
5
6
# File 'lib/rails_performance/reports/base_report.rb', line 4

def group
  @group
end

#sortObject (readonly)

Returns the value of attribute sort.



4
5
6
# File 'lib/rails_performance/reports/base_report.rb', line 4

def sort
  @sort
end

#titleObject (readonly)

Returns the value of attribute title.



4
5
6
# File 'lib/rails_performance/reports/base_report.rb', line 4

def title
  @title
end

Class Method Details

.time_in_app_time_zone(time) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/rails_performance/reports/base_report.rb', line 24

def self.time_in_app_time_zone(time)
  app_time_zone = ::Rails.application.config.time_zone
  if app_time_zone.present?
    time.in_time_zone(app_time_zone)
  else
    time
  end
end

Instance Method Details

#calculate_dataObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rails_performance/reports/base_report.rb', line 33

def calculate_data
  now        = Time.current
  stop       = Time.at(60 * ((now.to_i)/ 60))
  offset     = RailsPerformance::Reports::BaseReport::time_in_app_time_zone(now).utc_offset
  current    = stop - RailsPerformance.duration

  @data      = []
  all        = {}

  # read current values
  db.group_by(group).each do |(k, v)|
    yield(all, k, v)
  end

  # add blank columns
  while current <= stop
    key   = (current).strftime(RailsPerformance::FORMAT)
    views = all[key].presence || 0
    @data << [(current.to_i + offset) * 1000, views.round(2)]
    current += 1.minute
  end

  # sort by time
  @data.sort!
end

#collectObject



15
16
17
18
19
20
# File 'lib/rails_performance/reports/base_report.rb', line 15

def collect
  db.group_by(group).inject([]) do |res, (k,v)|
    res << yield(k, v)
    res
  end
end

#set_defaultsObject



22
# File 'lib/rails_performance/reports/base_report.rb', line 22

def set_defaults; end