From 58d1bb8e6f3bc505a0297326f248d39a3233eef5 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Tue, 18 Jun 2024 11:34:57 -0700 Subject: [PATCH] Improved code --- app/helpers/pg_hero/home_helper.rb | 2 +- lib/pghero/database.rb | 2 +- lib/pghero/methods/explain.rb | 2 +- lib/pghero/methods/query_stats.rb | 2 +- lib/pghero/methods/suggested_indexes.rb | 2 +- lib/pghero/methods/system.rb | 4 ++-- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/helpers/pg_hero/home_helper.rb b/app/helpers/pg_hero/home_helper.rb index 6caa2d4c4..00f766c41 100644 --- a/app/helpers/pg_hero/home_helper.rb +++ b/app/helpers/pg_hero/home_helper.rb @@ -5,7 +5,7 @@ def pghero_pretty_ident(table, schema: nil) if schema && schema != "public" ident = "#{schema}.#{table}" end - if ident =~ /\A[a-z0-9_]+\z/ + if /\A[a-z0-9_]+\z/.match?(ident) ident else @database.quote_ident(ident) diff --git a/lib/pghero/database.rb b/lib/pghero/database.rb index fa03e89be..2b70dd853 100644 --- a/lib/pghero/database.rb +++ b/lib/pghero/database.rb @@ -126,7 +126,7 @@ def connection_model # rough check for Postgres adapter # keep this message generic so it's useful # when empty url set in Docker image pghero.yml - unless @connection_model.connection.adapter_name =~ /postg/i + unless @connection_model.connection.adapter_name.match?(/postg/i) raise Error, "Invalid connection URL" end @adapter_checked = true diff --git a/lib/pghero/methods/explain.rb b/lib/pghero/methods/explain.rb index f997eeddc..7189667a5 100644 --- a/lib/pghero/methods/explain.rb +++ b/lib/pghero/methods/explain.rb @@ -9,7 +9,7 @@ def explain(sql) # use transaction for safety with_transaction(statement_timeout: (explain_timeout_sec * 1000).round, rollback: true) do - if (sql.sub(/;\z/, "").include?(";") || sql.upcase.include?("COMMIT")) && !explain_safe? + if (sql.delete_suffix(";").include?(";") || sql.upcase.include?("COMMIT")) && !explain_safe? raise ActiveRecord::StatementInvalid, "Unsafe statement" end explanation = execute("EXPLAIN #{sql}").map { |v| v["QUERY PLAN"] }.join("\n") diff --git a/lib/pghero/methods/query_stats.rb b/lib/pghero/methods/query_stats.rb index 03c6d5cc7..ab83f9c87 100644 --- a/lib/pghero/methods/query_stats.rb +++ b/lib/pghero/methods/query_stats.rb @@ -322,7 +322,7 @@ def combine_query_stats(grouped_stats) all_queries_total_minutes: stats2.sum { |s| s[:all_queries_total_minutes] } } value[:total_percent] = value[:total_minutes] * 100.0 / value[:all_queries_total_minutes] - value[:explainable_query] = stats2.map { |s| s[:explainable_query] }.select { |q| q && explainable?(q) }.first + value[:explainable_query] = stats2.map { |s| s[:explainable_query] }.find { |q| q && explainable?(q) } query_stats << value end query_stats diff --git a/lib/pghero/methods/suggested_indexes.rb b/lib/pghero/methods/suggested_indexes.rb index 26431850d..9d17560e2 100644 --- a/lib/pghero/methods/suggested_indexes.rb +++ b/lib/pghero/methods/suggested_indexes.rb @@ -46,7 +46,7 @@ def suggested_indexes_by_query(queries: nil, query_stats: nil, indexes: nil) indexes += existing_columns["brin"][index[:table]] end - covering_index = indexes.find { |e| index_covers?(e.map { |v| v.sub(/ inet_ops\z/, "") }, index[:columns]) } + covering_index = indexes.find { |e| index_covers?(e.map { |v| v.delete_suffix(" inet_ops") }, index[:columns]) } if covering_index best_index[:covering_index] = covering_index best_index[:explanation] = "Covered by index on (#{covering_index.join(", ")})" diff --git a/lib/pghero/methods/system.rb b/lib/pghero/methods/system.rb index 556ff0ea5..92ed19424 100644 --- a/lib/pghero/methods/system.rb +++ b/lib/pghero/methods/system.rb @@ -136,8 +136,8 @@ def gcp_stats(metric_name, duration: nil, period: nil, offset: nil, series: fals start_time = end_time - duration # validate input since we need to interpolate below - raise Error, "Invalid metric name" unless metric_name =~ /\A[a-z\/_]+\z/i - raise Error, "Invalid database id" unless gcp_database_id =~ /\A[a-z0-9\-:]+\z/i + raise Error, "Invalid metric name" unless /\A[a-z\/_]+\z/i.match?(metric_name) + raise Error, "Invalid database id" unless /\A[a-z0-9\-:]+\z/i.match?(gcp_database_id) # we handle four situations: # 1. google-cloud-monitoring-v3