Skip to content

Commit

Permalink
Use standard formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
pawurb committed Dec 8, 2024
1 parent 2249498 commit ec0b662
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 51 deletions.
10 changes: 4 additions & 6 deletions app/controllers/rails_pg_extras/web/actions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ def validate_action!
end

def run(action)
begin
RailsPgExtras.run_query(query_name: action, in_format: :raw)
redirect_to root_path, notice: "Successfully ran #{action}"
rescue ActiveRecord::StatementInvalid => e
redirect_to root_path, alert: "Error: #{e.message}"
end
RailsPgExtras.run_query(query_name: action, in_format: :raw)
redirect_to root_path, notice: "Successfully ran #{action}"
rescue ActiveRecord::StatementInvalid => e
redirect_to root_path, alert: "Error: #{e.message}"
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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]
Expand Down
6 changes: 2 additions & 4 deletions app/controllers/rails_pg_extras/web/queries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ def index
private

def load_queries
@all_queries = (RailsPgExtras::QUERIES - RailsPgExtras::Web::ACTIONS).inject({}) do |memo, query_name|
@all_queries = (RailsPgExtras::QUERIES - RailsPgExtras::Web::ACTIONS).each_with_object({}) do |query_name, memo|
unless query_name.in? %i[mandelbrot]
memo[query_name] = { disabled: query_disabled?(query_name) }
memo[query_name] = {disabled: query_disabled?(query_name)}
end

memo
end
end

Expand Down
36 changes: 18 additions & 18 deletions lib/rails-pg-extras.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,38 @@ 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

def self.run_query(query_name:, in_format:, args: {})
if %i(calls outliers).include?(query_name)
if %i[calls outliers].include?(query_name)
pg_stat_statements_version_sql = "SELECT installed_version
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))
if Gem::Version.new(version) < Gem::Version.new(NEW_PG_STAT_STATEMENTS)
query_name = "#{query_name}_legacy".to_sym
query_name = :"#{query_name}_legacy"
elsif Gem::Version.new(version) >= Gem::Version.new(PG_STAT_STATEMENTS_17)
query_name = "#{query_name}_17".to_sym
query_name = :"#{query_name}_17"
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 @@ -79,25 +79,25 @@ 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/
unless /SCHEMA/.match?(payload[:name])
key = payload[:sql]
queries[key] ||= { count: 0, total_duration: 0, min_duration: nil, max_duration: nil }
queries[key] ||= {count: 0, total_duration: 0, min_duration: nil, max_duration: nil}
queries[key][:count] += 1
duration = (finish - start) * 1000
queries[key][:total_duration] += duration
sql_duration += duration

if queries[key][:min_duration] == nil || queries[key][:min_duration] > duration
if queries[key][:min_duration].nil? || queries[key][:min_duration] > duration
queries[key][:min_duration] = duration.round(2)
end

if queries[key][:max_duration] == nil || queries[key][:max_duration] < duration
if queries[key][:max_duration].nil? || queries[key][:max_duration] < duration
queries[key][:max_duration] = duration.round(2)
end
end
Expand All @@ -118,7 +118,7 @@ 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

Expand Down
2 changes: 1 addition & 1 deletion lib/rails_pg_extras/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module RailsPgExtras
class Configuration
DEFAULT_CONFIG = { enabled_web_actions: Web::ACTIONS - [:kill_all], public_dashboard: ENV["RAILS_PG_EXTRAS_PUBLIC_DASHBOARD"] == "true" }
DEFAULT_CONFIG = {enabled_web_actions: Web::ACTIONS - [:kill_all], public_dashboard: ENV["RAILS_PG_EXTRAS_PUBLIC_DASHBOARD"] == "true"}

attr_reader :enabled_web_actions
attr_accessor :public_dashboard
Expand Down
10 changes: 4 additions & 6 deletions rails-pg-extras.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- encoding: utf-8 -*-
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "rails_pg_extras/version"
Expand All @@ -8,20 +7,19 @@ Gem::Specification.new do |s|
s.version = RailsPgExtras::VERSION
s.authors = ["pawurb"]
s.email = ["[email protected]"]
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.summary = "Rails PostgreSQL performance database insights"
s.description = "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.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"
s.add_development_dependency "standard"

if s.respond_to?(:metadata=)
s.metadata = { "rubygems_mfa_required" => "true" }
s.metadata = {"rubygems_mfa_required" => "true"}
end
end
2 changes: 1 addition & 1 deletion spec/smoke_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
expect do
RailsPgExtras.run_query(
query_name: query_name,
in_format: :hash,
in_format: :hash
)
end.not_to raise_error
end
Expand Down
28 changes: 14 additions & 14 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
pg_version = ENV["PG_VERSION"]

port = if pg_version == "12"
"5432"
elsif pg_version == "13"
"5433"
elsif pg_version == "14"
"5434"
elsif pg_version == "15"
"5435"
elsif pg_version == "16"
"5436"
elsif pg_version == "17"
"5437"
else
"5432"
end
"5432"
elsif pg_version == "13"
"5433"
elsif pg_version == "14"
"5434"
elsif pg_version == "15"
"5435"
elsif pg_version == "16"
"5436"
elsif pg_version == "17"
"5437"
else
"5432"
end

ENV["DATABASE_URL"] ||= "postgresql://postgres:secret@localhost:#{port}/rails-pg-extras-test"

Expand Down

0 comments on commit ec0b662

Please sign in to comment.