pghero 1.6.5 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of pghero might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e9c35316e3a2a8e668c93e3f6464359dddcc914e
4
- data.tar.gz: 4712b121ee072c668be2083bb9b26b356446b2a5
3
+ metadata.gz: a4607d19de5e8cf42a6dc99b6c57cdf1f960af67
4
+ data.tar.gz: 22801b6093b26d11456f885613e998ac3726ea66
5
5
  SHA512:
6
- metadata.gz: 15d0d1f9f455bd731564dea8d34556730cd0bb4bc140eead1c38b62a3453a7cb7d076148a56cb96b493beccf2a00377603f4385ed360d78b8df60157fb948a4b
7
- data.tar.gz: f0e884fd5c1c9d028ec7dca77607b37c3533aab9944397f00859f884c87bd36ddbe2f2f50a6abd3e9c72aed906a4c94e8fa2de78b11d271db0b7a0773f40fcaa
6
+ metadata.gz: 443c1324e86a101bb8434a373e8b38e3d5ea682dfbfffdad11e6c4e57b56775975f0e8f1ffe3d0ea9a0fdbfe33efcd118fe91026fb8c04719c9824a6f253dc66
7
+ data.tar.gz: 81ab2e0a5a3bab74a44a504c9d264acf982c5724c3bb568bdc461159be7f128a3d3b555b477ac538b65999edd1d491339472d10a3c656a7ebc2ee8f760a52991
@@ -1,3 +1,10 @@
1
+ ## 1.7.0
2
+
3
+ - Fixed migrations for Rails 5.1+
4
+ - Added `analyze`, `analyze_tables`, and `analyze_all` methods
5
+ - Added `pghero:analyze` rake task
6
+ - Fixed system stats display issue
7
+
1
8
  ## 1.6.5
2
9
 
3
10
  - Added support for Rails API
data/Gemfile CHANGED
@@ -3,4 +3,4 @@ source "https://rubygems.org"
3
3
  # Specify your gem's dependencies in pghero.gemspec
4
4
  gemspec
5
5
 
6
- gem "activerecord", "~> 5.0.0"
6
+ gem "activerecord", "~> 5.1.0"
@@ -177,36 +177,44 @@ module PgHero
177
177
  if @database.kill(params[:pid])
178
178
  redirect_to root_path, notice: "Query killed"
179
179
  else
180
- redirect_to :back, notice: "Query no longer running"
180
+ redirect_backward notice: "Query no longer running"
181
181
  end
182
182
  end
183
183
 
184
184
  def kill_long_running_queries
185
185
  @database.kill_long_running_queries
186
- redirect_to :back, notice: "Queries killed"
186
+ redirect_backward notice: "Queries killed"
187
187
  end
188
188
 
189
189
  def kill_all
190
190
  @database.kill_all
191
- redirect_to :back, notice: "Connections killed"
191
+ redirect_backward notice: "Connections killed"
192
192
  end
193
193
 
194
194
  def enable_query_stats
195
195
  @database.enable_query_stats
196
- redirect_to :back, notice: "Query stats enabled"
196
+ redirect_backward notice: "Query stats enabled"
197
197
  rescue ActiveRecord::StatementInvalid
198
- redirect_to :back, alert: "The database user does not have permission to enable query stats"
198
+ redirect_backward alert: "The database user does not have permission to enable query stats"
199
199
  end
200
200
 
201
201
  def reset_query_stats
202
202
  @database.reset_query_stats
203
- redirect_to :back, notice: "Query stats reset"
203
+ redirect_backward notice: "Query stats reset"
204
204
  rescue ActiveRecord::StatementInvalid
205
- redirect_to :back, alert: "The database user does not have permission to reset query stats"
205
+ redirect_backward alert: "The database user does not have permission to reset query stats"
206
206
  end
207
207
 
208
208
  protected
209
209
 
