Skip to content

Commit

Permalink
Raise error when query stats not enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jul 30, 2017
1 parent 0d12283 commit 357f7d4
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: ruby
rvm: 2.2.4
cache: bundler
sudo: false
script: TRAVIS_CI=1 bundle exec rake test
script: bundle exec rake test
before_script:
- psql -c 'create database pghero_test;' -U postgres
notifications:
Expand Down
2 changes: 2 additions & 0 deletions lib/pghero.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
require "pghero/query_stats"

module PgHero
class MissingRequirement < StandardError; end

# settings
class << self
attr_accessor :long_running_query_sec, :slow_query_ms, :slow_query_calls, :total_connections_threshold, :cache_hit_rate_threshold, :env, :show_migrations
Expand Down
11 changes: 8 additions & 3 deletions lib/pghero/methods/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ def settings

def ssl_used?
ssl_used = nil
with_transaction do
execute("CREATE EXTENSION IF NOT EXISTS sslinfo")
with_transaction(rollback: true) do
# rescue if not superuser
execute("CREATE EXTENSION IF NOT EXISTS sslinfo") rescue nil
ssl_used = select_one("SELECT ssl_is_used()")
end
ssl_used
Expand All @@ -33,7 +34,11 @@ def database_name
end

def server_version
select_one("SHOW server_version")
@server_version ||= select_one("SHOW server_version")
end

def server_version_num
@server_version_num ||= select_one("SHOW server_version_num").to_i
end

private
Expand Down
20 changes: 7 additions & 13 deletions lib/pghero/methods/query_stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Methods
module QueryStats
def query_stats(historical: false, start_at: nil, end_at: nil, min_average_time: nil, min_calls: nil, **options)
current_query_stats = historical && end_at && end_at < Time.now ? [] : current_query_stats(options)
historical_query_stats = historical ? historical_query_stats(start_at: start_at, end_at: end_at, **options) : []
historical_query_stats = historical && historical_query_stats_enabled? ? historical_query_stats(start_at: start_at, end_at: end_at, **options) : []

query_stats = combine_query_stats((current_query_stats + historical_query_stats).group_by { |q| [q[:query_hash], q[:user]] })
query_stats = combine_query_stats(query_stats.group_by { |q| [normalize_query(q[:query]), q[:user]] })
Expand Down Expand Up @@ -55,12 +55,10 @@ def disable_query_stats
end

def reset_query_stats
if query_stats_enabled?
execute("SELECT pg_stat_statements_reset()")
true
else
false
end
execute("SELECT pg_stat_statements_reset()")
true
rescue ActiveRecord::StatementInvalid
false
end

# http://stackoverflow.com/questions/20582500/how-to-check-if-a-table-exists-in-a-given-schema
Expand Down Expand Up @@ -200,7 +198,7 @@ def current_query_stats(limit: nil, sort: nil, database: nil, query_hash: nil)
LIMIT #{limit.to_i}
SQL
else
[]
raise MissingRequirement, "Query stats not enabled"
end
end

Expand Down Expand Up @@ -244,14 +242,10 @@ def historical_query_stats(sort: nil, start_at: nil, end_at: nil, query_hash: ni
LIMIT 100
SQL
else
[]
raise MissingRequirement, "Historical query stats not enabled"
end
end

def server_version_num
@server_version ||= select_one("SHOW server_version_num").to_i
end

def combine_query_stats(grouped_stats)
query_stats = []
grouped_stats.each do |_, stats2|
Expand Down
3 changes: 0 additions & 3 deletions test/suggested_indexes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

class SuggestedIndexesTest < Minitest::Test
def setup
# no pg_stat_statements
skip if ENV["TRAVIS_CI"]

PgHero.reset_query_stats
end

Expand Down
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

ActiveRecord::Base.establish_connection adapter: "postgresql", database: "pghero_test"

ActiveRecord::Migration.enable_extension "pg_stat_statements"

ActiveRecord::Migration.create_table :cities, force: true do |t|
t.string :name
end
Expand Down

0 comments on commit 357f7d4

Please sign in to comment.