lib/fnordmetric/app.rb in fnordmetric-0.5.1 vs lib/fnordmetric/app.rb in fnordmetric-0.5.2
- old
+ new
@@ -55,19 +55,28 @@
get '/favicon.ico' do
""
end
- #get '/metric/:name' do
- # content_type 'application/json'
- # FnordMetric::MetricAPI.new(params).render
- #end
+ get '/:namespace/gauge/:name' do
- #get '/widget/:name' do
- # @dashboard = FnordMetric.dashboards.first
- # @widget = @dashboard.widgets.first
- # haml :widget
- #end
+ gauge = current_namespace.gauges.fetch(params[:name].intern)
+
+ data = if gauge.three_dimensional?
+ _t = (params[:at] || Time.now).to_i
+ { :count => gauge.field_values_total(_t), :values => gauge.field_values_at(_t) }
+ elsif params[:at] && params[:at] =~ /^[0-9]+$/
+ { (_t = gauge.tick_at(params[:at].to_i)) => gauge.value_at(_t) }
+ elsif params[:at] && params[:at] =~ /^([0-9]+)-([0-9]+)$/
+ _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
+
+ data.to_json
+ end
get '/:namespace/sessions' do
sessions = current_namespace.sessions(:all, :limit => 100).map do |session|
session.fetch_data!