Skip to content

Commit

Permalink
Use rufo formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
pawurb committed Feb 22, 2024
1 parent 035ce19 commit b05d186
Show file tree
Hide file tree
Showing 18 changed files with 103 additions and 110 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
source 'https://rubygems.org'
source "https://rubygems.org"

gemspec
5 changes: 2 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
require "bundler/gem_tasks"
require 'rspec/core/rake_task'
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

desc 'Test all PG versions'
desc "Test all PG versions"
task :test_all do
system("PG_VERSION=11 bundle exec rspec spec/ && PG_VERSION=12 bundle exec rspec spec/ && PG_VERSION=13 bundle exec rspec spec/ && PG_VERSION=14 bundle exec rspec spec/")
end

8 changes: 4 additions & 4 deletions app/controllers/rails_pg_extras/web/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ class ApplicationController < ActionController::Base
REQUIRED_EXTENSIONS = {
pg_stat_statements: %i[calls outliers pg_stat_statements_reset],
pg_buffercache: %i[buffercache_stats buffercache_usage],
sslinfo: %i[ssl_used]
sslinfo: %i[ssl_used],
}

ACTIONS = %i[kill_all pg_stat_statements_reset add_extensions]

if ENV['RAILS_PG_EXTRAS_USER'].present? && ENV['RAILS_PG_EXTRAS_PASSWORD'].present?
http_basic_authenticate_with name: ENV.fetch('RAILS_PG_EXTRAS_USER'), password: ENV.fetch('RAILS_PG_EXTRAS_PASSWORD')
if ENV["RAILS_PG_EXTRAS_USER"].present? && ENV["RAILS_PG_EXTRAS_PASSWORD"].present?
http_basic_authenticate_with name: ENV.fetch("RAILS_PG_EXTRAS_USER"), password: ENV.fetch("RAILS_PG_EXTRAS_PASSWORD")
end

def validate_credentials!
if (ENV['RAILS_PG_EXTRAS_USER'].blank? || ENV['RAILS_PG_EXTRAS_PASSWORD'].blank?) && !RailsPgExtras.configuration.public_dashboard
if (ENV["RAILS_PG_EXTRAS_USER"].blank? || ENV["RAILS_PG_EXTRAS_PASSWORD"].blank?) && !RailsPgExtras.configuration.public_dashboard
raise "Missing credentials for rails-pg-extras dashboard! If you want to enable public dashboard please set RAILS_PG_EXTRAS_PUBLIC_DASHBOARD=true"
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/rails_pg_extras/web/queries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def unavailable_extensions
return @unavailable_extensions if defined?(@unavailable_extensions)

enabled_extensions = ActiveRecord::Base.connection.extensions
@unavailable_extensions = REQUIRED_EXTENSIONS.delete_if { |ext| ext.to_s.in?(enabled_extensions) }
@unavailable_extensions = REQUIRED_EXTENSIONS.delete_if { |ext| ext.to_s.in?(enabled_extensions) }
end
end
end
2 changes: 1 addition & 1 deletion app/views/rails_pg_extras/web/queries/_diagnose.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h1 class="font-bold text-xl my-5">Diagnose</h1>
<table class="w-full font-mono border-collapse border my-5">
<% RailsPgExtras.diagnose(in_format: :hash).each do |diagnosis| %>
<tr class="<%= diagnosis[:ok] ? 'bg-green-300' : 'bg-red-300' %>">
<tr class="<%= diagnosis[:ok] ? "bg-green-300" : "bg-red-300" %>">
<td class='p-1 border font-bold'><%= diagnosis[:check_name] %></td>
<td class='p-1 border'><%= diagnosis[:message] %></td>
</tr>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<div class="text-red-500 p-3 font-mono my-5">
<% unavailable_extensions.each do |extension, queries| %>
<% unavailable_extensions.each do |extension, queries| %>
WARNING: Queries <%= queries.map { |q| "<b><u>#{q}</u></b>" }.join(", ").html_safe %> require extension: <b><%= extension %></b>
<br>
<% end %>
<% end %>
</div>

