Class: Base::Summarizer

Inherits:
Object
  • Object
show all
Defined in:
lib/base/summarizer.rb

Overview

Functions to summarize an array of requets. Can calculate request counts, duratations, mean times etc. of all the requests given.

Constant Summary collapse

DEFAULT_BLOCKER_DURATION =
1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Summarizer

Initializer. Sets global variables Options



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/base/summarizer.rb', line 19

def initialize(options = {})
  @actions  = {}
  @blockers = {}
  @errors   = {}
  @request_count = 0
  @blocker_duration = DEFAULT_BLOCKER_DURATION
  @request_time_graph = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
  @methods = {:GET => 0, :POST => 0, :PUT => 0, :DELETE => 0}
  
  self.initialize_hook(options) if self.respond_to?(:initialize_hook)
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



6
7
8
# File 'lib/base/summarizer.rb', line 6

def actions
  @actions
end

#blocker_durationObject

Returns the value of attribute blocker_duration.



14
15
16
# File 'lib/base/summarizer.rb', line 14

def blocker_duration
  @blocker_duration
end

#errorsObject (readonly)

Returns the value of attribute errors.



7
8
9
# File 'lib/base/summarizer.rb', line 7

def errors
  @errors
end

#first_request_atObject (readonly)

Returns the value of attribute first_request_at.



10
11
12
# File 'lib/base/summarizer.rb', line 10

def first_request_at
  @first_request_at
end

#last_request_atObject (readonly)

Returns the value of attribute last_request_at.



11
12
13
# File 'lib/base/summarizer.rb', line 11

def last_request_at
  @last_request_at
end

#methodsObject (readonly)

Returns the value of attribute methods.



12
13
14
# File 'lib/base/summarizer.rb', line 12

def methods
  @methods
end

#request_countObject (readonly)

Returns the value of attribute request_count.



8
9
10
# File 'lib/base/summarizer.rb', line 8

def request_count
  @request_count
end

#request_time_graphObject (readonly)

Returns the value of attribute request_time_graph.



9
10
11
# File 'lib/base/summarizer.rb', line 9

def request_time_graph
  @request_time_graph
end

Instance Method Details

#durationObject

Calculate the duration of a request Returns a DateTime object if possible, 0 otherwise.



38
39
40
# File 'lib/base/summarizer.rb', line 38

def duration
  (@last_request_at && @first_request_at) ? (DateTime.parse(@last_request_at) - DateTime.parse(@first_request_at)).ceil : 0
end

#has_timestamps?Boolean

Check if any of the request parsed had a timestamp.

Returns:

  • (Boolean)


32
33
34
# File 'lib/base/summarizer.rb', line 32

def has_timestamps?
  @first_request_at
end

#request_time_graph?Boolean

Check if the request time graph usable data.

Returns:

  • (Boolean)


43
44
45
# File 'lib/base/summarizer.rb', line 43

def request_time_graph?
  @request_time_graph.uniq != [0] && duration > 0
end

#sort_actions_by(field, min_count = nil) ⇒ Object

Return a list of requests sorted on a specific action field field The action field to sort by. min_count Values which fall below this amount are not returned (default nil).



50
51
52
53
# File 'lib/base/summarizer.rb', line 50

def sort_actions_by(field, min_count = nil)
  actions = min_count.nil? ? @actions.to_a : @actions.delete_if { |k, v| v[:count] < min_count}.to_a
  actions.sort { |a, b| (a[1][field.to_sym] <=> b[1][field.to_sym]) }
end

#sort_blockers_by(field, min_count = @blocker_duration) ⇒ Object

Returns a list of request blockers sorted by a specific field field The action field to sort by. min_count Values which fall below this amount are not returned (default @blocker_duration).



58
59
60
61
# File 'lib/base/summarizer.rb', line 58

def sort_blockers_by(field, min_count = @blocker_duration)
  blockers = min_count.nil? ? @blockers.to_a : @blockers.delete_if { |k, v| v[:count] < min_count}.to_a
  blockers.sort { |a, b| a[1][field.to_sym] <=> b[1][field.to_sym] } 
end

#sort_errors_by(field, min_count = nil) ⇒ Object

Returns a list of request blockers sorted by a specific field field The action field to sort by. min_count Values which fall below this amount are not returned (default @blocker_duration).



66
67
68
69
# File 'lib/base/summarizer.rb', line 66

def sort_errors_by(field, min_count = nil)
  errors = min_count.nil? ? @errors.to_a : @errors.delete_if { |k, v| v[:count] < min_count}.to_a
  errors.sort { |a, b| a[1][field.to_sym] <=> b[1][field.to_sym] } 
end