Class: SourceMonitor::Items::RetentionPruner

Inherits:
Object
  • Object
show all
Defined in:
lib/source_monitor/items/retention_pruner.rb

Overview

Removes items that fall outside the configured retention rules for a source. Supports age-based (items_retention_days) and count-based (max_items) limits.

Defined Under Namespace

Classes: Result

Constant Summary collapse

STRATEGY_CLASSES =
{
  destroy: SourceMonitor::Items::RetentionStrategies::Destroy,
  soft_delete: SourceMonitor::Items::RetentionStrategies::SoftDelete
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source:, now: Time.current, strategy: nil) ⇒ RetentionPruner

Returns a new instance of RetentionPruner.



28
29
30
31
32
33
# File 'lib/source_monitor/items/retention_pruner.rb', line 28

def initialize(source:, now: Time.current, strategy: nil)
  @source = source
  @now = now
  @strategy_name = normalize_strategy(strategy)
  @strategy_handler = STRATEGY_CLASSES.fetch(@strategy_name).new(source: source)
end

Class Method Details

.call(source:, now: Time.current, strategy: nil) ⇒ Object



24
25
26
# File 'lib/source_monitor/items/retention_pruner.rb', line 24

def self.call(source:, now: Time.current, strategy: nil)
  new(source:, now:, strategy:).call
end

Instance Method Details

#callObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/source_monitor/items/retention_pruner.rb', line 35

def call
  removed_by_age = prune_by_age
  removed_by_limit = prune_by_limit
  removed_total = removed_by_age + removed_by_limit

  if removed_total.positive?
    SourceMonitor::Instrumentation.item_retention(
      source_id: source.id,
      removed_by_age:,
      removed_by_limit:,
      removed_total:,
      items_retention_days: items_retention_days,
      max_items: max_items_limit
    )
  end

  Result.new(
    removed_by_age:,
    removed_by_limit:,
    removed_total:
  )
end