pg_monitor 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aa26f1ee15de860372c7ec2ad7304a2fe5d01fb5a934e76b192d301faef0657f
4
- data.tar.gz: 2f1e2bba02213cd53e49502c2b5056963d0634e74125e342c8b1cd8e45a27a53
3
+ metadata.gz: 5c4c7d58e46ed8ee3d95495bb02fc8476bbc38b6f58e00f372b685b69691f37c
4
+ data.tar.gz: f59c2029bb6a99e861c65702ab590fdc6980e9b46eca30186629165da1b43e94
5
5
  SHA512:
6
- metadata.gz: 40a373281b77a31c5551ea8ec7b1cfc78e26d191ffc8f6730dec67d5e0bb964c4c933ac97497f29a3b9c8ceb1b93b1761511e71c2b817e204b9f53cc48be626f
7
- data.tar.gz: eb46b021df9b08fc7ff91663150173b476c9fa881a41e8a954e73617f965476b64db70d32cf1d6680abfc691fb3a4243cd24086a044b81320489bed1965e5d1e
6
+ metadata.gz: e5480cd130a99bd63468032c7cfbb45ce012b89fabbaba67495eaa418a723b08eec1b0ee67765447b84bad37211f1185d78b10244fb5f78bd46d05baaa937f34
7
+ data.tar.gz: d2a72a77da40a346dee9edc1e573c9c668dce9a291e7edb043a2e4c13e05db1518aa3dc70ec65b0868a8e20bb6f12f4b0d0a700b5b60f86b78ce86cf6f712f8c
data/README.md CHANGED
@@ -7,7 +7,7 @@ It would integrate seamlessly with a Rails application in the future, and would
7
7
 
8
8
  - View index usage statistics (last used, scan count, etc.)
9
9
  - Identify slow queries using `pg_stat_statements`
10
- - [TBD] Works with the existing Rails database configuration
10
+ - Works with the existing Rails database configuration
11
11
  - [TBD] Provides a simple HTML interface for monitoring
12
12
 
13
13
  ## Installation
@@ -50,6 +50,13 @@ Restart PostgreSQL after making the changes:
50
50
  sudo systemctl restart postgresql
51
51
  ```
52
52
 
53
+ Ensure that the extension is enabled in your database:
54
+
55
+ ```sql
56
+ CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
57
+ ```
58
+ Execute the above SQL query by connecting to your database using `psql` or any other client.
59
+
53
60
  ## Usage
54
61
 
55
62
  Mount the engine in `config/routes.rb`:
@@ -3,13 +3,29 @@ require "pg"
3
3
  module PgMonitor
4
4
  class DBConnection
5
5
  def self.connection
6
- @connection ||= PG.connect(
7
- dbname: ENV.fetch("PG_DATABASE", "pg_monitor_test"),
8
- user: ENV.fetch("PG_USER", "postgres"),
9
- password: ENV.fetch("PG_PASSWORD", "password"),
10
- host: ENV.fetch("PG_HOST", "localhost"),
11
- port: ENV.fetch("PG_PORT", "5432")
12
- )
6
+ if active_record_present?
7
+ ActiveRecord::Base.connection.raw_connection
8
+ else
9
+ @connection ||= PG.connect(connection_params)
10
+ end
11
+ end
12
+
13
+ def self.active_record_present?
14
+ defined?(ActiveRecord::Base)
15
+ end
16
+
17
+ def self.connection_params
18
+ {
19
+ dbname: ENV.fetch("PG_DATABASE"),
20
+ user: ENV.fetch("PG_USER"),
21
+ password: ENV.fetch("PG_PASSWORD"),
22
+ host: ENV.fetch("PG_HOST"),
23
+ port: ENV.fetch("PG_PORT")
24
+ }
25
+ end
26
+
27
+ def self.clear_connection!
28
+ @connection = nil
13
29
  end
14
30
  end
15
31
  end
@@ -1,7 +1,9 @@
1
1
  require_relative "db_connection"
2
2
 
3
3
  module PgMonitor
4
- class IndexUsage
4
+ module IndexUsage
5
+ module_function
6
+
5
7
  def fetch
6
8
  query = <<~SQL
7
9
  SELECT
@@ -1,7 +1,9 @@
1
1
  require_relative "db_connection"
2
2
 
3
3
  module PgMonitor
4
- class SlowQueries
4
+ module SlowQueries
5
+ module_function
6
+
5
7
  def fetch(limit: 10)
6
8
  query = <<~SQL
7
9
  SELECT
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PgMonitor
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_monitor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hassan Murtaza
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-03-07 00:00:00.000000000 Z
11
+ date: 2025-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg