Skip to content

Commit

Permalink
Log the tables sizes and statistics with db activity
Browse files Browse the repository at this point in the history
It's helpful to log the tables sorted by row counts and other information such as the
autovacuum/analzye times.

Even if we truncate the line to 8k (default for the logger), the largest row tables
should be represented.
  • Loading branch information
jrafanie committed Aug 16, 2023
1 parent cd1594a commit dc70e6b
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions app/models/vmdb_database_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,40 @@ class VmdbDatabaseConnection < ApplicationRecord
virtual_column :blocked_by, :type => :integer

def self.log_statistics(output = $log)
log_activity(output)
log_table_size(output)
log_table_statistics(output)
end

def self.log_csv(keys, stats, label, output)
require 'csv'
csv = CSV.generate do |rows|
rows << keys
stats.each { |s| rows << s.values_at(*keys) }
end

begin
stats = all.map(&:to_csv_hash)
output.info("MIQ(#{name}.#{__method__}) <<-#{label}\n#{csv}#{label}")
end

keys = stats.first.keys
def self.log_activity(output = $log)
stats = all.map(&:to_csv_hash)
log_csv(stats.first.keys, stats, "ACTIVITY_STATS_CSV", output)
rescue => err
output.warn("MIQ(#{name}.#{__method__}) Unable to log activity, '#{err.message}'")
end

csv = CSV.generate do |rows|
rows << keys
stats.each do |s|
vals = s.values_at(*keys)
rows << vals
end
end
def self.log_table_size(output = $log)
stats = ApplicationRecord.connection.table_size
log_csv(stats.first.keys, stats, "TABLE_SIZE_CSV", output)
rescue => err
output.warn("MIQ(#{name}.#{__method__}) Unable to log activity, '#{err.message}'")
end

output.info("MIQ(#{name}.#{__method__}) <<-ACTIVITY_STATS_CSV\n#{csv}ACTIVITY_STATS_CSV")
rescue => err
output.warn("MIQ(#{name}.#{__method__}) Unable to log stats, '#{err.message}'")
end
def self.log_table_statistics(output = $log)
stats = ApplicationRecord.connection.table_statistics.sort_by { |h| h['rows_live'] }.reverse!
log_csv(stats.first.keys, stats, "TABLE_STATS_CSV", output)
rescue => err
output.warn("MIQ(#{name}.#{__method__}) Unable to log activity, '#{err.message}'")
end

def address
Expand Down

0 comments on commit dc70e6b

Please sign in to comment.