class GitLab::Monitor::Database::RowCountCollector
A helper class that executes the query its given and returns an int of the row count This class works under the assumption you do COUNT(*) queries, define queries in the QUERIES
constant. If in doubt how these work, read construct_query
Constants
- MIRROR_QUERY
- QUERIES
- WHERE_MIRROR_ENABLED
Public Class Methods
new(args)
click to toggle source
Calls superclass method
GitLab::Monitor::Database::Base::new
# File lib/gitlab_monitor/database/row_count.rb, line 85 def initialize(args) super(args) @selected_queries = Set.new(args[:selected_queries].map(&:to_sym)) unless args[:selected_queries].nil? end
Public Instance Methods
run()
click to toggle source
# File lib/gitlab_monitor/database/row_count.rb, line 91 def run results = Hash.new(0) QUERIES.each do |key, query_hash| next if query_hash[:check] && !successful_check?(query_hash[:check]) next if !@selected_queries.nil? && !@selected_queries.include?(key) results[key] = count_from_query_hash(query_hash) end results end
Private Instance Methods
construct_query(query)
click to toggle source
Not private so I can test it without meta programming tricks
# File lib/gitlab_monitor/database/row_count.rb, line 129 def construct_query(query) query_string = "SELECT COUNT(*) FROM #{query[:select]} " query_string << "#{query[:joins]} " if query[:joins] query_string << "WHERE #{query[:where]}" if query[:where] query_string << ";" end
count_from_query_hash(query_hash)
click to toggle source
# File lib/gitlab_monitor/database/row_count.rb, line 106 def count_from_query_hash(query_hash) result = execute(construct_query(query_hash)) return 0 unless result result[0]["count"] end
execute(query)
click to toggle source
# File lib/gitlab_monitor/database/row_count.rb, line 120 def execute(query) with_connection_pool do |conn| conn.exec(query) end rescue PG::UndefinedTable, PG::UndefinedColumn nil end
successful_check?(query)
click to toggle source
# File lib/gitlab_monitor/database/row_count.rb, line 113 def successful_check?(query) result = execute("SELECT EXISTS (#{query})") return unless result result[0]["exists"] == "t" end