<commit_before>require 'sensu/base'
require 'thin'
require 'sinatra/async'
require 'em-http-request'
require 'slim'
require 'sass'
require 'uri'
require 'sprockets'
require 'yui/compressor'
require 'handlebars_assets'

module Sensu
  class Dashboard < Sinatra::Base
    register Sinatra::Async

    configure do
      set :assets, (Sprockets::Environment.new { |env|
          env.append_path(settings.root + "/assets/javascripts")
          env.append_path(settings.root + "/assets/stylesheets")
          env.append_path HandlebarsAssets.path
          if ENV['RACK_ENV'] == 'production'
            env.js_compressor  = YUI::JavaScriptCompressor.new
            env.css_compressor = YUI::CssCompressor.new
          end
        })
    end

    class << self
      def run(options={})
        EM::run do
          setup(options)
          start
          trap_signals
        end
      end

      def invalid_settings(reason, details={})
        $logger.fatal('invalid settings', {
          :reason => reason
        }.merge(details))
        $logger.fatal('SENSU DASHBOARD NOT RUNNING!')
        exit 2
      end

      def setup(options={})
        base = Sensu::Base.new(options)
        $logger = base.logger
        $settings = base.settings
        unless $settings[:dashboard].is_a?(Hash)
          invalid_settings('missing dashboard configuration')
        end
        unless $settings[:dashboard][:port].is_a?(Integer)
          invalid_settings('dashboard must have a port', {
            :settings => $settings[:dashboard]
          })
        end
        unless $settings[:dashboard][:user].is_a?(String) && $settings[:dashboard][:password].is_a?(String)
          invalid_settings('dashboard must have a user and password', {
            :settings => $settings[:dashboard]
          })
        end
        base.setup_process
        $api_url = 'http://' + $settings[:api][:host] + ':' + $settings[:api][:port].to_s
        $api_options = {}
        if $settings[:api][:user] && $settings[:api][:password]
          $api_options.merge!(:head => {:authorization => [$settings[:api][:user], $settings[:api][:password]]})
        end
      end

      def start
        Thin::Logging.silent = true
        Thin::Server.start(self, $settings[:dashboard][:port])
      end

      def stop
        $logger.warn('stopping')
        EM::stop_event_loop
      end

      def trap_signals
        %w[INT TERM].each do |signal|
          Signal.trap(signal) do
            $logger.warn('received signal', {
              :signal => signal
            })
            stop
          end
        end
      end
    end

    def request_log_line
      $logger.info([env['REQUEST_METHOD'], env['REQUEST_PATH']].join(' '), {
        :remote_address => env['REMOTE_ADDR'],<commit_msg>Set a default empty hash for [:head] to prevent error setting [:head]["Accept"] later.<commit_after>require 'sensu/base'
require 'thin'
require 'sinatra/async'
require 'em-http-request'
require 'slim'
require 'sass'
require 'uri'
require 'sprockets'
require 'yui/compressor'
require 'handlebars_assets'

module Sensu
  class Dashboard < Sinatra::Base
    register Sinatra::Async

    configure do
      set :assets, (Sprockets::Environment.new { |env|
          env.append_path(settings.root + "/assets/javascripts")
          env.append_path(settings.root + "/assets/stylesheets")
          env.append_path HandlebarsAssets.path
          if ENV['RACK_ENV'] == 'production'
            env.js_compressor  = YUI::JavaScriptCompressor.new
            env.css_compressor = YUI::CssCompressor.new
          end
        })
    end

    class << self
      def run(options={})
        EM::run do
          setup(options)
          start
          trap_signals
        end
      end

      def invalid_settings(reason, details={})
        $logger.fatal('invalid settings', {
          :reason => reason
        }.merge(details))
        $logger.fatal('SENSU DASHBOARD NOT RUNNING!')
        exit 2
      end

      def setup(options={})
        base = Sensu::Base.new(options)
        $logger = base.logger
        $settings = base.settings
        unless $settings[:dashboard].is_a?(Hash)
          invalid_settings('missing dashboard configuration')
        end
        unless $settings[:dashboard][:port].is_a?(Integer)
          invalid_settings('dashboard must have a port', {
            :settings => $settings[:dashboard]
          })
        end
        unless $settings[:dashboard][:user].is_a?(String) && $settings[:dashboard][:password].is_a?(String)
          invalid_settings('dashboard must have a user and password', {
            :settings => $settings[:dashboard]
          })
        end
        base.setup_process
        $api_url = 'http://' + $settings[:api][:host] + ':' + $settings[:api][:port].to_s
        $api_options = {:head => {}}
        if $settings[:api][:user] && $settings[:api][:password]
          $api_options.merge!(:head => {:authorization => [$settings[:api][:user], $settings[:api][:password]]})
        end
      end

      def start
        Thin::Logging.silent = true
        Thin::Server.start(self, $settings[:dashboard][:port])
      end

      def stop
        $logger.warn('stopping')
        EM::stop_event_loop
      end

      def trap_signals
        %w[INT TERM].each do |signal|
          Signal.trap(signal) do
            $logger.warn('received signal', {
              :signal => signal
            })
            stop
          end
        end
      end
    end

    def request_log_line
      $logger.info([env['REQUEST_METHOD'], env['REQUEST_PATH']].join(' '), {
        :remote_address => env['REMOTE_ADDR'],
{'fraction_non_alphanumeric': 0.13288447909284196, 'fraction_numerical': 0.00035435861091424523, 'mean_word_length': 2.2781649245063877, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5078927', 'n_tokens_mistral': 1673, 'n_tokens_neox': 1647, 'n_words': 334}