class Object

Public Instance Methods

delete_old_stats() click to toggle source
# File lib/rails_log_parser/heuristic_stat_file.rb, line 57
def delete_old_stats
  last_20_days = (0..19).map { |i| (Date.today - i) }.map { |date| File.join(path, "heuristic_stats_#{date}.json") }
  Dir[File.join(path, 'heuristic_stats_*.json')].reject { |file| last_20_days.include?(file) }.each do |file|
    File.unlink(file)
  end
end
heuristic_file_path() click to toggle source
# File lib/rails_log_parser/heuristic_stat_file.rb, line 71
def heuristic_file_path
  @heuristic_file_path ||= File.join(path, "heuristic_stats_#{date}.json")
end
load_stats() click to toggle source
# File lib/rails_log_parser/heuristic_stat_file.rb, line 64
def load_stats
  @stats = JSON.parse(File.read(heuristic_file_path), symbolize_names: true) if File.file?(heuristic_file_path)
  @stats ||= {}
rescue JSON::ParserError
  @stats = {}
end
rate(exception) click to toggle source
# File lib/rails_log_parser/heuristic_stat_file.rb, line 75
def rate(exception)
  return 0 if stats[:actions] == 0

  stats.dig(:known_exceptions, exception.to_sym).to_f / stats[:actions]
end
write_stats(actions) click to toggle source
# File lib/rails_log_parser/heuristic_stat_file.rb, line 40
def write_stats(actions)
  actions = actions.select { |action| action.datetime.to_date == date }.sort_by(&:datetime)
  @stats = {
    actions: actions.count,
    known_exceptions: {},
    starts_at: actions.first&.datetime,
    ends_at: actions.last&.datetime,
  }

  RailsLogParser::Action::KNOWN_EXCEPTIONS.each_key do |exception|
    @stats[:known_exceptions][exception.to_sym] = actions.count { |action| action.known_exception?(exception) }
  end

  delete_old_stats
  File.write(heuristic_file_path, @stats.to_json)
end