Class: PartitionGardener::Audit

Inherits:
Object
  • Object
show all
Defined in:
lib/partition_gardener/audit.rb

Defined Under Namespace

Classes: AuditResult

Constant Summary collapse

SCHEMA_VERSION =
"1.0"
HORIZON_WARNING_DAYS =
30

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, config: nil) ⇒ Audit

Returns a new instance of Audit.



20
21
22
23
# File 'lib/partition_gardener/audit.rb', line 20

def initialize(table_name, config: nil)
  @table_name = table_name
  @config = config || Registry.find_by_table_name(table_name)
end

Class Method Details

.call(table_name, config: Registry.find_by_table_name(table_name)) ⇒ Object



16
17
18
# File 'lib/partition_gardener/audit.rb', line 16

def self.call(table_name, config: Registry.find_by_table_name(table_name))
  new(table_name, config: config).call
end

Instance Method Details

#callObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/partition_gardener/audit.rb', line 25

def call
  unless Connection.table_is_partitioned?(@table_name)
    return AuditResult.new(
      table_name: @table_name,
      partitioned: false,
      default_row_count: 0,
      attached_child_count: 0,
      horizon_days: nil,
      gaps: [],
      warnings: ["#{@table_name} is not a partitioned table"]
    )
  end

  default_name = Naming.default_partition_name(@table_name)
  default_row_count = if Connection.partition_exists?(default_name)
    Connection.count_rows_in_partition_table(default_name)
  else
    0
  end

  partitions = Connection.attached_partitions(@table_name)
  warnings = []
  warnings << "default partition #{default_name} has #{default_row_count} rows" if default_row_count.positive?
  warnings << "default partition #{default_name} is missing" unless Connection.partition_exists?(default_name)

  horizon_days = compute_horizon_days(partitions)
  if horizon_days && horizon_days < HORIZON_WARNING_DAYS
    warnings << "partition horizon is #{horizon_days} days ahead (below #{HORIZON_WARNING_DAYS})"
  end

  if partitions.size > 200
    warnings << "attached child count is #{partitions.size} (high catalog pressure)"
  end

  gaps = GapDetection.call(@table_name, config: @config)
  gaps.each do |gap|
    warnings << "partition gap: #{gap.message}"
  end

  AuditResult.new(
    table_name: @table_name,
    partitioned: true,
    default_row_count: default_row_count,
    attached_child_count: partitions.size,
    horizon_days: horizon_days,
    gaps: gaps,
    warnings: warnings
  )
end