module Sidekiq::History::WebExtension

Constants

DAFAULT_DAYS

Public Class Methods

registered(app) click to toggle source
# File lib/sidekiq/history/web_extension.rb, line 8
def self.registered(app)
  view_path = File.join(File.expand_path('..', __FILE__), 'views')

  app.helpers do
    def formate_date(string, format = nil)
      Time.parse(string).strftime(format || '%T, %e %B %Y')
    end

    def calculate_date_range(params)
      if params['dateFrom'] && params['dateTo']
        from = Date.parse(params['dateFrom'])
        to   = Date.parse(params['dateTo'])

        [(to - from).to_i, to]
      else
        [DAFAULT_DAYS]
      end
    end
  end

  app.get '/history.js' do
    content_type 'text/javascript'
    File.read(File.join(view_path, 'history.js'))
  end

  app.get '/sidekiq-history.css' do
    content_type 'text/css'
    File.read(File.join(view_path, 'sidekiq-history.css'))
  end

  app.get '/history-live' do
    begin
      redis.subscribe(:one, :two) do |on|
        on.subscribe do |channel, subscriptions|
          puts "Subscribed to ##{channel} (#{subscriptions} subscriptions)"
        end

        on.message do |channel, message|
          puts "##{channel}: #{message}"
          redis.unsubscribe if message == "exit"
        end

        on.unsubscribe do |channel, subscriptions|
          puts "Unsubscribed from ##{channel} (#{subscriptions} subscriptions)"
        end
      end
    rescue Redis::BaseConnectionError => error
      puts "#{error}, retrying in 1s"
      sleep 1
      retry
    end
  end

  app.get '/history' do
    statistic = Sidekiq::History::Statistic.new(*calculate_date_range(params))
    @workers = statistic.display
    render(:erb, File.read(File.join(view_path, 'history.erb')))
  end

  app.get '/history/charts.json' do
    content_type :json
    charts = Sidekiq::History::Charts.new(*calculate_date_range(params))

    {
      tooltip_template: '<%= datasetLabel %> - <%= value %>',
      labels: charts.dates,
      failed_datasets: charts.information_for(:failed),
      passed_datasets: charts.information_for(:passed)
    }.to_json
  end

  app.get '/history/:worker' do
    @name = params[:worker]

    @worker_statistic =
      Sidekiq::History::Statistic.new(*calculate_date_range(params)).display_pre_day(@name)
    @worker_log =
      Sidekiq::History::LogParser.new(@name).parse

    render(:erb, File.read(File.join(view_path, 'worker.erb')))
  end
end

Public Instance Methods

calculate_date_range(params) click to toggle source
# File lib/sidekiq/history/web_extension.rb, line 16
def calculate_date_range(params)
  if params['dateFrom'] && params['dateTo']
    from = Date.parse(params['dateFrom'])
    to   = Date.parse(params['dateTo'])

    [(to - from).to_i, to]
  else
    [DAFAULT_DAYS]
  end
end
formate_date(string, format = nil) click to toggle source
# File lib/sidekiq/history/web_extension.rb, line 12
def formate_date(string, format = nil)
  Time.parse(string).strftime(format || '%T, %e %B %Y')
end