pghero 0.1.4 → 0.1.5

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: 42d5f779562b89764cd14697f3929b48407b2902
4
- data.tar.gz: e89c108a39b3eeb72d8e8c36bcfa83b3243a9410
3
+ metadata.gz: dc8ca524f848315726b63a987ca3a39ca7768d13
4
+ data.tar.gz: 230b213962882f7a62615f90fc9bc6b45cc97da5
5
5
  SHA512:
6
- metadata.gz: 9b04c5e26d00034f12600df994dc6b6c86195cf777dbcfd4e552dd10d41020de4747d8f50fea104d595aaa5d8579b9a868124f84c968b6288f107939bf7464b8
7
- data.tar.gz: 8ab45421d0d4a71e38d26916cef3747f360e581fdf5fd7ef4507ffdbbcc8ad9d374cfc0df772a976a29e36fd01d29365bfe6a8e06071977e929ab9b428b613d3
6
+ metadata.gz: 74956a91d498cf2b68d7f3dd261d7b727b7695d84a89851514ab8d31ce617f0c6005e18901a792aeb92c12e718ea4847518553ca74c6304f31f4b9aab7af2483
7
+ data.tar.gz: 0f63ae5e71393fab956c8d2df185acb4abbe3ce1aaba3b0fed637fc7404011f538b7070c8a0598824968fc7243bdce9845f8cf837b255a64cee3cc7ee5c2eae6
@@ -1,3 +1,10 @@
1
+ ## 0.1.5
2
+
3
+ - Added system stats for Amazon RDS
4
+ - Added code to remove unused indexes
5
+ - Require unused indexes to be at least 1 MB
6
+ - Use `pg_terminate_backend` to ensure queries are killed
7
+
1
8
  ## 0.1.4
2
9
 
3
10
  - Reduced long running queries threshold to 1 minute
data/README.md CHANGED
@@ -121,6 +121,23 @@ and reset stats with:
121
121
  SELECT pg_stat_statements_reset();
