rails-pg-extras-web 2.0.0 → 2.0.1

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: 5530b516b0ab842d471eaed6b902a6448ebf482b5851c7f9b3e8ebb3ca9e7b9e
4
- data.tar.gz: 7e0c1e7937d70ecd82fdecc39c601d4f6c3db59e7ca2303524539ac65b4bb302
3
+ metadata.gz: 5699fd3b704d3c1b44dba980a4c7f744bceb7a8ac56e59c7cc013b8b68544871
4
+ data.tar.gz: 9d881723852e10a275e0e146a0a2586275b7830e0ef21a9f10e263d9523c5d25
5
5
  SHA512:
6
- metadata.gz: 9f6b6b7e5027069883fc23e385ba8b5bdc74378eba10e702c95b3b975f09e4acea9639abee99e48c3dcc737f636aee8b014ad4070ceb3da98bab1a6f37c9bfc1
7
- data.tar.gz: 9387a5b9637197e5142c0c7c4bc8118d137ca05dd63a2cf9c580e2f93a9b6009725ac2f57ac0a4c6a63cd8105a767c38f64308e85be211b392d299379fcb8157
6
+ metadata.gz: 62c46bf2750fe4582b78454b610cf187735bccc48b39a33150d8445bb90c52e785e83d52f8d6ecb1fb01309eeb83c58eb2b1e2f1f2185e9364ed4dc132c05033
7
+ data.tar.gz: 49e2b3c3c17b16f4cad995d0ff79162699e51294aa0c93e6e2e71c31acb34a731a750b8fe05c4e3780f1d0cf39b18bd95a442c505bb9b2a16c2740d7e6ea35cc
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:
@@ -9,43 +9,41 @@ module RailsPgExtrasWeb
9
9
  helper_method :unavailable_extensions
10
10
 
11
11
  def index
12
- @query_name = params[:query_name]&.to_sym.presence_in(@all_queries.keys)
13
- return unless @query_name
12
+ if params[:query_name].present?
13
+ @query_name = params[:query_name].to_sym.presence_in(@all_queries.keys)
14
+ return unless @query_name
14
15
 
15
- @result = RailsPGExtras.run_query(query_name: @query_name.to_sym, in_format: :raw)
16
- rescue ActiveRecord::StatementInvalid => e
17
- @error = e.message
16
+ begin
17
+ @result = RailsPGExtras.run_query(query_name: @query_name.to_sym, in_format: :raw)
18
+ rescue ActiveRecord::StatementInvalid => e
19
+ @error = e.message
20
+ end
21
+
22
+ render :show
23
+ end
18
24
  end
19
25
 
20
26
  private
21
27
 
22
28
  def load_queries
23
- @all_queries = {}
29
+ @all_queries = RailsPGExtras::QUERIES.inject({}) do |memo, query_name|
30
+ unless query_name.in? %i[kill_all mandelbrot]
31
+ memo[query_name] = { disabled: query_disabled?(query_name) }
32
+ end
24
33
 
25
- ::RailsPGExtras::QUERIES.each do |query_name|
26
- @all_queries[query_name] = {
27
- disabled: query_disabled?(query_name),
28
- command: query_name == :kill_all
29
- }
34
+ memo
30
35
  end
31
36
  end
32
37
 
33
38
  def query_disabled?(query_name)
34
- case query_name
35
- when :calls, :outliers
36
- unavailable_extensions.key?(:pg_stat_statements)
37
- when :buffercache_stats, :buffercache_usage
38
- unavailable_extensions.key?(:pg_buffercache)
39
- else
40
- false
41
- end
39
+ unavailable_extensions.values.flatten.include?(query_name)
42
40
  end
43
41
 
44
42
  def unavailable_extensions
45
43
  return @unavailable_extensions if defined?(@unavailable_extensions)
46
44
 
47
- extensions = ActiveRecord::Base.connection.extensions
48
- @unavailable_extensions = REQUIRED_EXTENSIONS.select { |extension, _| !extensions.include?(extension.to_s) }
45
+ enabled_extensions = ActiveRecord::Base.connection.extensions
46
+ @unavailable_extensions = REQUIRED_EXTENSIONS.delete_if { |ext| ext.to_s.in?(enabled_extensions) }
49
47
  end
50
48
  end
51
49
  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,72 +1,4 @@
1
- <% if unavailable_extensions.any? && !@result %>
2
- <% unavailable_extensions.each do |extension, queries| %>
3
- <span style='color: orange'>
4
- WARNING: Queries <%= queries.map { |q| "<b><u>#{q}</u></b>" }.join(", ").html_safe %> require extension: <b><%= extension %></b>
5
- </span>
1
+ <%= content_for :title, "pg_extras" %>
2
+ <%= render "rails_pg_extras_web/shared/queries_selector" %>
6
3
 
7
- <h4>Rails</h4>
8
- <pre style='background-color: #e2e2e2; padding: 10px'>ActiveRecord::Base.connection.enable_extension "<%= extension %>"</pre>
9
- <h4>SQL</h4>
10
- <pre style='background-color: #e2e2e2; padding: 10px'>CREATE EXTENSION <%= extension %></pre>
11
- <% end %>
12
- <% end %>
13
-
14
- <% if @error %>
15
- <span style='color: red'><%= @error %></span>
16
- <% else %>
17
- <% if @result && @result.any? %>
18
- <table>
19
- <caption><%= RubyPGExtras.description_for(query_name: @query_name) %></caption>
20
- <thead>
21
- <tr>
22
- <% @result[0].keys.each do |header| %>
23
- <th><%= header %></th>
24
- <% end %>
25
- </tr>
26
- </thead>
27
- <tbody>
28
- <% @result.values.each do |row| %>
29
- <tr>
30
- <% row.each do |column| %>
31
- <td><%= column %></td>
32
- <% end %>
33
- </tr>
34
- <% end %>
35
- </tbody>
36
- </table>
37
-
38
- <pre>run_at: <%= Time.now.utc %></pre>
39
- <% else %>
40
- <pre>No results</pre>
41
- <% end %>
42
- <% end %>
43
-
44
- <style media="screen">
45
- table {
46
- font-family: monospace;
47
- border-collapse: collapse;
48
- width: 100%;
49
- }
50
-
51
- table th {
52
- background-color: #c9c9;
53
- }
54
-
55
- table th, table td {
56
- text-align: left;
57
- border: 1px solid #c9c9;
58
- padding: 3px;
59
- }
60
-
61
- table td {
62
- overflow: hidden;
63
- text-overflow: ellipsis;
64
- white-space: nowrap;
65
- }
66
-
67
- table caption {
68
- padding: 5px;
69
- font-size: 15px;
70
- font-weight: bold;
71
- }
72
- </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 = '2.0.0'
2
+ VERSION = '2.0.1'
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: 2.0.0
4
+ version: 2.0.1
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-07-16 00:00:00.000000000 Z
11
+ date: 2021-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg
@@ -38,7 +38,7 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '2.0'
41
- description:
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
@@ -76,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
80
  version: '0'
77
81
  requirements: []
78
82
  rubygems_version: 3.1.6
79
- signing_key:
83
+ signing_key:
80
84
  specification_version: 4
81
85
  summary: Web UI for rails-pg-extras
82
86
  test_files: []