-
Notifications
You must be signed in to change notification settings - Fork 4
/
pg_performance.rb
131 lines (105 loc) · 2.86 KB
/
pg_performance.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require 'sinatra/base'
require 'sinatra/reloader'
require 'sinatra/json'
require 'sequel'
require 'pg'
require 'rouge'
require 'erb'
require 'configurability'
class PGPerformance < Sinatra::Base
extend Configurability
configurability( :pgperformance ) do
setting :db_uri
setting :target_db
setting :root_url
end
def self::db
return @db ||= Sequel.connect( self.db_uri )
end
Sinatra::Base.configure do
register Sinatra::Reloader
config_file = File.join(File.dirname(__FILE__), 'config.yml')
config = Configurability::Config.load( config_file )
Configurability.configure_objects( config )
end
# monitoring aliveness endpoint
get '/heartbeat' do
'alive'
end
get '/' do
erb :index, :layout => :default, locals: {
mean_time_rows: mean_time_rows
}
end
get '/total-time' do
erb :total_time, :layout => :default, locals: {
total_time_rows: total_time_rows
}
end
get '/most-frequent' do
erb :most_frequent, :layout => :default, locals: {
most_frequent_rows: most_frequent_rows
}
end
get '/all-active' do
layout = request['X-PJAX'] ? false : :default
erb :all_active, :layout => layout
end
get '/long-running' do
erb :long_running, :layout => :default, locals: {
long_running_rows: long_running_rows
}
end
helpers do
def sql(sql)
formatter = Rouge::Formatters::HTMLInline.new( 'github' )
lexer = Rouge::Lexers::SQL.new
return formatter.format(lexer.lex(sql))
end
end
def total_time_rows
total_time_ds = self.relevant_stats_dataset.
order_by { total_time.desc }.
limit( 20 ).
select { [calls, total_time, mean_time, stddev_time, query] }
return total_time_ds.all
end
def mean_time_rows
mean_time_ds = self.relevant_stats_dataset.
order_by { mean_time.desc }.
limit( 20 ).
select { [ mean_time, total_time, stddev_time, query] }
return mean_time_ds.all
end
def most_frequent_rows
most_frequent_rows_ds = self.relevant_stats_dataset.
order_by { calls.desc }.
limit( 20 ).
select { [calls, mean_time, total_time, stddev_time, query] }
return most_frequent_rows_ds.all
end
def all_active_rows
all_active_rows_ds = self.activity_stats_dataset
return all_active_rows_ds.all
end
def long_running_rows
long_running_rows_ds = self.activity_stats_dataset.
where { query_start < Time.now - 1 }
return long_running_rows_ds.all
end
def activity_stats_dataset
return PGPerformance.db[:pg_stat_activity].
where( datname: PGPerformance.target_db ).
where( state: 'active' ).
limit( 20 ).
select { [ application_name, query_start, state, query ] }
end
def relevant_stats_dataset
return PGPerformance.db[:pg_stat_statements].join( :pg_database, oid: :dbid ).
where( datname: PGPerformance.target_db ).
exclude(query: '<insufficient privilege>').
exclude(query: 'COMMIT').
exclude(query: 'BEGIN').
exclude { query.like('%"pg_%') }
end
end