Class: Sidekiq::Statistic::LogParser

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/statistic/log_parser.rb

Overview

Heroku have read only file system. See more in this link: devcenter.heroku.com/articles/read-only-filesystem

Constant Summary collapse

WORKER_INFO_REGEXP_TEMPLATE =
"([\\W]+|^)%{worker_name}([\\W]+|$)"

Instance Method Summary collapse

Constructor Details

#initialize(worker_name) ⇒ LogParser

Returns a new instance of LogParser.



10
11
12
13
14
15
# File 'lib/sidekiq/statistic/log_parser.rb', line 10

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

Instance Method Details

#color(line) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/sidekiq/statistic/log_parser.rb', line 46

def color(line)
  case
  when line.include?('done')  then 'green'
  when line.include?('start') then 'yellow'
  when line.include?('fail')  then 'red'
  end
end

#jid_style(worker_jid) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/sidekiq/statistic/log_parser.rb', line 38

def jid_style(worker_jid)
  return unless worker_jid
  color = Digest::MD5.hexdigest(worker_jid)[4..9]
    .scan(/../).map{ |c| c.to_i(16) }.join ','

  "style=\"background-color: rgba(#{color},0.2);\""
end

#jid_tag(jid) ⇒ Object



33
34
35
36
# File 'lib/sidekiq/statistic/log_parser.rb', line 33

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>"
end

#parseObject



17
18
19
20
21
22
23
24
25
# File 'lib/sidekiq/statistic/log_parser.rb', line 17

def parse
  return [] unless File.exist?(@logfile)

  File
    .readlines(@logfile)
    .last(last_log_lines)
    .map{ |line| sub_line(line) if line.match(worker_info_regexp) }
    .compact
end

#sub_line(line) ⇒ Object



27
28
29
30
31
# File 'lib/sidekiq/statistic/log_parser.rb', line 27

def sub_line(line)
  line
    .sub(/\n/, '')
    .sub(jid_tag_regexp) { jid_tag($1) }
end