-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.rb
More file actions
64 lines (53 loc) · 1.74 KB
/
application.rb
File metadata and controls
64 lines (53 loc) · 1.74 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require 'bundler'
ENV['RACK_ENV'] ||= 'development'
Bundler.require(:default, ENV['RACK_ENV'])
ENV['DATABASE_URL'] ||= "postgres://#{ENV['USER']}:@localhost/dump_#{ENV['RACK_ENV']}"
DB = Sequel.connect(ENV['DATABASE_URL'])
DB.extension(:connection_validator)
DB.pool.connection_validation_timeout = -1
class Dump < Sequel::Model
def before_create
self.created_at ||= Time.now.utc
end
end
class Dumpster < Sinatra::Base
get '/' do
query = Dump.reverse_order(:created_at).select(:id, :created_at, :key).limit(params[:limit] || 200)
"<p>What can I dump for you today?</p>" +
"<div><a href='/stats'>Daily deadlock stats</a></div>" +
query.all.map {|d|
"<a href='/dumps/#{d.id}/body'>Fixture for #{d.key}</a> created on #{d.created_at}"
}.join('<br/>')
end
get '/stats' do
table = "<table><thead><th>Date</th><th>Count</th></thead><tbody>"
table += stats_query.all.map do |row|
"<tr><td>#{row[:date]}</td><td>#{row[:count]}</td></tr>"
end.join("\n")
table += "</tbody></table>"
"<h1>Daily deadlock statistics</h1> #{table}"
end
get '/stats.json' do
require 'json'
content_type :json
stats_query.all.map { |row| [row[:date], row[:count]] }.to_json
end
get %r{/dumps/(\d+)/body} do |id|
(Dump[id] || raise(Sinatra::NotFound)).body
end
post '/dump' do
d = Dump.create params
uri("/dumps/#{d.id}/body")
end
private
def stats_query
Dump
.select(
Sequel.lit('COUNT(*)').as(:count),
Sequel.lit("to_char(created_at, 'YYYY-MM-DD')").as(:date)
)
.where { id > 661035 } # random HTML before this
.group(Sequel.lit("to_char(created_at, 'YYYY-MM-DD')"))
.order(Sequel.lit("to_char(created_at, 'YYYY-MM-DD') DESC"))
end
end