class RailsLogParser::Parser

Attributes

log_path[W]
not_parseable_lines[R]

Public Class Methods

from_file(path) click to toggle source
# File lib/rails_log_parser/parser.rb, line 11
def from_file(path)
  parser = new
  File.open(path, 'r') do |handle|
    while (line = handle.gets)
      parser.puts(line)
    end
  end
  parser
end
log_path() click to toggle source
# File lib/rails_log_parser/parser.rb, line 7
def log_path
  @log_path || ENV['LOG_PATH']
end
new() click to toggle source
# File lib/rails_log_parser/parser.rb, line 24
def initialize
  @actions = {}
  @not_parseable_lines = RailsLogParser::NotParseableLines.new
  @heuristic = nil
end

Public Instance Methods

action(type, params) click to toggle source
# File lib/rails_log_parser/parser.rb, line 78
def action(type, params)
  @actions[params['id']] ||= RailsLogParser::Action.new(type, params['id'])
  @actions[params['id']].severity = params['severity_label']
  @actions[params['id']].datetime = params['datetime']
  @actions[params['id']].add_message(params['message'])
end
actions() click to toggle source
# File lib/rails_log_parser/parser.rb, line 70
def actions
  @actions.values
end
active_job(params) click to toggle source
# File lib/rails_log_parser/parser.rb, line 95
def active_job(params)
  action(:active_job, params)
end
add_message(params) click to toggle source
# File lib/rails_log_parser/parser.rb, line 103
def add_message(params)
  @actions[params['id']] ||= RailsLogParser::Action.new(type, params['id'])
  @actions[params['id']].add_message(params['message'])
end
delayed_job(params) click to toggle source
# File lib/rails_log_parser/parser.rb, line 99
def delayed_job(params)
  action(:delayed_job, params)
end
enable_heuristic(path) click to toggle source
# File lib/rails_log_parser/parser.rb, line 30
def enable_heuristic(path)
  @heuristic = path
  @heuristic_today = RailsLogParser::HeuristicStatFile.new(@heuristic, Date.today).tap { |p| p.write_stats(actions) }
end
last_action() click to toggle source
# File lib/rails_log_parser/parser.rb, line 108
def last_action
  RailsLogParser::Action.last
end
puts(line) click to toggle source
# File lib/rails_log_parser/parser.rb, line 74
def puts(line)
  RailsLogParser::Line.new(self, line.encode('UTF-8', invalid: :replace))
end
request(params) click to toggle source
# File lib/rails_log_parser/parser.rb, line 85
def request(params)
  action(:request, params)
end
summary(last_minutes: nil) click to toggle source
# File lib/rails_log_parser/parser.rb, line 35
def summary(last_minutes: nil)
  relevant = actions
  if last_minutes.present?
    from = last_minutes.to_i.minutes.ago
    relevant = relevant.select { |a| a.after?(from) }
  end
  summary_output = []
  if @not_parseable_lines.lines.present?
    summary_output.push('Not parseable lines:')
    summary_output += @not_parseable_lines.lines.map { |line| "  #{line}" }
    summary_output.push("\n\n")
    @not_parseable_lines.save
  end

  %i[warn error fatal].each do |severity|
    selected = relevant.select { |a| a.public_send("#{severity}?") }.reject(&:known_exception?)
    next if selected.blank?

    summary_output.push("#{selected.count} lines with #{severity}:")
    summary_output += selected.map(&:headline).map { |line| "  #{line}" }
    summary_output.push("\n\n")
  end

  unless @heuristic.nil?
    stats = RailsLogParser::HeuristicStatFile.build_heuristic(@heuristic, @heuristic_today)
    if stats.present?
      summary_output.push("Heuristic match! (threshold: #{RailsLogParser::HeuristicStatFile.heuristic_threshold})")
      stats.each { |k, v| summary_output.push("- #{k}: #{v.round(4)}") }
      summary_output.push("\n\n")
    end
  end

  summary_output.join("\n")
end
without_request(params) click to toggle source
# File lib/rails_log_parser/parser.rb, line 89
def without_request(params)
  params = params.named_captures
  params['id'] = SecureRandom.uuid
  action(:without_request, params)
end