rails-pg-extras-web 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c38b308833f95b44ec69458f762fdff8ddd7ae5414b628ca0619c4d6b8c17871
4
+ data.tar.gz: '0607678d4a6fae56d1af5024cf7bc40d4b40cad3258e97a2b87b2c7f546c79bc'
5
+ SHA512:
6
+ metadata.gz: caa689489549961d3563b840444d617caed45821a9ea952215b97ddec72b00150cf15c7b6ebc3fcda038a629aa4c593d23917ed1765d09fd38b372c1635dc28e
7
+ data.tar.gz: c4813eeed8770cf98d518ed5690b564f1b24615f3c992286cc2fc0cde452a35bf42ba1cf950a2d7b8d8ebe796ea25551ceaac3616c79e3a3091fb94dc37c23c5
@@ -0,0 +1,20 @@
1
+ Copyright 2020 defkode
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,33 @@
1
+ # RailsPgExtrasWeb
2
+ Web UI for rails-pg-extras-web
3
+
4
+ ## Installation
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'rails-pg-extras-web', github: 'defkode/rails-pg-extras-web'
9
+ ```
10
+
11
+ And then execute:
12
+ ```bash
13
+ $ bundle
14
+ ```
15
+
16
+ Update config/routes.rb
17
+
18
+ ```ruby
19
+ mount RailsPgExtrasWeb::Engine, at: 'pg_extras'
20
+ ```
21
+
22
+ Authorization in production enviroment via HTTP Basic. Please set PG_EXTRAS_USER, PG_EXTRAS_PASSWORD environment variables.
23
+
24
+ Heroku example
25
+
26
+ ```bash
27
+ heroku config:set PG_EXTRAS_USER=admin PG_EXTRAS_PASSWORD=secret
28
+ ```
29
+
30
+
31
+
32
+ ## License
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'RailsPgExtrasWeb'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+ task default: :test
@@ -0,0 +1,36 @@
1
+ require "rails-pg-extras"
2
+
3
+ module RailsPgExtrasWeb
4
+ class ApplicationController < ActionController::Base
5
+ layout "rails_pg_extras_web/application"
6
+
7
+ if Rails.env.production? && ENV['PG_EXTRAS_USER'].present? && ENV['PG_EXTRAS_PASSWORD'].present?
8
+ http_basic_authenticate_with name: ENV['PG_EXTRAS_USER'], password: ENV['PG_EXTRAS_PASS']
9
+ end
10
+
11
+ protect_from_forgery with: :exception
12
+
13
+ helper_method :pg_stats_statements_enabled?
14
+
15
+ private
16
+
17
+ def load_queries
18
+ @all_queries = {}
19
+
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
+ }
25
+ end
26
+ end
27
+
28
+ def query_disabled?(query_name)
29
+ query_name.in?([:calls, :outliers]) && !pg_stats_statements_enabled?
30
+ end
31
+
32
+ def pg_stats_statements_enabled?
33
+ ActiveRecord::Base.connection.extensions.include?("pg_stat_statements")
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,19 @@
1
+ module RailsPgExtrasWeb
2
+ class QueriesController < ApplicationController
3
+ before_action :load_queries
4
+
5
+ def index; end
6
+
7
+ def run
8
+ if @query_name = params[:query_name].presence
9
+ begin
10
+ @result = ActiveRecord::Base.connection.execute RubyPGExtras.sql_for(query_name: @query_name)
11
+ rescue ActiveRecord::StatementInvalid => e
12
+ @error = e.message
13
+ end
14
+ else
15
+ redirect_to root_path
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>PG::Extras</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+ </head>
8
+ <body onload="document.forms.queries.query_name.focus()">
9
+ <%= form_with url: run_query_url, id: "queries" do |f| %>
10
+ <%= f.select :query_name, options_for_select(@all_queries.except(:kill_all, :mandelbrot), params[:query_name]),
11
+ prompt: "--- select query ---"
12
+ %>
13
+
14
+ <button type="submit">Run</button>
15
+ <% end %>
16
+
17
+ <div style='margin-top: 20px'>
18
+ <%= yield %>
19
+ </div>
20
+ </body>
21
+ </html>
@@ -0,0 +1,10 @@
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>
5
+
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 %>
@@ -0,0 +1,59 @@
1
+ <% if @error %>
2
+ <span style='color: red'><%= @error %></span>
3
+ <% else %>
4
+ <% if @result && @result.any? %>
5
+ <table>
6
+ <caption><%= RubyPGExtras.description_for(query_name: @query_name) %></caption>
7
+ <thead>
8
+ <tr>
9
+ <% @result[0].keys.each do |header| %>
10
+ <th><%= header %></th>
11
+ <% end %>
12
+ </tr>
13
+ </thead>
14
+ <tbody>
15
+ <% @result.values.each do |row| %>
16
+ <tr>
17
+ <% row.each do |column| %>
18
+ <td><%= column %></td>
19
+ <% end %>
20
+ </tr>
21
+ <% end %>
22
+ </tbody>
23
+ </table>
24
+
25
+ <pre>run_at: <%= Time.now.utc %></pre>
26
+ <% else %>
27
+ <pre>No results</pre>
28
+ <% end %>
29
+ <% end %>
30
+
31
+ <style media="screen">
32
+ table {
33
+ font-family: monospace;
34
+ border-collapse: collapse;
35
+ width: 100%;
36
+ }
37
+
38
+ table th {
39
+ background-color: #c9c9;
40
+ }
41
+
42
+ table th, table td {
43
+ text-align: left;
44
+ border: 1px solid #c9c9;
45
+ padding: 3px;
46
+ }
47
+
48
+ table td {
49
+ overflow: hidden;
50
+ text-overflow: ellipsis;
51
+ white-space: nowrap;
52
+ }
53
+
54
+ table caption {
55
+ padding: 5px;
56
+ font-size: 15px;
57
+ font-weight: bold;
58
+ }
59
+ </style>
@@ -0,0 +1,5 @@
1
+ RailsPgExtrasWeb::Engine.routes.draw do
2
+ root to: "queries#index"
3
+
4
+ post "/queries/run", as: :run_query
5
+ end
@@ -0,0 +1,5 @@
1
+ require "rails_pg_extras_web/engine"
2
+
3
+ module RailsPgExtrasWeb
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,5 @@
1
+ module RailsPgExtrasWeb
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace RailsPgExtrasWeb
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module RailsPgExtrasWeb
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-pg-extras-web
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tomasz Mazur
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-01-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pg
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails-pg-extras
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
41
+ description:
42
+ email:
43
+ - tomasz.mazur@hey.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - app/controllers/rails_pg_extras_web/application_controller.rb
52
+ - app/controllers/rails_pg_extras_web/queries_controller.rb
53
+ - app/views/layouts/rails_pg_extras_web/application.html.erb
54
+ - app/views/rails_pg_extras_web/queries/index.html.erb
55
+ - app/views/rails_pg_extras_web/queries/run.html.erb
56
+ - config/routes.rb
57
+ - lib/rails-pg-extras-web.rb
58
+ - lib/rails_pg_extras_web/engine.rb
59
+ - lib/rails_pg_extras_web/version.rb
60
+ homepage: https://github.com/defkode/rails-pg-extras-web
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubygems_version: 3.0.3
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Web UI for rails-pg-extras
83
+ test_files: []