Class: RailsAiBridge::DatabaseSize

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_bridge/database_size.rb

Overview

Shared utility for mapping approximate database row counts to safe, human-oriented size bucket labels (+small+, medium, large, hot).

Extracted from Serializers::ContextSummary so that both the introspection layer and the serializer layer can use it without introspectors depending on serializers (an ArchSpec boundary violation).

Defined Under Namespace

Classes: BucketLabel

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ DatabaseSize

Returns a new instance of DatabaseSize.



25
26
27
# File 'lib/rails_ai_bridge/database_size.rb', line 25

def initialize(context)
  @context = context
end

Class Method Details

.bucket(row_count) ⇒ String?

Returns safe size bucket label.

Parameters:

  • row_count (Integer, Float, String, nil) —

    approximate row count, precomputed safe label, or numeric string

Returns:

  • (String, nil) —

    safe size bucket label



14
15
16
# File 'lib/rails_ai_bridge/database_size.rb', line 14

def self.bucket(row_count)
  BucketLabel.new(row_count).label
end

.bucket_for_table(context, table_name) ⇒ String?

Returns safe size bucket label for the table.

Parameters:

  • context (Hash)
  • table_name (String, nil)

Returns:

  • (String, nil) —

    safe size bucket label for the table



21
22
23
# File 'lib/rails_ai_bridge/database_size.rb', line 21

def self.bucket_for_table(context, table_name)
  new(context).bucket_for_table(table_name)
end

Instance Method Details

#bucket ⇒ String?

Returns safe size bucket label.

Parameters:

  • row_count (Integer, Float, String, nil) —

    approximate row count, precomputed safe label, or numeric string

Returns:

  • (String, nil) —

    safe size bucket label



32
# File 'lib/rails_ai_bridge/database_size.rb', line 32

delegate :bucket, to: :class

#bucket_for_table(table_name) ⇒ String?

Returns safe size bucket label for the table.

Parameters:

  • table_name (String, nil)

Returns:

  • (String, nil) —

    safe size bucket label for the table



36
37
38
39
40
41
42
# File 'lib/rails_ai_bridge/database_size.rb', line 36

def bucket_for_table(table_name)
  table_stats = row_for(table_name.to_s)
  return unless table_stats

  stats = table_stats.with_indifferent_access
  bucket(stats[:size_bucket] || stats[:approximate_rows])
end