Class: InventoryRefresh::SaveCollection::Sweeper
- Defined in:
- lib/inventory_refresh/save_collection/sweeper.rb
Instance Attribute Summary collapse
-
#inventory_collection ⇒ Object
readonly
Returns the value of attribute inventory_collection.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#primary_key ⇒ Object
readonly
Returns the value of attribute primary_key.
-
#refresh_state ⇒ Object
readonly
Returns the value of attribute refresh_state.
Class Method Summary collapse
- .in_scope?(inventory_collection, sweep_scope) ⇒ Boolean
-
.sweep(_ems, inventory_collections, refresh_state) ⇒ Object
Sweeps inactive records based on :last_seen_on and :refresh_start timestamps.
- .sweep_possible?(inventory_collection, refresh_state) ⇒ Boolean
Instance Method Summary collapse
-
#initialize(inventory_collection, refresh_state) ⇒ Sweeper
constructor
A new instance of Sweeper.
- #sweep ⇒ Object
Methods inherited from Base
save_inventory_object_inventory
Methods included from Logging
Constructor Details
#initialize(inventory_collection, refresh_state) ⇒ Sweeper
Returns a new instance of Sweeper.
40 41 42 43 44 45 46 |
# File 'lib/inventory_refresh/save_collection/sweeper.rb', line 40 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_collection ⇒ Object (readonly)
Returns the value of attribute inventory_collection.
38 39 40 |
# File 'lib/inventory_refresh/save_collection/sweeper.rb', line 38 def inventory_collection @inventory_collection end |
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
38 39 40 |
# File 'lib/inventory_refresh/save_collection/sweeper.rb', line 38 def model_class @model_class end |
#primary_key ⇒ Object (readonly)
Returns the value of attribute primary_key.
38 39 40 |
# File 'lib/inventory_refresh/save_collection/sweeper.rb', line 38 def primary_key @primary_key end |
#refresh_state ⇒ Object (readonly)
Returns the value of attribute refresh_state.
38 39 40 |
# File 'lib/inventory_refresh/save_collection/sweeper.rb', line 38 def refresh_state @refresh_state end |
Class Method Details
.in_scope?(inventory_collection, sweep_scope) ⇒ Boolean
28 29 30 31 32 33 |
# File 'lib/inventory_refresh/save_collection/sweeper.rb', line 28 def in_scope?(inventory_collection, sweep_scope) return true unless sweep_scope return true if sweep_scope.kind_of?(Array) && sweep_scope.include?(inventory_collection&.name&.to_s) 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.
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
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
#sweep ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/inventory_refresh/save_collection/sweeper.rb', line 48 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 |