Class: LogParser::LogEntry

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

Overview

LogEntry contains a summary of log data for a single request.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry) ⇒ LogEntry

Returns a new instance of LogEntry.



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/production_log/parser.rb', line 69

def initialize(entry)
  @page = nil
  @ip = nil
  @time = nil
  @queries = []
  @request_time = 0
  @render_time = 0
  @db_time = 0
  @in_component = 0

  parse entry
end

Instance Attribute Details

#db_timeObject (readonly)

Total database time



62
63
64
# File 'lib/production_log/parser.rb', line 62

def db_time
  @db_time
end

#ipObject (readonly)

Requesting IP



36
37
38
# File 'lib/production_log/parser.rb', line 36

def ip
  @ip
end

#pageObject (readonly)

Controller and action for this request



31
32
33
# File 'lib/production_log/parser.rb', line 31

def page
  @page
end

#queriesObject (readonly)

Array of SQL queries containing query type and time taken. The complete text of the SQL query is not saved to reduct memory usage.



47
48
49
# File 'lib/production_log/parser.rb', line 47

def queries
  @queries
end

#query_countObject (readonly)

Creates a new LogEntry from the log data in entry.



67
68
69
# File 'lib/production_log/parser.rb', line 67

def query_count
  @query_count
end

#render_timeObject (readonly)

Total render time.



57
58
59
# File 'lib/production_log/parser.rb', line 57

def render_time
  @render_time
end

#request_sizeObject (readonly)

Creates a new LogEntry from the log data in entry.



67
68
69
# File 'lib/production_log/parser.rb', line 67

def request_size
  @request_size
end

#request_timeObject (readonly)

Total request time, including database, render and other.



52
53
54
# File 'lib/production_log/parser.rb', line 52

def request_time
  @request_time
end

#response_sizeObject (readonly)

Creates a new LogEntry from the log data in entry.



67
68
69
# File 'lib/production_log/parser.rb', line 67

def response_size
  @response_size
end

#row_countObject (readonly)

Creates a new LogEntry from the log data in entry.



67
68
69
# File 'lib/production_log/parser.rb', line 67

def row_count
  @row_count
end

#timeObject (readonly)

Time the request was made



41
42
43
# File 'lib/production_log/parser.rb', line 41

def time
  @time
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



150
151
152
153
154
155
156
157
158
159
# File 'lib/production_log/parser.rb', line 150

def ==(other) # :nodoc:
  other.class == self.class and
  other.page == self.page and
  other.ip == self.ip and
  other.time == self.time and
  other.queries == self.queries and
  other.request_time == self.request_time and
  other.render_time == self.render_time and
  other.db_time == self.db_time
end

#parse(entry) ⇒ Object

Extracts log data from entry, which is an Array of lines from the same request.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/production_log/parser.rb', line 86

def parse(entry)
  entry.each do |line|
    case line
    when /^Parameters/, /^Cookie set/, /^Rendering/,
      /^Redirected/ then
      # nothing
    when /^Processing ([\S]+) \(for (.+) at (.*)\)/ then
      next if @in_component > 0
      @page = $1
      @ip   = $2
      @time = $3
    when /^Completed in ([\S]+) \(\d* reqs\/sec\) \| (.+)/,
      /^Completed in ([\S]+) \((.+)\)/ then

      next if @in_component > 0
      # handle millisecond times as well as fractional seconds
      @times_in_milliseconds = $1[-2..-1] == 'ms'

      @request_time = @times_in_milliseconds ? ($1.to_i/1000.0) : $1.to_f
      log_info = $2

      log_info = log_info.split(/[,|]/)
      log_info = log_info.map do |entry|
        next nil unless entry.index(': ')
        result = entry.strip.split(': ')
        if result.size > 2
          result = [result[0], result[1..-1].join(':')]
        end
        result
      end.compact.flatten

      log_info = Hash[*log_info]

      @row_count = log_info['Rows'].to_i
      @query_count = log_info['Queries'].to_i
      @request_size = log_info['Request Size'].to_i
      @response_size = log_info['Response Size'].to_i

      @page = log_info['Processed'] if log_info['Processed']
      @page += ".#{log_info['Response Format']}" if log_info['Response Format']

      if x = (log_info['DB'])
        x = x.split(' ').first
        @db_time = @times_in_milliseconds ? (x.to_i/1000.0) : x.to_f
      end

      if x = (log_info['Rendering'] || log_info['View'])
        x = x.split(' ').first
        @render_time = @times_in_milliseconds ? (x.to_i/1000.0) : x.to_f
      end

    when /(.+?) \(([^)]+)\)   / then
      @queries << [$1, $2.to_f]
    when /^Start rendering component / then
      @in_component += 1
    when /^End of component rendering$/ then
      @in_component -= 1
    when /^Fragment hit: / then
    else # noop
#          raise "Can't handle #{line.inspect}" if $TESTING
    end
  end
end