Module: Legion::Data::Archiver

Extended by:
Logging::Helper
Defined in:
lib/legion/data/archiver.rb

Defined Under Namespace

Classes: UploadError

Class Method Summary collapse

Methods included from Logging::Helper

handle_exception

Class Method Details

.archive_table(table:, retention_days: 90, batch_size: 1000, storage_backend: nil) ⇒ Object



19
20
21
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
48
# File 'lib/legion/data/archiver.rb', line 19

def archive_table(table:, retention_days: 90, batch_size: 1000, storage_backend: nil)
  return { skipped: true, reason: 'not_postgres' } unless postgres?

  log.info "Archiving table #{table} (retention: #{retention_days}d)"

  conn = Legion::Data.connection
  cutoff = Time.now - (retention_days * 86_400)
  archive_results = archive_batches(
    conn:            conn,
    table:           table,
    cutoff:          cutoff,
    batch_size:      batch_size,
    storage_backend: storage_backend
  )

  log.info "Archived #{archive_results[:total_rows]} rows from #{table} in #{archive_results[:batches]} batch(es)"
  archive_results
rescue StandardError => e
  handle_exception(
    e,
    level:           :error,
    handled:         false,
    operation:       :archive_table,
    table:           table,
    retention_days:  retention_days,
    batch_size:      batch_size,
    storage_backend: storage_backend
  )
  raise
end

.manifest_statsObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/legion/data/archiver.rb', line 62

def manifest_stats
  return {} unless postgres?
  return {} unless Legion::Data.connection.table_exists?(:archive_manifest)

  Legion::Data.connection[:archive_manifest]
              .group_and_count(:source_table)
              .select_append(
                Sequel.function(:sum, :row_count).as(:total_rows),
                Sequel.function(:min, :archived_at).as(:earliest),
                Sequel.function(:max, :archived_at).as(:latest)
              )
              .all
              .to_h do |row|
    [row[:source_table], {
      batches:    row[:count],
      total_rows: row[:total_rows].to_i,
      earliest:   row[:earliest],
      latest:     row[:latest]
    }]
  end
end

.upload_batch(data:, table:, year:, month:, batch_n:, backend:) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/legion/data/archiver.rb', line 50

def upload_batch(data:, table:, year:, month:, batch_n:, backend:)
  log.info "Archiver storing batch table=#{table} backend=#{backend || :tmpdir} year=#{year} month=#{month} batch=#{batch_n}"
  case backend
  when :s3
    upload_s3(data: data, table: table, year: year, month: month, batch_n: batch_n)
  when :azure
    upload_azure(data: data, table: table, year: year, month: month, batch_n: batch_n)
  else
    upload_tmpdir(data: data, table: table, year: year, month: month, batch_n: batch_n)
  end
end