From 6c725da3bd1d3b4406feca354b651390c4f4dd47 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Sun, 4 Feb 2024 16:42:31 -0800 Subject: [PATCH] Added current stats to query details --- app/controllers/pg_hero/home_controller.rb | 2 +- lib/pghero/methods/query_stats.rb | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/app/controllers/pg_hero/home_controller.rb b/app/controllers/pg_hero/home_controller.rb index e8759353..e7c56860 100644 --- a/app/controllers/pg_hero/home_controller.rb +++ b/app/controllers/pg_hero/home_controller.rb @@ -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}] diff --git a/lib/pghero/methods/query_stats.rb b/lib/pghero/methods/query_stats.rb index c86cd639..d7952c5a 100644 --- a/lib/pghero/methods/query_stats.rb +++ b/lib/pghero/methods/query_stats.rb @@ -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, @@ -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 @@ -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" @@ -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,