Sha256: 0457798daaf883dd0ca6a768be97c6a6213ba02459cf376c3a1d0c6c3f0bd4f4

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

# 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
      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.exist?(@logfile)

        File
          .readlines(@logfile)
          .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_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>"
      end

      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

      def color(line)
        case
        when line.include?('done')  then 'green'
        when line.include?('start') then 'yellow'
        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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sidekiq-statistic-1.4.0 lib/sidekiq/statistic/log_parser.rb