Sha256: c77616ab5a4ca21416b8ceafc370ea91f683bb93b70afb70df95e58f6c0495a7

Contents?: true

Size: 1.56 KB

Versions: 6

Compression:

Stored size: 1.56 KB

Contents

require 'role'
require 'sinatra/base'
require 'db_crap'

class StatsRole < Role
  PORT = 9500
  role 'stats'

  def initialize(manifest)
    super
    init_links 'db_read', @manifest['db_read']
    init_links 'db_peer', @manifest['db_peer']
  end

  def start
    start_server
  end

  def stop
    @http_server.shutdown
  end

  alias :enable :start
  alias :disable :stop

  private

  def start_server
    my_app = StatsApp.new(@active_links, @manifest)

    trap('INT') do
      @http_server.shutdown if @http_server
      exit
    end

    Thread.new {
      Rack::Handler::WEBrick.run(my_app, :Port => PORT.to_s) do |server|
        @http_server = server
      end
    }
  end

  class StatsApp < Sinatra::Base
    def initialize(active_links, manifest)
      @active_links = active_links
      @manifest = manifest
      super()
    end

    get '/stats' do
      stats_html
    end

    get '/ping' do
      'pong'
    end

    private

    def stats_html
      resp = list

      key_count = resp.size
      last_updated = key_count > 0 ? resp.sort_by { |rec| rec['updated'] }[-1]['key'] : nil
      largest_value = key_count > 0 ? resp.sort_by { |rec| rec['value'].to_s.size }[-1]['key'] : nil

      <<-EOF
        <div class="stats">
          <h3>DB Stats</h3>
          <dl>
            <dt>DB Size</dt>
            <dd>#{key_count}</dd>

            <dt>Last updated key</dt>
            <dd>#{last_updated}</dd>

            <dt>Largest value</dt>
            <dd>#{largest_value}</dd>
          </dl>
        </div>
      EOF
    end

    private

    include DbCrap
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
bum-0.0.17 lib/stats_role.rb
bum-0.0.16 lib/stats_role.rb
bum-0.0.15 lib/stats_role.rb
bum-0.0.14 lib/stats_role.rb
bum-0.0.13 lib/stats_role.rb
bum-0.0.12 lib/stats_role.rb