class Flapjack::Gateways::Web

Public Class Methods

start() click to toggle source
# File lib/flapjack/gateways/web.rb, line 37
def start
  Flapjack.logger.info "starting web - class"

  set :show_exceptions, false
  @show_exceptions = Sinatra::ShowExceptions.new(self)

  if access_log = (@config && @config['access_log'])
    unless File.directory?(File.dirname(access_log))
      raise "Parent directory for log file #{access_log} doesn't exist"
    end

    use Rack::CommonLogger, ::Logger.new(@config['access_log'])
  end

  # session's only used for error message display, so
  session_secret = @config['session_secret']

  use Rack::Session::Cookie, :key => 'flapjack.session',
                             :path => '/',
                             :secret => session_secret || SecureRandom.hex(64)

  @api_url = @config['api_url']
  if @api_url.nil?
    raise "'api_url' config must contain a Flapjack API instance address"
  end

  uri = begin
    URI(@api_url)
  rescue URI::InvalidURIError
    # TODO should we just log and re-raise the exception?
    raise "'api_url' is not a valid URI (#{@api_url})"
  end

  unless ['http', 'https'].include?(uri.scheme)
    raise "'api_url' is not a valid http or https URI (#{@api_url})"
  end
  unless @api_url.match(/^.*\/$/)
    Flapjack.logger.info "api_url must end with a trailing '/', setting to '#{@api_url}/'"
    @api_url = "#{@api_url}/"
  end

  Flapjack::Diner.base_uri(@api_url)
  Flapjack::Diner.logger = Flapjack.logger

  # constants won't be exposed to eRb scope
  @default_logo_url = "img/flapjack-2013-notext-transparent-300-300.png"
  @logo_image_file  = nil
  @logo_image_ext   = nil

  if logo_image_path = @config['logo_image_path']
    if File.file?(logo_image_path)
      @logo_image_file = logo_image_path
      @logo_image_ext  = File.extname(logo_image_path)
    else
      Flapjack.logger.error "logo_image_path '#{logo_image_path}'' does not point to a valid file."
    end
  end

  @auto_refresh = (@config['auto_refresh'].respond_to?('to_i') &&
                   (@config['auto_refresh'].to_i > 0)) ? @config['auto_refresh'].to_i : false
end

Public Instance Methods

charset_for_content_type(ct) click to toggle source
# File lib/flapjack/gateways/web.rb, line 116
def charset_for_content_type(ct)
  charset = Encoding.default_external
  charset.nil? ? ct : "#{ct}; charset=#{charset.name}"
end
h(text) click to toggle source
# File lib/flapjack/gateways/web.rb, line 103
def h(text)
  ERB::Util.h(text)
end
include_active?(path) click to toggle source
# File lib/flapjack/gateways/web.rb, line 111
def include_active?(path)
  return '' unless request.path == "/#{path}"
  " class='active'"
end
u(text) click to toggle source
# File lib/flapjack/gateways/web.rb, line 107
def u(text)
  ERB::Util.u(text)
end

Private Instance Methods

boolean_from_str(str) click to toggle source
# File lib/flapjack/gateways/web.rb, line 644
def boolean_from_str(str)
  case str
  when '0', 'f', 'false', 'n', 'no'
    false
  when '1', 't', 'true', 'y', 'yes'
    true
  end
end
check_extra_state(check, time) click to toggle source
# File lib/flapjack/gateways/web.rb, line 558
def check_extra_state(check, time)
  state = check_state(check, time)

  current_state = Flapjack::Diner.related(check, :current_state)

  current_scheduled_maintenances = Flapjack::Diner.related(check, :current_scheduled_maintenances)
  current_scheduled_maintenance = current_scheduled_maintenances.max_by do |sm|
    begin
      DateTime.parse(sm[:end_time]).to_i
    rescue ArgumentError
      Flapjack.logger.warn "Couldn't parse time from current_scheduled_maintenances"
      -1
    end
  end

  current_unscheduled_maintenance = Flapjack::Diner.related(check, :current_unscheduled_maintenance)

  state.merge(
    :details       => current_state.nil? ? '-' : current_state[:details],
    :perfdata      => current_state.nil? ? '-' : current_state[:perfdata],
    :current_scheduled_maintenances => (current_scheduled_maintenances || []),
    :current_scheduled_maintenance => current_scheduled_maintenance,
    :current_unscheduled_maintenance => current_unscheduled_maintenance,
  )
