/
app.rb
49 lines (37 loc) · 1.24 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# This is a sinatra app packaged in a gem, running directly from the gem.
#
raise "ENV['PICKY_LOG_FILE'] needs to be set for the statistics app to be run. Use either it, or run 'picky stats <logfile> <port>'." unless ENV['PICKY_LOG_FILE']
log_file = File.expand_path ENV['PICKY_LOG_FILE'], Dir.pwd
port = ENV['PICKY_STATISTICS_PORT'] || 4567
Dir.chdir File.expand_path('..', __FILE__)
require 'sinatra'
require 'haml'
begin
require File.expand_path '../../picky-statistics', Dir.pwd
rescue LoadError => e
require 'picky-statistics'
end
Stats = LogfileReader.new log_file
class PickyStatistics < Sinatra::Base
set :static, true
set :public, File.expand_path('.', Dir.pwd)
set :views, File.expand_path('views', Dir.pwd)
set :haml, { :format => :html5 }
# Returns an index page with all the statistics.
#
get '/' do
haml :'/index'
end
# Returns statistics data in JSON for the index page.
#
get '/index.json' do
Stats.since(0).to_json
end
# Returns statistics data in JSON since the last request.
#
get '/since_last.json' do
Stats.since_last.to_json
end
end
puts "Clam, Picky's friend, is looking at Picky's logfile\n#{log_file}\nand showing results on port #{port}."
PickyStatistics.run! :port => port