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

FILE_LINES_COUNT =
1_000

Instance Method Summary collapse

Constructor Details

#initialize(worker_name) ⇒ LogParser

Returns a new instance of LogParser.



7
8
9
10
# File 'lib/sidekiq/statistic/log_parser.rb', line 7

def initialize(worker_name)
  @worker_name = worker_name
  @logfile = log_file
end

Instance Method Details

#color(line) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/sidekiq/statistic/log_parser.rb', line 41

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



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

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



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

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



12
13
14
15
16
17
18
19
20
# File 'lib/sidekiq/statistic/log_parser.rb', line 12

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

  File
    .readlines(@logfile)
    .first(FILE_LINES_COUNT)
    .map{ |line| sub_line(line) if line[/\W?#@worker_name\W?/] }
    .compact
end

#sub_line(line) ⇒ Object



22
23
24
25
26
# File 'lib/sidekiq/statistic/log_parser.rb', line 22

def sub_line(line)
  line
    .sub(/\n/, '')
    .sub(/(JID-[\w]+)/) { jid_tag($1) }
end