Module: Legion::Data::StorageTiers

Defined in:
lib/legion/data/storage_tiers.rb

Constant Summary collapse

TIERS =
{ hot: 0, warm: 1, cold: 2 }.freeze

Class Method Summary collapse

Class Method Details

.archive_to_warm(table:, age_days: 90, batch_size: 1000) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/legion/data/storage_tiers.rb', line 9

def archive_to_warm(table:, age_days: 90, batch_size: 1000)
  return { archived: 0, reason: 'no_connection' } unless Legion::Data.connection
  return { archived: 0, reason: 'no_archive_table' } unless Legion::Data.connection.table_exists?(:data_archive)

  cutoff = Time.now - (age_days * 86_400)
  records = Legion::Data.connection[table].where { created_at < cutoff }.limit(batch_size).all
  return { archived: 0 } if records.empty?

  Legion::Data.connection.transaction do
    records.each do |record|
      Legion::Data.connection[:data_archive].insert(
        source_table: table.to_s, source_id: record[:id],
        data:         Legion::JSON.dump(record),
        tier:         TIERS[:warm],
        archived_at:  Time.now.utc
      )
    end

    ids = records.map { |r| r[:id] }
    Legion::Data.connection[table].where(id: ids).delete
  end

  { archived: records.size, table: table.to_s }
end

.export_to_cold(age_days: 365, batch_size: 5000) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/legion/data/storage_tiers.rb', line 34

def export_to_cold(age_days: 365, batch_size: 5000)
  return { exported: 0 } unless Legion::Data.connection&.table_exists?(:data_archive)

  cutoff = Time.now - (age_days * 86_400)
  records = Legion::Data.connection[:data_archive]
                        .where(tier: TIERS[:warm])
                        .where { archived_at < cutoff }
                        .limit(batch_size).all
  return { exported: 0 } if records.empty?

  ids = records.map { |r| r[:id] }
  Legion::Data.connection[:data_archive].where(id: ids).update(tier: TIERS[:cold])
  { exported: records.size, data: records }
end

.statsObject



49
50
51
52
53
# File 'lib/legion/data/storage_tiers.rb', line 49

def stats
  return {} unless Legion::Data.connection&.table_exists?(:data_archive)

  { warm: count_tier(:warm), cold: count_tier(:cold) }
end