From dc70e6b18d5d9016ef0ba4797e5705c58ca33f97 Mon Sep 17 00:00:00 2001 From: Joe Rafaniello Date: Wed, 16 Aug 2023 17:25:03 -0400 Subject: [PATCH] Log the tables sizes and statistics with db activity 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. --- app/models/vmdb_database_connection.rb | 43 +++++++++++++++++--------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/app/models/vmdb_database_connection.rb b/app/models/vmdb_database_connection.rb index e3c717d9a442..ae1f57dd5cdf 100644 --- a/app/models/vmdb_database_connection.rb +++ b/app/models/vmdb_database_connection.rb @@ -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