Class: Rectify::RSpec::DatabaseReporter::QueryStats

Inherits:
Object
  • Object
show all
Defined in:
lib/rectify/rspec/database_reporter/query_stats.rb

Instance Method Summary collapse

Constructor Details

#initializeQueryStats

Returns a new instance of QueryStats.



5
6
7
# File 'lib/rectify/rspec/database_reporter/query_stats.rb', line 5

def initialize
  @stats = Hash.new { |h, k| h[k] = [] }
end

Instance Method Details

#add(example, start, finish, query) ⇒ Object



9
10
11
12
13
14
# File 'lib/rectify/rspec/database_reporter/query_stats.rb', line 9

def add(example, start, finish, query)
  info = QueryInfo.new(example, start, finish, query)
  return if info.ignore?

  stats[info.target] << info
end

#eachObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/rectify/rspec/database_reporter/query_stats.rb', line 16

def each
  stats.sort.each do |target, infos|
    yield(
      target,
      infos.first.type,
      infos.count,
      infos.sum(&:time).round(5)
    )
  end
end

#empty?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/rectify/rspec/database_reporter/query_stats.rb', line 41

def empty?
  stats.empty?
end

#longest_targetObject



35
36
37
38
39
# File 'lib/rectify/rspec/database_reporter/query_stats.rb', line 35

def longest_target
  return 0 if stats.empty?

  stats.keys.max_by(&:length).length
end

#total_queriesObject



27
28
29
# File 'lib/rectify/rspec/database_reporter/query_stats.rb', line 27

def total_queries
  stats.values.flatten.count
end

#total_timeObject



31
32
33
# File 'lib/rectify/rspec/database_reporter/query_stats.rb', line 31

def total_time
  stats.values.flatten.sum(&:time).round(5)
end