lib/fnordmetric/app.rb in fnordmetric-0.6.1 vs lib/fnordmetric/app.rb in fnordmetric-0.6.2
- old
+ new
@@ -1,20 +1,20 @@
# encoding: utf-8
class FnordMetric::App < Sinatra::Base
-
+
@@sessions = Hash.new
-
+
Encoding.default_external = Encoding::UTF_8
#use Rack::Reloader, 0
-
+
enable :session
- set :haml, :format => :html5
+ set :haml, :format => :html5
set :views, ::File.expand_path('../../../haml', __FILE__)
- set :public, ::File.expand_path('../../../pub', __FILE__)
+ set :public_folder, ::File.expand_path('../../../pub', __FILE__)
def initialize(namespaces, opts)
@namespaces = {}
@redis = Redis.connect(:url => opts[:redis_url])
@opts = opts
@@ -23,11 +23,11 @@
@namespaces[key].instance_eval(&block)
@namespaces[key].ready!(@redis.clone)
end
super(nil)
end
-
+
helpers do
include Rack::Utils
alias_method :h, :escape_html
def path_prefix
@@ -36,20 +36,20 @@
def namespaces
@namespaces
end
- def current_namespace
+ def current_namespace
@namespaces[@namespaces.keys.detect{ |k|
k.to_s == params[:namespace]
}.try(:intern)]
end
end
if ENV['RACK_ENV'] == "test"
- set :raise_errors, true
+ set :raise_errors, true
end
get '/' do
redirect "#{path_prefix}/#{@namespaces.keys.first}"
end
@@ -76,28 +76,28 @@
_range = params[:at].split("-").map(&:to_i)
_values = gauge.values_in(_range.first.._range.last)
params[:sum] ? { :sum => _values.values.compact.map(&:to_i).sum } : _values
else
{ (_t = gauge.tick_at(Time.now.to_i-gauge.tick)) => gauge.value_at(_t) }
- end
+ end
data.to_json
end
get '/:namespace/sessions' do
sessions = current_namespace.sessions(:all, :limit => 100).map do |session|
session.fetch_data!
session.to_json
- end
+ end
{ :sessions => sessions }.to_json
end
get '/:namespace/events' do
- events = if params[:type]
+ events = if params[:type]
current_namespace.events(:by_type, :type => params[:type])
elsif params[:session_key]
current_namespace.events(:by_session_key, :session_key => params[:session_key])
else
find_opts = { :limit => 100 }
@@ -120,18 +120,18 @@
dashboard.to_json
end
post '/events' do
- halt 400, 'please specify the event_type (_type)' unless params["_type"]
+ halt 400, 'please specify the event_type (_type)' unless params["_type"]
track_event((8**32).to_s(36), parse_params(params))
end
private
def parse_params(hash)
hash.tap do |h|
- h.keys.each{ |k| h[k] = parse_param(h[k]) }
+ h.keys.each{ |k| h[k] = parse_param(h[k]) }
end
end
def parse_param(object)
return object unless object.is_a?(String)