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.



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

def initialize(context)
  @context = context
end

Class Method Details

.bucket(row_count) ⇒ String?

Returns safe size bucket label.

Parameters:

  • row_count (Integer, nil)

Returns:

  • (String, nil)

    safe size bucket label



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

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



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

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

Instance Method Details

#bucketString?

Returns safe size bucket label.

Parameters:

  • row_count (Integer, nil)

Returns:

  • (String, nil)

    safe size bucket label



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

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



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

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