<% if RailsPgExtras::Web.action_enabled?(:add_extensions) %>
<%= link_to "Enable extensions", add_extensions_action_path,
method: "post",
data: {
confirm: "This command will enable following extensions: #{unavailable_extensions.keys.join(', ')}. Do you want to proceeed?"
}, class: 'border p-3 bg-green-500 text-white hover:bg-green-600 font-bold rounded' %>
<% end %>
<% if RailsPgExtras::Web.action_enabled?(:add_extensions) %>
<%= link_to "Enable extensions", add_extensions_action_path,
method: "post",
data: {
confirm: "This command will enable following extensions: #{unavailable_extensions.keys.join(", ")}. Do you want to proceeed?",
}, class: "border p-3 bg-green-500 text-white hover:bg-green-600 font-bold rounded" %>
<% end %>

22 changes: 10 additions & 12 deletions app/views/rails_pg_extras/web/queries/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@
<h1 class="font-bold text-xl my-5">Actions</h1>

<% if RailsPgExtras::Web.action_enabled?(:kill_all) %>
<%= link_to "kill_all", kill_all_action_path,
method: "post",
data: {
confirm: "This commands kills all the currently active connections to the database. Do you want to proceed?"
},
class: 'border p-3 bg-red-500 text-white hover:bg-red-600 font-bold rounded'
%>
<%= link_to "kill_all", kill_all_action_path,
method: "post",
data: {
confirm: "This commands kills all the currently active connections to the database. Do you want to proceed?",
},
class: "border p-3 bg-red-500 text-white hover:bg-red-600 font-bold rounded" %>
<% end %>
<% if RailsPgExtras::Web.action_enabled?(:pg_stat_statements_reset) && unavailable_extensions.exclude?(:pg_stat_statements) %>
<%= link_to "pg_stat_statements_reset", pg_stat_statements_reset_action_path,
method: "post",
data: {
confirm: "This command discards all statistics gathered so far by pg_stat_statements. Do you want to proceed?"
}, class: 'border p-3 bg-blue-500 text-white hover:bg-blue-600 font-bold rounded'
%>
method: "post",
data: {
confirm: "This command discards all statistics gathered so far by pg_stat_statements. Do you want to proceed?",
}, class: "border p-3 bg-blue-500 text-white hover:bg-blue-600 font-bold rounded" %>
<% end %>
25 changes: 12 additions & 13 deletions app/views/rails_pg_extras/web/queries/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
<%= content_for :title, params[:query_name].presence || "pg_extras" %>
<%= render "rails_pg_extras/web/shared/queries_selector" %>
<%= content_for :title, params[:query_name].presence || "pg_extras" %>
<%= render "rails_pg_extras/web/shared/queries_selector" %>
<% if @error %>
<% if @error %>
<div class="text-red-500 p-3 font-mono my-5"><%= @error %></div>
<% else %>
<% if @result&.any? %>
<%= render "result",
title: RubyPgExtras.description_for(query_name: @query_name),
headers: @result[0].keys,
rows: @result.values
%>
<% else %>
<% else %>
<% if @result&.any? %>
<%= render "result",
title: RubyPgExtras.description_for(query_name: @query_name),
headers: @result[0].keys,
rows: @result.values %>
<% else %>
<div class="font-mono p-3 bg-gray-100 mt-3">No results</div>
<% end %>
<% end %>
<% end %>
<% end %>

