ruby-pg-extras 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 771a487899452aae45b3491ef7b5c4704ff627abe1768c9342d64a6ae5c2e050
4
- data.tar.gz: 98b7ccbc8b3c74e4b623be5cc932d73110676cf8de6610ceeffa0127c25c750b
3
+ metadata.gz: 3fdc09221bd85ddacc607f1aa91aa6bd29ebe3d1f5193ec176c14a2f80739718
4
+ data.tar.gz: 2377afa5338292f230cefb60afc85e80c174412df6bac422bfb8cbab392e7d61
5
5
  SHA512:
6
- metadata.gz: 4b7f48a2907f36db39033aa81801eef6cc397c8e2670024ad2b0f5d06984792bc7cfc664e3aab34b5f6d84268dc6bb312c345a078f7914fa858eb7dfa0aa1fe2
7
- data.tar.gz: cf675a7c8364d01468602aee27961f008a33a6168ab11afd406cfaa7eb9b97c1197f6e0e455ba1801d9c4282d95502cb8041644b992e4455431e5eed6c5c4f1a
6
+ metadata.gz: a0140f50e9719f4740e99cb678edc5d78cfb2352398765a49565f4031a7d0aaea473710ef2811fcb8856acca3b6ce414648ce3f590c7beb3f7a437b37b4da3ea
7
+ data.tar.gz: fe5c2b267931dcbf4993576da5721d7bfc2435b5a16f050d7494942c334a80484c1a9b39708782574e6138ed1ad8490765308f227844744aeca39a5e440aa119
@@ -0,0 +1,29 @@
1
+ version: 2
2
+ jobs:
3
+ test:
4
+ docker:
5
+ - image: circleci/ruby:2.6.5
6
+ environment:
7
+ DATABASE_URL: postgresql://postgres:secret@localhost:5432/ruby-pg-extras-test
8
+ - image: circleci/postgres:11.5
9
+ environment:
10
+ POSTGRES_USER: postgres
11
+ POSTGRES_DB: ruby-pg-extras-test
12
+ POSTGRES_PASSWORD: secret
13
+ parallelism: 1
14
+ steps:
15
+ - checkout
16
+ - run: gem update --system
17
+ - run: gem install bundler
18
+ - run: bundle install --path vendor/bundle
19
+ - run: sudo apt install postgresql-client
20
+ - run: dockerize -wait tcp://localhost:5432 -timeout 1m
21
+ - run:
22
+ name: Run specs
23
+ command: |
24
+ bundle exec rspec spec/
25
+ workflows:
26
+ version: 2
27
+ test:
28
+ jobs:
29
+ - test
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # Rails PG Extras [![Gem Version](https://badge.fury.io/rb/ruby-pg-extras.svg)](https://badge.fury.io/rb/ruby-pg-extras) [![CircleCI](https://circleci.com/gh/pawurb/ruby-pg-extras.svg?style=svg)](https://circleci.com/gh/pawurb/ruby-pg-extras)
1
+ # Ruby PG Extras [![Gem Version](https://badge.fury.io/rb/ruby-pg-extras.svg)](https://badge.fury.io/rb/ruby-pg-extras) [![CircleCI](https://circleci.com/gh/pawurb/ruby-pg-extras.svg?style=svg)](https://circleci.com/gh/pawurb/ruby-pg-extras)
2
2
 