210
+ def redirect_backward(options = {})
211
+ if Rails.version >= "5.1"
212
+ redirect_back options.merge(fallback_location: root_path)
213
+ else
214
+ redirect_to :back, options
215
+ end
216
+ end
217
+
210
218
  def set_database
211
219
  @databases = PgHero.databases.values
212
220
  if params[:database]
@@ -236,7 +244,7 @@ module PgHero
236
244
  end
237
245
 
238
246
  def system_params
239
- params.slice(:duration, :period)
247
+ params.permit(:duration, :period)
240
248
  end
241
249
 
242
250
  def chart_library_options
@@ -22,7 +22,13 @@ module Pghero
22
22
  end
23
23
 
24
24
  def copy_migration
25
- migration_template "query_stats.rb", "db/migrate/create_pghero_query_stats.rb"
25
+ migration_template "query_stats.rb", "db/migrate/create_pghero_query_stats.rb", migration_version: migration_version
26
+ end
27
+
28
+ def migration_version
29
+ if ActiveRecord::VERSION::MAJOR >= 5
30
+ "[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
31
+ end
26
32
  end
27
33
  end
28
34
  end
@@ -22,7 +22,13 @@ module Pghero
22
22
  end
23
23
 
24
24
  def copy_migration
25
- migration_template "space_stats.rb", "db/migrate/create_pghero_space_stats.rb"
25
+ migration_template "space_stats.rb", "db/migrate/create_pghero_space_stats.rb", migration_version: migration_version
26
+ end
27
+
28
+ def migration_version
29
+ if ActiveRecord::VERSION::MAJOR >= 5
30
+ "[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
31
+ end
26
32
  end
27
33
  end
28
34
  end
@@ -1,4 +1,4 @@
1
- class <%= migration_class_name %> < ActiveRecord::Migration
1
+ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version %>
2
2
  def change
3
3
  create_table :pghero_query_stats do |t|
4
4
  t.text :database
@@ -1,4 +1,4 @@
1
- class <%= migration_class_name %> < ActiveRecord::Migration
1
+ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version %>
2
2
  def change
3
3
  create_table :pghero_space_stats do |t|
4
4
  t.text :database
@@ -40,7 +40,7 @@ module PgHero
40
40
 
41
41
  class << self
42
42
  extend Forwardable
43
- def_delegators :current_database, :access_key_id, :autoindex, :autoindex_all, :autovacuum_danger,
43
+ def_delegators :current_database, :access_key_id, :analyze, :analyze_tables, :autoindex, :autoindex_all, :autovacuum_danger,
44
44
  :best_index, :blocked_queries, :connection_sources, :connection_stats,
45
45
  :cpu_usage, :create_user, :database_size, :db_instance_identifier, :disable_query_stats, :drop_user,
46
46
  :duplicate_indexes, :enable_query_stats, :explain, :historical_query_stats_enabled?, :index_caching,
@@ -133,6 +133,13 @@ module PgHero
133
133
  true
134
134
  end
135
135
 
136
+ def analyze_all
137
+ databases.each do |_, database|
138
+ database.analyze_tables
139
+ end
140
+ true
141
+ end
142
+
136
143
  # Handles Rails 4 ('t') and Rails 5 (true) values.
137
144
  def truthy?(value)
138
145
  value == true || value == 't'
@@ -72,6 +72,13 @@ module PgHero
72
72
  part
73
73
  end
74
74
  end
75
+
76
+ def with_lock_timeout(timeout)
77
+ connection_model.transaction do
78
+ select_all "SET LOCAL lock_timeout = #{timeout.to_i}"
79
+ yield
80
+ end
81
+ end
75
82
  end
76
83
  end
77
84
  end
@@ -4,14 +4,13 @@ module PgHero
4
4
  def explain(sql)
5
5
  sql = squish(sql)
6
6
  explanation = nil
7
- explain_safe = explain_safe?
8
7
 
9
8
  # use transaction for safety
10
9
  connection_model.transaction do
