Skip to content

Instantly share code, notes, and snippets.

@markmarkoh
Created January 12, 2010 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markmarkoh/274802 to your computer and use it in GitHub Desktop.
Save markmarkoh/274802 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'sinatra'
require 'haml'
require 'model'
set :port, 80
get '/' do
protected!
@upload_types = pie Chart.upload_types
@activity_data = timeline Chart.count_all_activity
haml :index
end
helpers do
def timeline(data_array)
@data_str = "["
data_array.find().to_a.each do |f|
@data_str += "[#{f['_id'].round * 1000}, #{f['value']['count'].round}],"
end
return @data_str + "]"
end
def pie(data_array)
@labels = ""
@data_str = ""
@data = []
@sum = 0
data_array.find().to_a.each do |f|
@labels += "#{f['_id']}|"
@sum += f['value']['count'].to_i
@data.push(f['value']['count'].to_i)
end
@data.each do |d|
@num = d/(@sum + 0.0) * 100
@data_str += "#{@num.round},"
end
@labels = @labels[0..@labels.length-2]
@data_str = @data_str[0..@data_str.length-2]
return <<-HTML
<img src="http://chart.apis.google.com/chart?chs=450x200&chp=0.99&chd=t:#{@data_str}&cht=p3&chl=#{@labels}" />
HTML
end
def protected!
response['WWW-Authenticate'] = %(Basic realm="Admin Only") and \
throw(:halt, [401, "Not authorized\n"]) and \
return unless authorized?
end
def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
@auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == ['USER', 'PASS']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment