Class: QueryTraceSummary::EventSummary

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

Constant Summary collapse

ASCII_COLOR_RX =
/\e\[\d+m/
EVENT_RX =
/([A-Z][A-Za-z0-9_:]+) (Create|Load|Update|Destroy) \((\d+(?:\.\d+)?)ms\)/
LOCATION_PREFIX_RX =
/Query Trace:$/
LOCATION_RX =

Assumes conservative file naming

/([a-zA-Z0-9_.\-\/]+:\d+:in `.+')$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, action, location) ⇒ EventSummary

Returns a new instance of EventSummary.



15
16
17
18
19
20
# File 'lib/event_summary.rb', line 15

def initialize(model, action, location)
  @model = model
  @action = action
  @location = location
  @total_time = @calls = 0
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



5
6
7
# File 'lib/event_summary.rb', line 5

def action
  @action
end

#callsObject

Returns the value of attribute calls.



5
6
7
# File 'lib/event_summary.rb', line 5

def calls
  @calls
end

#locationObject

Returns the value of attribute location.



5
6
7
# File 'lib/event_summary.rb', line 5

def location
  @location
end

#modelObject

Returns the value of attribute model.



5
6
7
# File 'lib/event_summary.rb', line 5

def model
  @model
end

#total_timeObject

Returns the value of attribute total_time.



5
6
7
# File 'lib/event_summary.rb', line 5

def total_time
  @total_time
end

Class Method Details

.parse_files(*file_names) ⇒ Object

Gathers all of the events in provided files and returns array of event summaries sorted in descending order by their total time



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/event_summary.rb', line 25

def self.parse_files(*file_names)
  events = {}

  event_match = nil

  each_line(*file_names) do |line|
    # Previous line was an event
    if event_match
      location_match = LOCATION_RX.match(line)
      if location_match
        model, action, time = event_match.captures
        location = location_match.captures.first

        key = "#{model}/#{action}/#{location}"

        event = events[key] ||= new(model, action, location)
        event.calls += 1
        event.total_time += time.to_d

        event_match = nil

        next
      elsif LOCATION_PREFIX_RX.match(line)
        next
      end
    end

    event_match = EVENT_RX.match(line)
  end

  events.values.sort_by { |summary| -summary.total_time }
end