end
check_state(check, time) click to toggle source
# File lib/flapjack/gateways/web.rb, line 507
def check_state(check, time)
  current_state = Flapjack::Diner.related(check, :current_state)

  last_changed = if current_state.nil? || current_state[:created_at].nil?
    nil
  else
    begin
      DateTime.parse(current_state[:created_at])
    rescue ArgumentError
      Flapjack.logger.warn("error parsing check state :created_at ( #{current_state.inspect} )")
    end
  end

  last_updated = if current_state.nil? || current_state[:updated_at].nil?
    nil
  else
    begin
      DateTime.parse(current_state[:updated_at])
    rescue ArgumentError
      Flapjack.logger.warn("error parsing check state :updated_at ( #{current_state.inspect} )")
    end
  end

  latest_notifications = Flapjack::Diner.related(check, :latest_notifications)

  current_scheduled_maintenances = Flapjack::Diner.related(check, :current_scheduled_maintenances)
  current_scheduled_maintenance = current_scheduled_maintenances.max_by do |sm|
    begin
      DateTime.parse(sm[:end_time]).to_i
    rescue ArgumentError
      Flapjack.logger.warn "Couldn't parse time from current_scheduled_maintenances"
      -1
    end
  end

  in_scheduled_maintenance = !current_scheduled_maintenance.nil?

  current_unscheduled_maintenance = Flapjack::Diner.related(check, :current_unscheduled_maintenance)
  in_unscheduled_maintenance = !current_unscheduled_maintenance.nil?

  {
    :condition     => current_state.nil? ? '-' : current_state[:condition],
    :summary       => current_state.nil? ? '-' : current_state[:summary],
    :latest_notifications => (latest_notifications || []),
    :last_changed  => last_changed,
    :last_updated  => last_updated,
    :in_scheduled_maintenance => in_scheduled_maintenance,
    :in_unscheduled_maintenance => in_unscheduled_maintenance
  }
end
include_page_title() click to toggle source
# File lib/flapjack/gateways/web.rb, line 637
def include_page_title
  if instance_variable_defined?('@page_title') && !@page_title.nil?
    return "#{@page_title} | Flapjack"
  end
  "Flapjack"
end
include_required_css() click to toggle source
# File lib/flapjack/gateways/web.rb, line 607
def include_required_css
  return "" if @required_css.nil?
  @required_css.map { |filename|
    %(<link rel="stylesheet" href="#{link_to("css/#{filename}.css")}" media="screen">)
  }.join("\n    ")
end
include_required_js() click to toggle source
# File lib/flapjack/gateways/web.rb, line 600
def include_required_js
  return "" if @required_js.nil?
  @required_js.map { |filename|
    "<script type='text/javascript' src='#{link_to("js/#{filename}.js")}'></script>"
  }.join("\n    ")
end
page_title(string) click to toggle source
# File lib/flapjack/gateways/web.rb, line 633
def page_title(string)
  @page_title = string
end
pagination_from_context(context) click to toggle source
# File lib/flapjack/gateways/web.rb, line 584
def pagination_from_context(context)
  ((context || {})[:meta] || {})[:pagination]
end
require_css(*css) click to toggle source
# File lib/flapjack/gateways/web.rb, line 594
def require_css(*css)
  @required_css ||= []
  @required_css += css
  @required_css.uniq!
end
require_js(*js) click to toggle source
# File lib/flapjack/gateways/web.rb, line 588
def require_js(*js)
  @required_js ||= []
  @required_js += js
  @required_js.uniq!
end