122
122
  ```
123
123
 
124
+ ## System Stats
125
+
126
+ System stats are available for Amazon RDS. Add these lines to your application’s Gemfile:
127
+
128
+ ```ruby
129
+ gem 'aws-sdk'
130
+ gem 'chartkick'
131
+ ```
132
+
133
+ And add these variables to your environment:
134
+
135
+ ```sh
136
+ PGHERO_ACCESS_KEY_ID=accesskey123
137
+ PGHERO_SECRET_ACCESS_KEY=secret123
138
+ PGHERO_DB_INSTANCE_IDENTIFIER=datakick-production
139
+ ```
140
+
124
141
  ## TODO
125
142
 
126
143
  - show exactly which indexes to add
@@ -42,6 +42,11 @@ module PgHero
42
42
  @query_stats = PgHero.query_stats
43
43
  end
44
44
 
45
+ def system_stats
46
+ @title = "System Stats"
47
+ @cpu_usage = PgHero.cpu_usage
48
+ end
49
+
45
50
  def explain
46
51
  @title = "Explain"
47
52
  @query = params[:query]
@@ -91,6 +96,7 @@ module PgHero
91
96
 
92
97
  def set_query_stats_enabled
93
98
  @query_stats_enabled = PgHero.query_stats_enabled?
99
+ @system_stats_enabled = PgHero.system_stats_enabled?
94
100
  end
95
101
 
96
102
  end
@@ -349,6 +349,9 @@
349
349
  <% if @query_stats_enabled %>
350
350
  <li class="<%= controller.action_name == "query_stats" ? "active" : "" %>"><%= link_to "Queries", query_stats_path %></li>
351
351
  <% end %>
352
+ <% if @system_stats_enabled %>
353
+ <li class="<%= controller.action_name == "system_stats" ? "active" : "" %>"><%= link_to "System", system_stats_path %></li>
354
+ <% end %>
352
355
  <li class="<%= controller.action_name == "indexes" ? "active" : "" %>"><%= link_to "Indexes", indexes_path %></li>
353
356
  <li class="<%= controller.action_name == "space" ? "active" : "" %>"><%= link_to "Space", space_path %></li>
354
357
  <li class="<%= controller.action_name == "explain" ? "active" : "" %>"><%= link_to "Explain", explain_path %></li>
@@ -126,7 +126,18 @@ pg_stat_statements.track = all</pre>
126
126
  <div class="content">
127
127
  <h1>Unused Indexes</h1>
128
128
 
129
- <p>Unused indexes cause unnecessary overhead. Remove them for faster writes.</p>
129
+ <p>
130
+ Unused indexes cause unnecessary overhead. Remove them
131
+ <a href="javascript: void(0);" onclick="document.getElementById('migration').style.display = 'block';">with a migration</a>
132
+ for faster writes.
133
+ </p>
134
+
135
+ <div id="migration" style="display: none;">
136
+ <pre>rails g migration remove_unused_indexes</pre>
137
+ <p>And paste</p>
138
+ <pre style="overflow: scroll; white-space: pre; word-break: normal;"><% @unused_indexes.each do |query| %>
139
+ remove_index <%= query["table"].to_sym.inspect %>, name: <%= query["index"].to_s.inspect %><% end %></pre>
140
+ </div>
130
141
 
131
142
  <table class="table">
132
143
  <thead>
@@ -0,0 +1,6 @@
1
+ <div class="content">
2
+ <h1>CPU Usage</h1>
3
+
4
+ <%= javascript_include_tag "//www.google.com/jsapi", "chartkick" %>
5
+ <div style="margin-bottom: 20px;"><%= line_chart @cpu_usage %></div>
6
+ </div>
@@ -4,6 +4,7 @@ PgHero::Engine.routes.draw do
4
4
  get "space", to: "home#space"
5
5
  get "queries", to: "home#queries"
6
6
  get "query_stats", to: "home#query_stats"
7
+ get "system_stats", to: "home#system_stats"
7
8
  get "explain", to: "home#explain"
8
9
 
9
10
  post "kill", to: "home#kill"
@@ -137,9 +137,9 @@ module PgHero
137
137
  pg_index i ON ui.indexrelid = i.indexrelid
138
138
  WHERE
139
139
  NOT indisunique
140
- AND idx_scan < 50 AND pg_relation_size(relid) > 5 * 8192
140
+ AND idx_scan < 50
141
+ AND pg_relation_size(i.indexrelid) > 1024 * 1024
141
142
  ORDER BY
142
- pg_relation_size(i.indexrelid) / nullif(idx_scan, 0) DESC NULLS FIRST,
143
143
  pg_relation_size(i.indexrelid) DESC,
144
144
  relname ASC
145
145
  }
@@ -170,7 +170,7 @@ module PgHero
170
170
  end
171
171
 
172
172
  def kill(pid)
173
- execute("SELECT pg_cancel_backend(#{pid.to_i})").first["pg_cancel_backend"] == "t"
173
+ execute("SELECT pg_terminate_backend(#{pid.to_i})").first["pg_terminate_backend"] == "t"
174
174
  end
175
175
 
176
176
  def kill_all
@@ -270,6 +270,45 @@ module PgHero
270
270
  end
271
271
  end
272
272
 
273
+ def cpu_usage
274
+ if system_stats_enabled?
275
+ cw = AWS::CloudWatch.new(access_key_id: access_key_id, secret_access_key: secret_access_key)
276
+ now = Time.now
277
+ resp = cw.client.get_metric_statistics(
278
+ namespace: "AWS/RDS",
279
+ metric_name: "CPUUtilization",
280
+ dimensions: [{name: "DBInstanceIdentifier", value: db_instance_identifier}],
281
+ start_time: (now - 1 * 3600).iso8601,
282
+ end_time: now.iso8601,
283
+ period: 60,
284
+ statistics: ["Average"]
285
+ )
286
+ data = {}
287
+ resp[:datapoints].sort_by{|d| d[:timestamp] }.each do |d|
288
+ data[d[:timestamp]] = d[:average]
289
+ end
290
+ data
291
+ else
292
+ {}
293
+ end
294
+ end
295
+
296
+ def system_stats_enabled?
297
+ !!(defined?(AWS) && access_key_id && secret_access_key && db_instance_identifier)
298
+ end
299
+
300
+ def access_key_id
301
+ ENV["PGHERO_ACCESS_KEY_ID"] || ENV["AWS_ACCESS_KEY_ID"]
302
+ end
303
+
304
+ def secret_access_key
305
+ ENV["PGHERO_SECRET_ACCESS_KEY"] || ENV["AWS_SECRET_ACCESS_KEY"]
306
+ end
307
+
308
+ def db_instance_identifier
309
+ ENV["PGHERO_DB_INSTANCE_IDENTIFIER"]
310
+ end
311
+
273
312
  # TODO better RDS check
274
313
  def rds?
275
314
  !!(Connection.connection_config[:host].to_s =~ /rds\.amazonaws\.com\z/)
@@ -1,3 +1,3 @@
1
1
  module PgHero
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
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: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-31 00:00:00.000000000 Z
11
+ date: 2014-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -103,6 +103,7 @@ files:
103
103
  - app/views/pg_hero/home/queries.html.erb
104
104
  - app/views/pg_hero/home/query_stats.html.erb
105
105
  - app/views/pg_hero/home/space.html.erb
106
+ - app/views/pg_hero/home/system_stats.html.erb
106
107
  - config/routes.rb
107
108
  - gemfiles/activerecord31.gemfile
108
109
  - gemfiles/activerecord32.gemfile