3
- Rails port of [Heroku PG Extras](https://github.com/heroku/heroku-pg-extras). The goal of this project is to provide a powerful insights into PostgreSQL database for Ruby on Rails apps that are not using the default Heroku PostgreSQL plugin.
3
+ Ruby port of [Heroku PG Extras](https://github.com/heroku/heroku-pg-extras). The goal of this project is to provide a powerful insights into PostgreSQL database for Ruby on Ruby apps that are not using the default Heroku PostgreSQL plugin.
4
4
 
5
5
  Included rake tasks and Ruby methods can be used to obtain information about a Postgres instance, that may be useful when analyzing performance issues. This includes information about locks, index usage, buffer cache hit ratios and vacuum statistics. Ruby API enables developers to easily integrate the tool into e.g. automatic monitoring tasks.
6
6
 
@@ -14,12 +14,6 @@ gem 'ruby-pg-extras'
14
14
 
15
15
  ### Usage
16
16
 
17
- Each command can be used as a rake task, or a directly from the Ruby code.
18
-
19
- ```bash
20
- rake pg_extras:cache_hit
21
- ```
22
-
23
17
  ```ruby
24
18
  RubyPGExtras.cache_hit
25
19
  ```
@@ -55,8 +49,10 @@ RubyPGExtras.cache_hit(in_format: :raw) =>
55
49
 
56
50
  #### `cache_hit`
57
51
 
58
- ```bash
59
- $ rake pg_extras:cache_hit
52
+ ```ruby
53
+
54
+ RubyPGExtras.cache_hit
55
+
60
56
  name | ratio
61
57
  ----------------+------------------------
62
58
  index hit rate | 0.99957765013541945832
@@ -68,8 +64,10 @@ This command provides information on the efficiency of the buffer cache, for bot
68
64
 
69
65
  #### `index_usage`
70
66
 
71
- ```
72
- $ rake pg_extras:index_usage
67
+ ```ruby
68
+
69
+ RubyPGExtras.index_usage
70
+
73
71
  relname | percent_of_times_index_used | rows_in_table
74
72
  ---------------------+-----------------------------+---------------
75
73
  events | 65 | 1217347
@@ -84,8 +82,10 @@ This command provides information on the efficiency of indexes, represented as w
84
82
 
85
83
  ### `locks`
86
84
 
87
- ```
88
- $ rake pg_extras:locks
85
+ ```ruby
86
+
87
+ RubyPGExtras.locks
88
+
89
89
  procpid | relname | transactionid | granted | query_snippet | age
90
90
  ---------+---------+---------------+---------+-----------------------+-----------------
91
91
  31776 | | | t | <IDLE> in transaction | 00:19:29.837898
@@ -101,16 +101,20 @@ This command displays queries that have taken out an exlusive lock on a relation
101
101
 
102
102
  ### `all_locks`
103
103
 
104
- ```
105
- $ rake pg_extras:all_locks
104
+ ```ruby
105
+
106
+ RubyPGExtras.all_locks
107
+
106
108
  ```
107
109
 
108
110
  This command displays all the current locks, regardless of their type.
109
111
 
110
112
  ### `outliers`
111
113
 
112
- ```
113
- $ rake pg_extras:outliers
114
+ ```ruby
115
+
116
+ RubyPGExtras.outliers
117
+
114
118
  qry | exec_time | prop_exec_time | ncalls | sync_io_time
115
119
  -----------------------------------------+------------------+----------------+-------------+--------------
116
120
  SELECT * FROM archivable_usage_events.. | 154:39:26.431466 | 72.2% | 34,211,877 | 00:00:00
@@ -132,8 +136,10 @@ Typically, an efficient query will have an appropriate ratio of calls to total e
132
136
 
133
137
  ### `calls`
134
138
 
135
- ```
136
- $ rake pg_extras:calls
139
+ ```ruby
140
+
141
+ RubyPGExtras.calls
142
+
137
143
  qry | exec_time | prop_exec_time | ncalls | sync_io_time
138
144
  -----------------------------------------+------------------+----------------+-------------+--------------
139
145
  SELECT * FROM usage_events WHERE (alp.. | 01:18:11.073333 | 0.6% | 102,120,780 | 00:00:00
@@ -153,8 +159,10 @@ This command is much like `pg:outliers`, but ordered by the number of times a st
153
159
 
154
160
  ### `blocking`
155
161
 
156
- ```
157
- $ rake pg_extras:blocking
162
+ ```ruby
163
+
164
+ RubyPGExtras.blocking
165
+
158
166
  blocked_pid | blocking_statement | blocking_duration | blocking_pid | blocked_statement | blocked_duration
159
167
  -------------+--------------------------+-------------------+--------------+------------------------------------------------------------------------------------+------------------
160
168
  461 | select count(*) from app | 00:00:03.838314 | 15682 | UPDATE "app" SET "updated_at" = '2013-03-04 15:07:04.746688' WHERE "id" = 12823149 | 00:00:03.821826
@@ -165,8 +173,10 @@ This command displays statements that are currently holding locks that other sta
165
173
 
166
174
  #### `total_index_size`
167
175
 
168
- ```
169
- $ rake pg_extras:total_index_size
176
+ ```ruby
177
+
178
+ RubyPGExtras.total_index_size
179
+
170
180
  size
171
181
  -------
172
182
  28194 MB
@@ -177,8 +187,10 @@ This command displays the total size of all indexes on the database, in MB. It i
177
187
 
178
188
  ### `index_size`
179
189
 
180
- ```
181
- $ rake pg_extras:index_size
190
+ ```ruby
191
+
192
+ RubyPGExtras.index_size
193
+
182
194
  name | size
183
195
  ---------------------------------------------------------------+---------
184
196
  idx_activity_attemptable_and_type_lesson_enrollment | 5196 MB
@@ -203,8 +215,10 @@ This command displays the size of each each index in the database, in MB. It is
203
215
 
204
216
  ### `table_size`
205
217
 
206
- ```
207
- $ rake pg_extras:table_size
218
+ ```ruby
219
+
220
+ RubyPGExtras.table_size
221
+
208
222
  name | size
209
223
  ---------------------------------------------------------------+---------
210
224
  learning_coaches | 196 MB
@@ -219,8 +233,10 @@ This command displays the size of each table in the database, in MB. It is calcu
219
233
 
220
234
  ### `table_indexes_size`
221
235
 
222
- ```
223
- $ rake pg_extras:table-indexes-size
236
+ ```ruby
237
+
238
+ RubyPGExtras.table_indexes_size
239
+
224
240
  table | indexes_size
225
241
  ---------------------------------------------------------------+--------------
226
242
  learning_coaches | 153 MB
@@ -235,8 +251,10 @@ This command displays the total size of indexes for each table, in MB. It is cal
235
251
 
236
252
  ### `total_table_size`
237
253
 
238
- ```
239
- $ rake pg_extras:total_table_size
254
+ ```ruby
255
+
256
+ RubyPGExtras.total_table_size
257
+
240
258
  name | size
241
259
  ---------------------------------------------------------------+---------
242
260
  learning_coaches | 349 MB
@@ -251,8 +269,10 @@ This command displays the total size of each table in the database, in MB. It is
251
269
 
252
270
  ### `unused_indexes`
253
271
 
254
- ```
255
- $ rake pg_extras:unused_indexes
272
+ ```ruby
273
+
274
+ RubyPGExtras.unused_indexes
275
+
256
276
  table | index | index_size | index_scans
257
277
  ---------------------+--------------------------------------------+------------+-------------
258
278
  public.grade_levels | index_placement_attempts_on_grade_level_id | 97 MB | 0
@@ -265,8 +285,10 @@ This command displays indexes that have < 50 scans recorded against them, and ar
265
285
 
266
286
  ### `seq_scans`
267
287
 
268
- ```
269
- $ rake pg_extras:seq_scans
288
+ ```ruby
289
+
290
+ RubyPGExtras.seq_scans
291
+
270
292
 
271
293
  name | count
272
294
  -----------------------------------+----------
@@ -293,8 +315,10 @@ This command displays the number of sequential scans recorded against all tables
293
315
 
294
316
  ### long_running_queries
295
317
 
296
- ```
297
- $ rake pg_extras:long_running_queries
318
+ ```ruby
319
+
320
+ RubyPGExtras.long_running_queries
321
+
298
322
 
299
323
  pid | duration | query
300
324
  -------+-----------------+---------------------------------------------------------------------------------------
@@ -308,8 +332,10 @@ This command displays currently running queries, that have been running for long
308
332
 
309
333
  ### records_rank
310
334
 
311
- ```
312
- $ rake pg_extras:records_rank
335
+ ```ruby
336
+
337
+ RubyPGExtras.records_rank
338
+
313
339
  name | estimated_count
314
340
  -----------------------------------+-----------------
315
341
  tastypie_apiaccess | 568891
@@ -325,8 +351,10 @@ This command displays an estimated count of rows per table, descending by estima
325
351
 
326
352
  ### bloat
327
353
 
328
- ```
329
- $ rake pg_extras:bloat
354
+ ```ruby
355
+
356
+ RubyPGExtras.bloat
357
+
330
358
 
331
359
  type | schemaname | object_name | bloat | waste
332
360
  -------+------------+-------------------------------+-------+----------
@@ -341,8 +369,10 @@ This command displays an estimation of table "bloat" – space allocated to a re
341
369
 
342
370
  ### vacuum_stats
343
371
 
344
- ```
345
- $ rake pg_extras:vacuum_stats
372
+ ```ruby
373
+
374
+ RubyPGExtras.vacuum_stats
375
+
346
376
  schema | table | last_vacuum | last_autovacuum | rowcount | dead_rowcount | autovacuum_threshold | expect_autovacuum
347
377
  --------+-----------------------+-------------+------------------+----------------+----------------+----------------------+-------------------
348
378
  public | log_table | | 2013-04-26 17:37 | 18,030 | 0 | 3,656 |
@@ -356,8 +386,10 @@ This command displays statistics related to vacuum operations for each table, in
356
386
 
357
387
  ### mandelbrot
358
388
 
359
- ```
360
- $ rake pg_extras:mandelbrot
389
+ ```ruby
390
+
391
+ RubyPGExtras.mandelbrot
392
+
361
393
  ```
362
394
 
363
395
  This command outputs the Mandelbrot set, calculated through SQL.
@@ -1,3 +1,3 @@
1
1
  module RubyPGExtras
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,8 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'terminal-table'
4
+ require 'uri'
5
+ require 'pg'
4
6
 
5
7
  module RubyPGExtras
8
+ @@database_url = nil
9
+
6
10
  QUERIES = %i(
7
11
  bloat blocking cache_hit
8
12
  calls extensions
@@ -23,7 +27,7 @@ module RubyPGExtras
23
27
  end
24
28
 
25
29
  def self.run_query(query_name:, in_format:)
26
- result = connection.execute(
30
+ result = connection.exec(
27
31
  sql_for(query_name: query_name)
28
32
  )
29
33
 
@@ -78,7 +82,23 @@ module RubyPGExtras
78
82
  end
79
83
 
80
84
  def self.connection
81
- ActiveRecord::Base.connection
85
+ database_uri = URI.parse(database_url)
86
+
87
+ @_connection ||= PG.connect(
88
+ dbname: database_uri.path[1..-1],
89
+ host: database_uri.host,
90
+ port: database_uri.port,
91
+ user: database_uri.user,
92
+ password: database_uri.password
93
+ )
94
+ end
95
+
96
+ def self.database_url=(value)
97
+ @@database_url = value
98
+ end
99
+
100
+ def self.database_url
101
+ @@database_url || ENV.fetch("DATABASE_URL")
82
102
  end
83
103
 
84
104
  %i(
data/spec/smoke_spec.rb CHANGED
@@ -25,4 +25,13 @@ describe RubyPGExtras do
25
25
  end.not_to raise_error
26
26
  end
27
27
  end
28
+
29
+ describe "#database_url=" do
30
+ it "setting custom database URL works" do
31
+ RubyPGExtras.database_url = ENV.fetch("DATABASE_URL")
32
+ expect do
33
+ RubyPGExtras.bloat(in_format: :hash)
34
+ end.not_to raise_error
35
+ end
36
+ end
28
37
  end
data/spec/spec_helper.rb CHANGED
@@ -5,15 +5,3 @@ require 'bundler/setup'
5
5
  require_relative '../lib/ruby-pg-extras'
6
6
 
7
7
  ENV["DATABASE_URL"] ||= "postgresql://postgres:secret@localhost:5432/ruby-pg-extras-test"
8
-
9
- RSpec.configure do |config|
10
- config.before :suite do
11
- ActiveRecord::Base.establish_connection(
12
- ENV.fetch("DATABASE_URL")
13
- )
14
- end
15
-
16
- config.after :suite do
17
- ActiveRecord::Base.remove_connection
18
- end
19
- end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-pg-extras
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - pawurb
@@ -75,6 +75,7 @@ executables: []
75
75
  extensions: []
76
76
  extra_rdoc_files: []
77
77
  files:
78
+ - ".circleci/config.yml"
78
79
  - ".gitignore"
79
80
  - Gemfile
80
81
  - LICENSE.txt
@@ -101,7 +102,6 @@ files:
101
102
  - lib/ruby-pg-extras/queries/total_table_size.sql
102
103
  - lib/ruby-pg-extras/queries/unused_indexes.sql
103
104
  - lib/ruby-pg-extras/queries/vacuum_stats.sql
104
- - lib/ruby-pg-extras/tasks/all.rake
105
105
  - lib/ruby-pg-extras/version.rb
106
106
  - ruby-pg-extras.gemspec
107
107
  - spec/smoke_spec.rb
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'ruby-pg-extras'
4
-
5
- namespace :pg_extras do
6
- RubyPGExtras::QUERIES.each do |query_name|
7
- desc RubyPGExtras.description_for(query_name: query_name)
8
- task query_name.to_sym => :environment do
9
- RubyPGExtras.public_send(query_name)
10
- end
11
- end
12
- end