<style>
@media print {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<%= form_tag queries_path, id: "queries", method: :get do |f| %>
<%= select_tag :query_name, options_for_select(@all_queries, params[:query_name]),
{prompt: "--- select query ---", class: "border p-2 font-bold", autofocus: true}
%>
<% end %>
<%= form_tag queries_path, id: "queries", method: :get do |f| %>
<%= select_tag :query_name, options_for_select(@all_queries, params[:query_name]),
{ prompt: "--- select query ---", class: "border p-2 font-bold", autofocus: true } %>
<% end %>
<%= javascript_tag nonce: true do -%>
document.getElementById('queries').addEventListener('change', (e) => {
Expand Down
49 changes: 24 additions & 25 deletions lib/rails-pg-extras.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# frozen_string_literal: true

require 'terminal-table'
require 'ruby-pg-extras'
require 'rails_pg_extras/diagnose_data'
require 'rails_pg_extras/diagnose_print'
require 'rails_pg_extras/index_info'
require 'rails_pg_extras/index_info_print'
require 'rails_pg_extras/table_info'
require 'rails_pg_extras/table_info_print'
require "terminal-table"
require "ruby-pg-extras"
require "rails_pg_extras/diagnose_data"
require "rails_pg_extras/diagnose_print"
require "rails_pg_extras/index_info"
require "rails_pg_extras/index_info_print"
require "rails_pg_extras/table_info"
require "rails_pg_extras/table_info_print"

module RailsPgExtras
QUERIES = RubyPgExtras::QUERIES
Expand All @@ -19,7 +19,7 @@ module RailsPgExtras
run_query(
query_name: query_name,
in_format: options.fetch(:in_format, :display_table),
args: options.fetch(:args, {})
args: options.fetch(:args, {}),
)
end
end
Expand All @@ -30,25 +30,25 @@ def self.run_query(query_name:, in_format:, args: {})
FROM pg_available_extensions
WHERE name = 'pg_stat_statements'"
if (version = RailsPgExtras.connection.execute(pg_stat_statements_version_sql)
.to_a[0].fetch("installed_version", nil))
.to_a[0].fetch("installed_version", nil))
if Gem::Version.new(version) < Gem::Version.new(NEW_PG_STAT_STATEMENTS)
query_name = "#{query_name}_legacy".to_sym
end
end
end

sql = if (custom_args = DEFAULT_ARGS[query_name].merge(args)) != {}
RubyPgExtras.sql_for(query_name: query_name) % custom_args
else
RubyPgExtras.sql_for(query_name: query_name)
end
RubyPgExtras.sql_for(query_name: query_name) % custom_args
else
RubyPgExtras.sql_for(query_name: query_name)
end

result = connection.execute(sql)

RubyPgExtras.display_result(
result,
title: RubyPgExtras.description_for(query_name: query_name),
in_format: in_format
in_format: in_format,
)
end

Expand Down Expand Up @@ -76,10 +76,10 @@ def self.measure_queries(&block)
sql_duration = 0

method_name = if ActiveSupport::Notifications.respond_to?(:monotonic_subscribe)
:monotonic_subscribe
else
:subscribe
end
:monotonic_subscribe
else
:subscribe
end

subscriber = ActiveSupport::Notifications.public_send(method_name, "sql.active_record") do |_name, start, finish, _id, payload|
unless payload[:name] =~ /SCHEMA/
Expand Down Expand Up @@ -115,11 +115,10 @@ def self.measure_queries(&block)
count: queries.reduce(0) { |agg, val| agg + val[1].fetch(:count) },
queries: queries,
total_duration: total_duration.round(2),
sql_duration: sql_duration.round(2)
sql_duration: sql_duration.round(2),
}
end


def self.index_info(args: {}, in_format: :display_table)
data = RailsPgExtras::IndexInfo.call(args[:table_name])

Expand Down Expand Up @@ -149,14 +148,14 @@ def self.table_info(args: {}, in_format: :display_table)
end

def self.connection
if (db_url = ENV['RAILS_PG_EXTRAS_DATABASE_URL'])
if (db_url = ENV["RAILS_PG_EXTRAS_DATABASE_URL"])
ActiveRecord::Base.establish_connection(db_url).connection
else
ActiveRecord::Base.connection
end
end
end

require 'rails_pg_extras/web'
require 'rails_pg_extras/configuration'
require 'rails_pg_extras/railtie' if defined?(Rails)
require "rails_pg_extras/web"
require "rails_pg_extras/configuration"
require "rails_pg_extras/railtie" if defined?(Rails)
1 change: 0 additions & 1 deletion lib/rails_pg_extras/diagnose_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

module RailsPgExtras
class DiagnoseData < ::RubyPgExtras::DiagnoseData

private

def query_module
Expand Down
1 change: 0 additions & 1 deletion lib/rails_pg_extras/diagnose_print.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

module RailsPgExtras
class DiagnosePrint < ::RubyPgExtras::DiagnosePrint

private

def title
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_pg_extras/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

class RailsPgExtras::Railtie < Rails::Railtie
rake_tasks do
load 'rails_pg_extras/tasks/all.rake'
load "rails_pg_extras/tasks/all.rake"
end
end
2 changes: 1 addition & 1 deletion lib/rails_pg_extras/tasks/all.rake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'rails-pg-extras'
require "rails-pg-extras"

namespace :pg_extras do
RailsPgExtras::QUERIES.each do |query_name|
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_pg_extras/web/engine.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'rails'
require "rails"

module RailsPgExtras::Web
class Engine < ::Rails::Engine
Expand Down
25 changes: 13 additions & 12 deletions rails-pg-extras.gemspec
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
# -*- encoding: utf-8 -*-
lib = File.expand_path('../lib', __FILE__)
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'rails_pg_extras/version'
require "rails_pg_extras/version"

Gem::Specification.new do |s|
s.name = "rails-pg-extras"
s.version = RailsPgExtras::VERSION
s.authors = ["pawurb"]
s.email = ["contact@pawelurbanek.com"]
s.summary = %q{ Rails PostgreSQL performance database insights }
s.description = %q{ Rails port of 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. }
s.homepage = "http://github.com/pawurb/rails-pg-extras"
s.files = `git ls-files`.split("\n")
s.test_files = s.files.grep(%r{^(spec)/})
s.name = "rails-pg-extras"
s.version = RailsPgExtras::VERSION
s.authors = ["pawurb"]
s.email = ["contact@pawelurbanek.com"]
s.summary = %q{ Rails PostgreSQL performance database insights }
s.description = %q{ Rails port of 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. }
s.homepage = "http://github.com/pawurb/rails-pg-extras"
s.files = `git ls-files`.split("\n")
s.test_files = s.files.grep(%r{^(spec)/})
s.require_paths = ["lib"]
s.license = "MIT"
s.license = "MIT"
s.add_dependency "ruby-pg-extras", RailsPgExtras::VERSION
s.add_dependency "rails"
s.add_development_dependency "rake"
s.add_development_dependency "rspec"
s.add_development_dependency "rufo"

if s.respond_to?(:metadata=)
s.metadata = { "rubygems_mfa_required" => "true" }
Expand Down
10 changes: 5 additions & 5 deletions spec/smoke_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# frozen_string_literal: true

require 'spec_helper'
require 'rails-pg-extras'
require "spec_helper"
require "rails-pg-extras"

describe RailsPgExtras do
RailsPgExtras::QUERIES.each do |query_name|
it "#{query_name} query can be executed" do
expect do
RailsPgExtras.run_query(
query_name: query_name,
in_format: :hash
in_format: :hash,
)
end.not_to raise_error
end
Expand All @@ -35,12 +35,12 @@
end

it "supports custom RAILS_PG_EXTRAS_DATABASE_URL" do
ENV['RAILS_PG_EXTRAS_DATABASE_URL'] = ENV['DATABASE_URL']
ENV["RAILS_PG_EXTRAS_DATABASE_URL"] = ENV["DATABASE_URL"]

expect do
RailsPgExtras.calls
end.not_to raise_error

ENV['RAILS_PG_EXTRAS_DATABASE_URL'] = nil
ENV["RAILS_PG_EXTRAS_DATABASE_URL"] = nil
end
end
Loading

0 comments on commit b05d186

Please sign in to comment.