rails-pg-extras-web 1.5.3 → 2.2.0

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: d0867a93537ef7b75064a408a632ab466f1ff2e8163de3b6dc604b3bcaad9bbe
4
- data.tar.gz: 2ad9ef98329c15accb2e02974fed7101611b95cae8dbf77bef77be9caa9ef6a1
3
+ metadata.gz: a2a3e9fef31d95866c584c87bb8f01cc3f150f086500b01d0dbb05ebfc6d9533
4
+ data.tar.gz: 7d1974687a38bd6792dab2aeacaeec4375e391052bb029c5dfd8dfc66f303085
5
5
  SHA512:
6
- metadata.gz: e1cbdfad662c7da11b61d4675cab7eaee7c5498bda223771fa634cb5b8498da5f2bfe7c378fb370363d22c3404367e4dc1c63f8debca87ff70d81df2a6ce8dd6
7
- data.tar.gz: a1331dbb06b9017fd36b3776f543a91fef069b62722cb31f3d229a2c6a05d8f6ba3b5aff6c1b45587de3cceebffd71dac4c75ee3be871fdefe437e1ddb143a45
6
+ metadata.gz: 92f4793d25c4edaf1943daa7cb36af925b74ce9219ee7c87e410959d902ea2b0291b533011468c4f7e4bf68409af2c121e2288216cb3cfcc656ff292a3c24235
7
+ data.tar.gz: 249851bc75a35aa16f904e998ac96d461e8f01b8ab6c875b6c15414b8af0bd808e6b7520e97835f2801b46e458b8c16bd0e1000d2683db0238d99dcfb552090e
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # RailsPgExtrasWeb
2
- Web UI for rails-pg-extras-web
2
+ Web UI for [rails-pg-extras](https://github.com/pawurb/rails-pg-extras)
3
3
 
4
4
  ## Installation
5
5
  Add this line to your application's Gemfile:
@@ -1,36 +1,50 @@
1
1
  module RailsPgExtrasWeb
2
2
  class QueriesController < ApplicationController
3
+ REQUIRED_EXTENSIONS = {
4
+ pg_stat_statements: %i[calls outliers],
5
+ pg_buffercache: %i[buffercache_stats buffercache_usage],
6
+ sslinfo: %i[ssl_used]
7
+ }
8
+
3
9
  before_action :load_queries
4
- helper_method :pg_stats_statements_enabled?
10
+ helper_method :unavailable_extensions
5
11
 
6
12
  def index
7
- @query_name = params[:query_name]&.to_sym.presence_in(@all_queries.keys)
8
- return unless @query_name
13
+ if params[:query_name].present?
14
+ @query_name = params[:query_name].to_sym.presence_in(@all_queries.keys)
15
+ return unless @query_name
16
+
17
+ begin
18
+ @result = RailsPGExtras.run_query(query_name: @query_name.to_sym, in_format: :raw)
19
+ rescue ActiveRecord::StatementInvalid => e
20
+ @error = e.message
21
+ end
9
22
 
10
- @result = RailsPGExtras.run_query(query_name: @query_name.to_sym, in_format: :raw)
11
- rescue ActiveRecord::StatementInvalid => e
12
- @error = e.message
23
+ render :show
24
+ end
13
25
  end
14
26
 
15
27
  private
16
28
 
17
29
  def load_queries
18
- @all_queries = {}
30
+ @all_queries = RailsPGExtras::QUERIES.inject({}) do |memo, query_name|
31
+ unless query_name.in? %i[kill_all mandelbrot]
32
+ memo[query_name] = { disabled: query_disabled?(query_name) }
33
+ end
19
34
 
20
- ::RailsPGExtras::QUERIES.each do |query_name|
21
- @all_queries[query_name] = {
22
- disabled: query_disabled?(query_name),
23
- command: query_name == :kill_all
24
- }
35
+ memo
25
36
  end
26
37
  end
27
38
 
28
39
  def query_disabled?(query_name)
29
- query_name.in?([:calls, :outliers]) && !pg_stats_statements_enabled?
40
+ unavailable_extensions.values.flatten.include?(query_name)
30
41
  end
31
42
 
32
- def pg_stats_statements_enabled?
33
- ActiveRecord::Base.connection.extensions.include?("pg_stat_statements")
43
+ def unavailable_extensions
44
+ return @unavailable_extensions if defined?(@unavailable_extensions)
45
+
46
+ enabled_extensions = ActiveRecord::Base.connection.extensions
47
+ @unavailable_extensions = REQUIRED_EXTENSIONS.delete_if { |ext| ext.to_s.in?(enabled_extensions) }
34
48
  end
35
49
  end
36
50
  end
@@ -1,19 +1,10 @@
1
1
  <!DOCTYPE html>
2
2
  <html>
3
3
  <head>
4
- <title>PG::Extras</title>
4
+ <title><%= content_for :title %></title>
5
+ <link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
5
6
  </head>
6
- <body onload="document.forms.queries.query_name.focus()">
7
- <%= form_with url: queries_path, id: "queries", method: :get do |f| %>
8
- <%= f.select :query_name, options_for_select(@all_queries.except(:kill_all, :mandelbrot), params[:query_name]),
9
- prompt: "--- select query ---"
10
- %>
11
-
12
- <button type="submit">Run</button>
13
- <% end %>
14
-
15
- <div style='margin-top: 20px'>
16
- <%= yield %>
17
- </div>
7
+ <body class="p-5 text-xs">
8
+ <%= yield %>
18
9
  </body>
19
10
  </html>
@@ -0,0 +1,22 @@
1
+ <h1 class="font-bold text-xl my-5"><%= title %></h1>
2
+
3
+ <table class="w-full font-mono border-collapse border border-green-800 my-5">
4
+ <thead>
5
+ <tr class="bg-gray-300">
6
+ <% headers.each do |header| %>
7
+ <th class="p-2 border text-left"><%= header %></th>
8
+ <% end %>
9
+ </tr>
10
+ </thead>
11
+ <tbody>
12
+ <% rows.each.with_index do |row, i| %>
13
+ <tr class="hover:bg-gray-400 hover:text-white <%= i.even? ? "bg-gray-100" : "bg-gray-200" %>">
14
+ <% row.each do |column| %>
15
+ <td class="p-1 border"><%= column %></td>
16
+ <% end %>
17
+ </tr>
18
+ <% end %>
19
+ </tbody>
20
+ </table>
21
+
22
+ <span class="italic">run_at: <%= Time.now.utc %></span>
@@ -0,0 +1,48 @@
1
+ <div class="text-red-500 p-3 font-mono my-5">
2
+ <% unavailable_extensions.each do |extension, queries| %>
3
+ WARNING: Queries <%= queries.map { |q| "<b><u>#{q}</u></b>" }.join(", ").html_safe %> require extension: <b><%= extension %></b>
4
+ <br>
5
+ <% end %>
6
+ </div>
7
+
8
+
9
+ <span id="rails" class="font-mono font-bold p-3 bg-gray-100 cursor-pointer select-none">RAILS</span> <span id="sql" class="font-mono font-normal p-3 bg-gray-50 cursor-pointer select-none">SQL</span>
10
+ <pre id="rails_code" class="font-mono p-5 bg-gray-100 block">
11
+ <% unavailable_extensions.each do |extension, queries| %>
12
+ ActiveRecord::Base.connection.enable_extension "<%= extension %>"
13
+ <% end %>
14
+ </pre>
15
+
16
+ <pre id="sql_code" class="font-mono p-5 bg-gray-100 hidden">
17
+ <% unavailable_extensions.each do |extension, queries| %>
18
+ CREATE EXTENSION <%= extension %>;
19
+ <% end %>
20
+ </pre>
21
+
22
+ <script>
23
+ const rails = document.getElementById('rails')
24
+ const sql = document.getElementById('sql')
25
+ const railsCode = document.getElementById('rails_code')
26
+ const sqlCode = document.getElementById('sql_code')
27
+
28
+ rails.addEventListener('click', (e) => {
29
+ railsCode.classList.remove('hidden')
30
+ sqlCode.classList.add('hidden')
31
+
32
+ rails.classList.replace('bg-gray-50','bg-gray-100')
33
+ sql.classList.replace('bg-gray-100','bg-gray-50')
34
+ rails.classList.replace('font-normal','font-bold')
35
+ sql.classList.replace('font-bold','font-normal')
36
+ })
37
+
38
+ sql.addEventListener('click', (e) => {
39
+ railsCode.classList.add('hidden')
40
+ sqlCode.classList.remove('hidden')
41
+
42
+ rails.classList.replace('bg-gray-100','bg-gray-50')
43
+ sql.classList.replace('bg-gray-50','bg-gray-100')
44
+ rails.classList.replace('font-bold','font-normal')
45
+ sql.classList.replace('font-normal','font-bold')
46
+ })
47
+ </script>
48
+
@@ -1,70 +1,4 @@
1
- <% unless pg_stats_statements_enabled? %>
2
- <span style='color: red'>
3
- WARNING: <b><u>outliers</u></b>, <b><u>calls</u></b> require extension: <b>pg_stat_statements</b>
4
- </span>
1
+ <%= content_for :title, "pg_extras" %>
2
+ <%= render "rails_pg_extras_web/shared/queries_selector" %>
5
3
 
6
- <h4>Rails</h4>
7
- <pre style='background-color: #e2e2e2; padding: 10px'>ActiveRecord::Base.connection.enable_extension "pg_stat_statements"</pre>
8
- <h4>SQL</h4>
9
- <pre style='background-color: #e2e2e2; padding: 10px'>CREATE EXTENSION pg_stat_statements</pre>
10
- <% end %>
11
-
12
- <% if @error %>
13
- <span style='color: red'><%= @error %></span>
14
- <% else %>
15
- <% if @result && @result.any? %>
16
- <table>
17
- <caption><%= RubyPGExtras.description_for(query_name: @query_name) %></caption>
18
- <thead>
19
- <tr>
20
- <% @result[0].keys.each do |header| %>
21
- <th><%= header %></th>
22
- <% end %>
23
- </tr>
24
- </thead>
25
- <tbody>
26
- <% @result.values.each do |row| %>
27
- <tr>
28
- <% row.each do |column| %>
29
- <td><%= column %></td>
30
- <% end %>
31
- </tr>
32
- <% end %>
33
- </tbody>
34
- </table>
35
-
36
- <pre>run_at: <%= Time.now.utc %></pre>
37
- <% else %>
38
- <pre>No results</pre>
39
- <% end %>
40
- <% end %>
41
-
42
- <style media="screen">
43
- table {
44
- font-family: monospace;
45
- border-collapse: collapse;
46
- width: 100%;
47
- }
48
-
49
- table th {
50
- background-color: #c9c9;
51
- }
52
-
53
- table th, table td {
54
- text-align: left;
55
- border: 1px solid #c9c9;
56
- padding: 3px;
57
- }
58
-
59
- table td {
60
- overflow: hidden;
61
- text-overflow: ellipsis;
62
- white-space: nowrap;
63
- }
64
-
65
- table caption {
66
- padding: 5px;
67
- font-size: 15px;
68
- font-weight: bold;
69
- }
70
- </style>
4
+ <%= render "unavailable_extensions_warning" if unavailable_extensions.any? %>
@@ -0,0 +1,24 @@
1
+ <%= content_for :title, params[:query_name].presence || "pg_extras" %>
2
+ <%= render "rails_pg_extras_web/shared/queries_selector" %>
3
+
4
+ <% if @error %>
5
+ <span class="text-red"><%= @error %></span>
6
+ <% else %>
7
+ <% if @result&.any? %>
8
+ <%= render "result",
9
+ title: RubyPGExtras.description_for(query_name: @query_name),
10
+ headers: @result[0].keys,
11
+ rows: @result.values
12
+ %>
13
+ <% else %>
14
+ <div class="font-mono p-3 bg-gray-100 mt-3">No results</div>
15
+ <% end %>
16
+ <% end %>
17
+
18
+ <style>
19
+ @media print {
20
+ form {
21
+ display: none
22
+ }
23
+ }
24
+ </style>
@@ -0,0 +1,11 @@
1
+ <%= form_with url: queries_path, id: "queries", method: :get do |f| %>
2
+ <%= f.select :query_name, options_for_select(@all_queries, params[:query_name]),
3
+ {prompt: "--- select query ---"},
4
+ {class: "border p-2 font-bold", autofocus: true}
5
+ %>
6
+ <% end %>
7
+ <script>
8
+ document.getElementById('queries').addEventListener('change', (e) => {
9
+ e.target.form.submit()
10
+ })
11
+ </script>
data/config/routes.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  RailsPgExtrasWeb::Engine.routes.draw do
2
- resources :queries, only: :index
2
+ resources :queries, only: [:index]
3
3
 
4
4
  root to: "queries#index"
5
5
  end
@@ -1,3 +1,3 @@
1
1
  module RailsPgExtrasWeb
2
- VERSION = '1.5.3'
2
+ VERSION = '2.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-pg-extras-web
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.3
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomasz Mazur
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-01 00:00:00.000000000 Z
11
+ date: 2021-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg
@@ -30,15 +30,15 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 1.5.0
33
+ version: '2.2'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 1.5.0
41
- description:
40
+ version: '2.2'
41
+ description:
42
42
  email:
43
43
  - tomasz.mazur@hey.com
44
44
  executables: []
@@ -51,7 +51,11 @@ files:
51
51
  - app/controllers/rails_pg_extras_web/application_controller.rb
52
52
  - app/controllers/rails_pg_extras_web/queries_controller.rb
53
53
  - app/views/layouts/rails_pg_extras_web/application.html.erb
54
+ - app/views/rails_pg_extras_web/queries/_result.html.erb
55
+ - app/views/rails_pg_extras_web/queries/_unavailable_extensions_warning.html.erb
54
56
  - app/views/rails_pg_extras_web/queries/index.html.erb
57
+ - app/views/rails_pg_extras_web/queries/show.html.erb
58
+ - app/views/rails_pg_extras_web/shared/_queries_selector.html.erb
55
59
  - config/routes.rb
56
60
  - lib/rails-pg-extras-web.rb
57
61
  - lib/rails_pg_extras_web/engine.rb
@@ -60,7 +64,7 @@ homepage: https://github.com/defkode/rails-pg-extras-web
60
64
  licenses:
61
65
  - MIT
62
66
  metadata: {}
63
- post_install_message:
67
+ post_install_message:
64
68
  rdoc_options: []
65
69
  require_paths:
66
70
  - lib
@@ -75,8 +79,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
79
  - !ruby/object:Gem::Version
76
80
  version: '0'
77
81
  requirements: []
78
- rubygems_version: 3.1.4
79
- signing_key:
82
+ rubygems_version: 3.1.6
83
+ signing_key:
80
84
  specification_version: 4
81
85
  summary: Web UI for rails-pg-extras
82
86
  test_files: []