Class: RailsAiBridge::Introspectors::DatabaseStatsIntrospector
- Inherits:
-
Object
- Object
- RailsAiBridge::Introspectors::DatabaseStatsIntrospector
- Defined in:
- lib/rails_ai_bridge/introspectors/database_stats_introspector.rb
Overview
Collects approximate row counts from PostgreSQL's pg_stat_user_tables. Only activates for PostgreSQL adapter; returns { skipped: true } otherwise.
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
Instance Method Summary collapse
-
#call ⇒ Hash
Collects approximate row counts from PostgreSQL's pg_stat_user_tables.
-
#initialize(app) ⇒ DatabaseStatsIntrospector
constructor
A new instance of DatabaseStatsIntrospector.
Constructor Details
#initialize(app) ⇒ DatabaseStatsIntrospector
Returns a new instance of DatabaseStatsIntrospector.
11 12 13 |
# File 'lib/rails_ai_bridge/introspectors/database_stats_introspector.rb', line 11 def initialize(app) @app = app end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
8 9 10 |
# File 'lib/rails_ai_bridge/introspectors/database_stats_introspector.rb', line 8 def app @app end |
Instance Method Details
#call ⇒ Hash
Collects approximate row counts from PostgreSQL's pg_stat_user_tables.
Returns { skipped: true } for non-PostgreSQL adapters or when
ActiveRecord is unavailable.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rails_ai_bridge/introspectors/database_stats_introspector.rb', line 22 def call return { skipped: true, reason: 'ActiveRecord not available' } unless defined?(ActiveRecord::Base) adapter = ActiveRecord::Base.connection.adapter_name.downcase return { skipped: true, reason: "Only available for PostgreSQL (current: #{adapter})" } unless adapter.include?('postgresql') rows = ActiveRecord::Base.connection.select_all(<<~SQL.squish) SELECT relname AS table_name, n_live_tup AS approximate_row_count FROM pg_stat_user_tables ORDER BY n_live_tup DESC SQL tables = rows.map do |row| approximate_rows = row['approximate_row_count'].to_i { table: row['table_name'], approximate_rows: approximate_rows, size_bucket: Serializers::ContextSummary.database_size_bucket(approximate_rows) } end { adapter: 'postgresql', tables: tables, total_tables: tables.size } rescue StandardError => error { error: error. } end |