Class: RequestLog::Profiler

Inherits:
Object
  • Object
show all
Defined in:
lib/request_log/profiler.rb

Class Method Summary collapse

Class Method Details

.attribute_namesObject



16
17
18
# File 'lib/request_log/profiler.rb', line 16

def self.attribute_names
  default_values.keys
end

.attributesObject



66
67
68
69
70
71
# File 'lib/request_log/profiler.rb', line 66

def self.attributes
  attribute_names.inject({}) do |hash, attribute|
    hash[attribute] = send(attribute)
    hash
  end
end

.call(options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/request_log/profiler.rb', line 44

def self.call(options = {})
  if options[:result] == :success
    self.success_count += 1
    update_times(options[:elapsed_time])
  else
    self.failure_count += 1
    if options[:exception]
      failure_exceptions[options[:exception].class.name] ||= 0
      failure_exceptions[options[:exception].class.name] += 1
    end
  end
  persist! if should_persist?
end

.default_valuesObject



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/request_log/profiler.rb', line 3

def self.default_values
  {
    :success_count => 0,
    :failure_count => 0,
    :failure_exceptions => {},
    :min_time => nil,
    :max_time => nil,
    :avg_time => nil,
    :persist_enabled => false,
    :persist_frequency => 2000
  }
end

.failure_ratioObject



62
63
64
# File 'lib/request_log/profiler.rb', line 62

def self.failure_ratio
  failure_count.to_f/total_count
end

.persist!Object



73
74
75
76
77
78
79
80
81
# File 'lib/request_log/profiler.rb', line 73

def self.persist!
  ::RequestLog::Db.profiling.insert(
    :total_count => total_count,
    :failure_ratio => failure_ratio,
    :max_time => max_time,
    :avg_time => avg_time,
    :data => attributes
  )
end

.resetObject



35
36
37
38
39
40
# File 'lib/request_log/profiler.rb', line 35

def self.reset
  attribute_names.each do |attribute|
    send("#{attribute}=", default_values[attribute])
  end
  self
end

.should_persist?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/request_log/profiler.rb', line 83

def self.should_persist?
  persist_enabled && (total_count % persist_frequency == 0)
end

.to_sObject



87
88
89
# File 'lib/request_log/profiler.rb', line 87

def self.to_s
  attributes.inspect
end

.total_countObject



58
59
60
# File 'lib/request_log/profiler.rb', line 58

def self.total_count
  success_count + failure_count
end