Skip to content

Commit

Permalink
Added current stats to query details
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Feb 5, 2024
1 parent bacbd78 commit 6c725da
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/controllers/pg_hero/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def show_query
@explainable_query = stats[:explainable_query]

if @show_details
query_hash_stats = @database.query_hash_stats(@query_hash, user: @user)
query_hash_stats = @database.query_hash_stats(@query_hash, user: @user, current: true)

@chart_data = [{name: "Value", data: query_hash_stats.map { |r| [r[:captured_at].change(sec: 0), (r[:total_minutes] * 60 * 1000).round] }, library: chart_library_options}]
@chart2_data = [{name: "Value", data: query_hash_stats.map { |r| [r[:captured_at].change(sec: 0), r[:average_time].round(1)] }, library: chart_library_options}]
Expand Down
18 changes: 14 additions & 4 deletions lib/pghero/methods/query_stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,10 @@ def slow_queries(query_stats: nil, **options)
query_stats.select { |q| q[:calls].to_i >= slow_query_calls.to_i && q[:average_time].to_f >= slow_query_ms.to_f }
end

# TODO option to include current period
def query_hash_stats(query_hash, user: nil)
def query_hash_stats(query_hash, user: nil, current: false)
if historical_query_stats_enabled? && supports_query_hash?
start_at = 24.hours.ago
select_all_stats <<~SQL
stats = select_all_stats <<~SQL
SELECT
captured_at,
total_time / 1000 / 60 AS total_minutes,
Expand All @@ -193,6 +192,15 @@ def query_hash_stats(query_hash, user: nil)
ORDER BY
1 ASC
SQL
if current
captured_at = Time.current
current_stats = current_query_stats(query_hash: query_hash, user: user, origin: true)
current_stats.each do |r|
r[:captured_at] = captured_at
end
stats += current_stats
end
stats
else
raise NotEnabled, "Query hash stats not enabled"
end
Expand All @@ -201,7 +209,7 @@ def query_hash_stats(query_hash, user: nil)
private

# http://www.craigkerstiens.com/2013/01/10/more-on-postgres-performance/
def current_query_stats(limit: nil, sort: nil, database: nil, query_hash: nil)
def current_query_stats(limit: nil, sort: nil, database: nil, query_hash: nil, user: nil, origin: false)
if query_stats_enabled?
limit ||= 100
sort ||= "total_minutes"
Expand All @@ -225,10 +233,12 @@ def current_query_stats(limit: nil, sort: nil, database: nil, query_hash: nil)
calls > 0 AND
pg_database.datname = #{database ? quote(database) : "current_database()"}
#{query_hash ? "AND queryid = #{quote(query_hash)}" : nil}
#{user ? "AND rolname = #{quote(user)}" : nil}
)
SELECT
query,
query AS explainable_query,
#{origin ? "(SELECT regexp_matches(query, '.*/\\*(.+?)\\*/'))[1] AS origin," : nil}
query_hash,
query_stats.user,
total_minutes,
Expand Down

0 comments on commit 6c725da

Please sign in to comment.