lib/sidekiq/statistic/log_parser.rb in sidekiq-statistic-1.2.0 vs lib/sidekiq/statistic/log_parser.rb in sidekiq-statistic-1.4.0
- old
+ new
@@ -1,30 +1,35 @@
+# frozen_string_literal: true
+
module Sidekiq
module Statistic
# Heroku have read only file system. See more in this link:
# https://devcenter.heroku.com/articles/read-only-filesystem
class LogParser
- FILE_LINES_COUNT = 1_000
+ WORKER_INFO_REGEXP_TEMPLATE = "([\\W]+|^)%{worker_name}([\\W]+|$)"
+
def initialize(worker_name)
@worker_name = worker_name
@logfile = log_file
+ @worker_info_regexp = Regexp.compile(WORKER_INFO_REGEXP_TEMPLATE % { worker_name: @worker_name })
+ @jid_tag_regexp = Regexp.compile('(JID-[\\w]+)')
end
def parse
- return [] unless File.exists?(@logfile)
+ return [] unless File.exist?(@logfile)
File
.readlines(@logfile)
- .first(FILE_LINES_COUNT)
- .map{ |line| sub_line(line) if line[/\W?#@worker_name\W?/] }
+ .last(last_log_lines)
+ .map{ |line| sub_line(line) if line.match(worker_info_regexp) }
.compact
end
def sub_line(line)
line
.sub(/\n/, '')
- .sub(/(JID-[\w]+)/) { jid_tag($1) }
+ .sub(jid_tag_regexp) { jid_tag($1) }
end
def jid_tag(jid)
"<span class=\"statistic__jid js-jid__#{jid[4..-1]}\""\
"data-target=\".js-jid__#{jid[4..-1]}\" #{jid_style jid}>#{jid}</span>"
@@ -45,13 +50,20 @@
when line.include?('fail') then 'red'
end
end
private
+
+ attr_reader :worker_info_regexp, :jid_tag_regexp
+
def log_file
Sidekiq.options[:logfile] ||
Sidekiq.logger.instance_variable_get(:@logdev).filename ||
Sidekiq::Statistic.configuration.log_file
+ end
+
+ def last_log_lines
+ Sidekiq::Statistic.configuration.last_log_lines
end
end
end
end