Skip to content

Commit

Permalink
Improved code
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jun 18, 2024
1 parent b321b57 commit 58d1bb8
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/helpers/pg_hero/home_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/pghero/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/pghero/methods/explain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion lib/pghero/methods/query_stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/pghero/methods/suggested_indexes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(", ")})"
Expand Down
4 changes: 2 additions & 2 deletions lib/pghero/methods/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 58d1bb8

Please sign in to comment.