Class: InventoryRefresh::SaveCollection::Sweeper

Inherits:
Base
  • Object
show all
Includes:
InventoryRefresh::SaveCollection::Saver::RetentionHelper
Defined in:
lib/inventory_refresh/save_collection/sweeper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

save_inventory_object_inventory

Methods included from Logging

#logger

Constructor Details

#initialize(inventory_collection, refresh_state) ⇒ Sweeper

Returns a new instance of Sweeper.



43
44
45
46
47
48
49
# File 'lib/inventory_refresh/save_collection/sweeper.rb', line 43

def initialize(inventory_collection, refresh_state)
  @inventory_collection = inventory_collection
  @refresh_state        = refresh_state

  @model_class            = inventory_collection.model_class
  @primary_key            = @model_class.primary_key
end

Instance Attribute Details

#inventory_collectionObject (readonly)

Returns the value of attribute inventory_collection.



41
42
43
# File 'lib/inventory_refresh/save_collection/sweeper.rb', line 41

def inventory_collection
  @inventory_collection
end

#model_classObject (readonly)

Returns the value of attribute model_class.



41
42
43
# File 'lib/inventory_refresh/save_collection/sweeper.rb', line 41

def model_class
  @model_class
end

#primary_keyObject (readonly)

Returns the value of attribute primary_key.



41
42
43
# File 'lib/inventory_refresh/save_collection/sweeper.rb', line 41

def primary_key
  @primary_key
end

#refresh_stateObject (readonly)

Returns the value of attribute refresh_state.



41
42
43
# File 'lib/inventory_refresh/save_collection/sweeper.rb', line 41

def refresh_state
  @refresh_state
end

Class Method Details

.in_scope?(inventory_collection, sweep_scope) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
# File 'lib/inventory_refresh/save_collection/sweeper.rb', line 28

def in_scope?(inventory_collection, sweep_scope)
  return true unless sweep_scope

  if sweep_scope.kind_of?(Array)
    return true if sweep_scope.include?(inventory_collection&.name&.to_s)
  end

  false
end

.sweep(_ems, inventory_collections, refresh_state) ⇒ Object

Sweeps inactive records based on :last_seen_on and :refresh_start timestamps. All records having :last_seen_on lower than :refresh_start or nil will be archived/deleted.

Parameters:

  • _ems (ActiveRecord)

    Manager owning the inventory_collections

  • inventory_collections (Array<InventoryRefresh::InventoryCollection>)

    Array of InventoryCollection objects for sweeping

  • refresh_state (ActiveRecord)

    Record of :refresh_states



14
15
16
17
18
19
20
# File 'lib/inventory_refresh/save_collection/sweeper.rb', line 14

def sweep(_ems, inventory_collections, refresh_state)
  inventory_collections.each do |inventory_collection|
    next unless sweep_possible?(inventory_collection, refresh_state)

    new(inventory_collection, refresh_state).sweep
  end
end

.sweep_possible?(inventory_collection, refresh_state) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
# File 'lib/inventory_refresh/save_collection/sweeper.rb', line 22

def sweep_possible?(inventory_collection, refresh_state)
  inventory_collection.supports_column?(:last_seen_at) && inventory_collection.parallel_safe? &&
    inventory_collection.strategy == :local_db_find_missing_references &&
    in_scope?(inventory_collection, refresh_state.sweep_scope)
end

Instance Method Details

#sweepObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/inventory_refresh/save_collection/sweeper.rb', line 51

def sweep
  refresh_start = refresh_state.created_at
  raise "Couldn't load :created_at out of RefreshState record: #{refresh_state}" unless refresh_start

  table       = model_class.arel_table
  date_field  = table[:last_seen_at]
  all_entities_query = inventory_collection.full_collection_for_comparison
  all_entities_query.active if inventory_collection.retention_strategy == :archive

  query = all_entities_query
          .where(date_field.lt(refresh_start)).or(all_entities_query.where(:last_seen_at => nil))
          .select(table[:id])

  query.find_in_batches do |batch|
    destroy_records!(batch)
  end
end