class Dasht::RackApp

Attributes

parent[RW]

Public Class Methods

new(parent) click to toggle source
# File lib/dasht/rack_app.rb, line 5
def initialize(parent)
  @parent = parent
end

Public Instance Methods

_call(env) click to toggle source
# File lib/dasht/rack_app.rb, line 29
def _call(env)
  if "/" == env["REQUEST_PATH"] && parent.boards["default"]
    return ['200', {'Content-Type' => 'text/html'}, [parent.boards["default"].to_html]]
  end

  /^\/boards\/(.+)$/.match(env["REQUEST_PATH"]) do |match|
    board = match[1]
    if parent.boards[board]
      return ['200', {'Content-Type' => 'text/html'}, [parent.boards[board].to_html]]
    else
      return ['404', {'Content-Type' => 'text/html'}, ["Board #{board} not found."]]
    end
  end

  /^\/data$/.match(env["REQUEST_PATH"]) do |match|
    queries = JSON.parse(env['rack.input'].gets)
    now = Time.now.to_i
    data = queries.map do |query|
      metric     = query["metric"]
      resolution = query["resolution"]
      periods    = query["periods"]
      ts = now - (resolution * periods)
      (1..periods).map do |n|
        parent.metrics.get(metric, ts, ts += resolution) || 0
      end
    end

    return ['200', {'Content-Type' => 'application/json'}, [data.to_json]]
  end

  /^\/data\/(.+)/.match(env["REQUEST_PATH"]) do |match|
    parts = match[1].split('/')
    metric     = parts.shift
    resolution = parts.shift.to_i
    periods    = (parts.shift || 1).to_i
    ts = Time.now.to_i - (resolution * periods)
    data = (1..periods).map do |n|
      parent.metrics.get(metric, ts, ts += resolution) || 0
    end
    return ['200', {'Content-Type' => 'application/json'}, [data.to_json]]
  end

  return ['404', {'Content-Type' => 'text/html'}, ["Path not found: #{env['REQUEST_PATH']}"]]
end
root_path() click to toggle source
# File lib/dasht/rack_app.rb, line 9
def root_path
  File.join(File.dirname(__FILE__), "..", "..")
end
run(port) click to toggle source
# File lib/dasht/rack_app.rb, line 13
def run(port)
  context = self
  app = Rack::Builder.new do
    use Rack::Static, :urls => ["/assets"], :root  => context.root_path
    run lambda { |env|
      begin
        context._call(env)
      rescue => e
        context.parent.log e
        raise e
      end
    }
  end
  Rack::Server.start(:app => app, :Port => (port || 8080))
end