11
10
  # protect the DB with a 10 second timeout
12
11
  # this could potentially increase the timeout, but 10 seconds should be okay
13
12
  select_all("SET LOCAL statement_timeout = 10000")
14
- if (sql.sub(/;\z/, "").include?(";") || sql.upcase.include?("COMMIT")) && !explain_safe
13
+ if (sql.sub(/;\z/, "").include?(";") || sql.upcase.include?("COMMIT")) && !explain_safe?
15
14
  raise ActiveRecord::StatementInvalid, "Unsafe statement"
16
15
  end
17
16
  explanation = select_all("EXPLAIN #{sql}").map { |v| v["QUERY PLAN"] }.join("\n")
@@ -56,6 +56,23 @@ module PgHero
56
56
  1, 2
57
57
  SQL
58
58
  end
59
+
60
+ def analyze(table)
61
+ execute "ANALYZE #{quote_table_name(table)}"
62
+ true
63
+ end
64
+
65
+ def analyze_tables
66
+ table_stats.reject { |s| %w(information_schema pg_catalog).include?(s["schema"]) }.map { |s| s.slice("schema", "table") }.each do |stats|
67
+ begin
68
+ with_lock_timeout(5000) do
69
+ analyze "#{stats["schema"]}.#{stats["table"]}"
70
+ end
71
+ rescue ActiveRecord::StatementInvalid => e
72
+ $stderr.puts e.message
73
+ end
74
+ end
75
+ end
59
76
  end
60
77
  end
61
78
  end
@@ -23,17 +23,17 @@ module PgHero
23
23
 
24
24
  def rds_stats(metric_name, options = {})
25
25
  if system_stats_enabled?
26
- options = {region: region}
26
+ aws_options = {region: region}
27
27
  if access_key_id
28
- options[:access_key_id] = access_key_id
29
- options[:secret_access_key] = secret_access_key
28
+ aws_options[:access_key_id] = access_key_id
29
+ aws_options[:secret_access_key] = secret_access_key
30
30
  end
31
31
 
32
32
  client =
33
33
  if defined?(Aws)
34
- Aws::CloudWatch::Client.new(options)
34
+ Aws::CloudWatch::Client.new(aws_options)
35
35
  else
36
- AWS::CloudWatch.new(options).client
36
+ AWS::CloudWatch.new(aws_options).client
37
37
  end
38
38
 
39
39
  duration = (options[:duration] || 1.hour).to_i
@@ -57,7 +57,7 @@ module PgHero
57
57
  pg_namespace ON pg_namespace.oid = pg_class.relnamespace
58
58
  WHERE
59
59
  relkind = 'r'
60
- AND nspname = #{quote(schema)}
60
+ #{schema ? "AND nspname = #{quote(schema)}" : nil}
61
61
  #{tables ? "AND relname IN (#{tables.map { |t| quote(t) }.join(", ")})" : nil}
62
62
  ORDER BY
63
63
  1, 2
@@ -1,3 +1,3 @@
1
1
  module PgHero
2
- VERSION = "1.6.5"
2
+ VERSION = "1.7.0"
3
3
  end
@@ -9,6 +9,11 @@ namespace :pghero do
9
9
  PgHero.capture_space_stats
10
10
  end
11
11
 
12
+ desc "analyze tables"
13
+ task analyze: :environment do
14
+ PgHero.analyze_all
15
+ end
16
+
12
17
  desc "autoindex"
13
18
  task autoindex: :environment do
14
19
  PgHero.autoindex_all(create: true)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pghero
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.5
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-20 00:00:00.000000000 Z
11
+ date: 2017-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -208,7 +208,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
208
208
  version: '0'
209
209
  requirements: []
210
210
  rubyforge_project:
211
- rubygems_version: 2.6.8
211
+ rubygems_version: 2.6.11
212
212
  signing_key:
213
213
  specification_version: 4
214
214
  summary: A performance dashboard for Postgres