inventory_refresh 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +6 -0
- data/Gemfile +4 -0
- data/bundler.d/.gitkeep +0 -0
- data/inventory_refresh.gemspec +4 -4
- data/lib/inventory_refresh/inventory_collection/builder.rb +249 -0
- data/lib/inventory_refresh/inventory_collection/graph.rb +0 -15
- data/lib/inventory_refresh/inventory_collection/helpers/associations_helper.rb +80 -0
- data/lib/inventory_refresh/inventory_collection/helpers/initialize_helper.rb +456 -0
- data/lib/inventory_refresh/inventory_collection/helpers/questions_helper.rb +132 -0
- data/lib/inventory_refresh/inventory_collection/helpers.rb +6 -0
- data/lib/inventory_refresh/inventory_collection/index/type/skeletal.rb +5 -5
- data/lib/inventory_refresh/inventory_collection/reference.rb +4 -0
- data/lib/inventory_refresh/inventory_collection/scanner.rb +111 -18
- data/lib/inventory_refresh/inventory_collection/serialization.rb +7 -7
- data/lib/inventory_refresh/inventory_collection/unconnected_edge.rb +19 -0
- data/lib/inventory_refresh/inventory_collection.rb +114 -649
- data/lib/inventory_refresh/inventory_object.rb +17 -11
- data/lib/inventory_refresh/inventory_object_lazy.rb +20 -10
- data/lib/inventory_refresh/persister.rb +212 -0
- data/lib/inventory_refresh/save_collection/base.rb +18 -3
- data/lib/inventory_refresh/save_collection/saver/base.rb +25 -62
- data/lib/inventory_refresh/save_collection/saver/concurrent_safe_batch.rb +73 -225
- data/lib/inventory_refresh/save_collection/saver/partial_upsert_helper.rb +226 -0
- data/lib/inventory_refresh/save_collection/saver/retention_helper.rb +115 -0
- data/lib/inventory_refresh/save_collection/saver/sql_helper.rb +122 -0
- data/lib/inventory_refresh/save_collection/saver/sql_helper_update.rb +24 -5
- data/lib/inventory_refresh/save_collection/saver/sql_helper_upsert.rb +6 -6
- data/lib/inventory_refresh/save_collection/sweeper.rb +69 -0
- data/lib/inventory_refresh/save_inventory.rb +18 -8
- data/lib/inventory_refresh/target_collection.rb +12 -0
- data/lib/inventory_refresh/version.rb +1 -1
- data/lib/inventory_refresh.rb +1 -0
- metadata +24 -15
- data/lib/inventory_refresh/save_collection/recursive.rb +0 -52
- data/lib/inventory_refresh/save_collection/saver/concurrent_safe.rb +0 -71
@@ -1,52 +0,0 @@
|
|
1
|
-
require "inventory_refresh/save_collection/base"
|
2
|
-
|
3
|
-
module InventoryRefresh::SaveCollection
|
4
|
-
class Recursive < InventoryRefresh::SaveCollection::Base
|
5
|
-
class << self
|
6
|
-
# Saves the passed InventoryCollection objects by recursively passing the graph
|
7
|
-
#
|
8
|
-
# @param ems [ExtManagementSystem] manager owning the inventory_collections
|
9
|
-
# @param inventory_collections [Array<InventoryRefresh::InventoryCollection>] array of InventoryCollection objects
|
10
|
-
# for saving
|
11
|
-
def save_collections(ems, inventory_collections)
|
12
|
-
graph = InventoryRefresh::InventoryCollection::Graph.new(inventory_collections)
|
13
|
-
graph.build_directed_acyclic_graph!
|
14
|
-
|
15
|
-
graph.nodes.each do |inventory_collection|
|
16
|
-
save_collection(ems, inventory_collection, [])
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
private
|
21
|
-
|
22
|
-
# Saves the one passed InventoryCollection object
|
23
|
-
#
|
24
|
-
# @param ems [ExtManagementSystem] manager owning the inventory_collections
|
25
|
-
# @param inventory_collection [InventoryRefresh::InventoryCollection] InventoryCollection object for saving
|
26
|
-
# @param traversed_collections [Array<InventoryRefresh::InventoryCollection>] array of traversed InventoryCollection
|
27
|
-
# objects, that we use for detecting possible cycle
|
28
|
-
def save_collection(ems, inventory_collection, traversed_collections)
|
29
|
-
unless inventory_collection.kind_of?(::InventoryRefresh::InventoryCollection)
|
30
|
-
raise "A InventoryRefresh::SaveInventory needs a InventoryCollection object, it got: #{inventory_collection.inspect}"
|
31
|
-
end
|
32
|
-
|
33
|
-
return if inventory_collection.saved?
|
34
|
-
|
35
|
-
traversed_collections << inventory_collection
|
36
|
-
|
37
|
-
unless inventory_collection.saveable?
|
38
|
-
inventory_collection.dependencies.each do |dependency|
|
39
|
-
next if dependency.saved?
|
40
|
-
if traversed_collections.include?(dependency)
|
41
|
-
raise "Edge from #{inventory_collection} to #{dependency} creates a cycle"
|
42
|
-
end
|
43
|
-
save_collection(ems, dependency, traversed_collections)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
logger.debug("Saving #{inventory_collection} of size #{inventory_collection.size}")
|
48
|
-
save_inventory_object_inventory(ems, inventory_collection)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
@@ -1,71 +0,0 @@
|
|
1
|
-
require "inventory_refresh/save_collection/saver/base"
|
2
|
-
|
3
|
-
module InventoryRefresh::SaveCollection
|
4
|
-
module Saver
|
5
|
-
class ConcurrentSafe < InventoryRefresh::SaveCollection::Saver::Base
|
6
|
-
# TODO(lsmola) this strategy does not make much sense, it's better to use concurent_safe_batch and make batch size
|
7
|
-
# configurable
|
8
|
-
private
|
9
|
-
|
10
|
-
# Updates the passed record with hash data and stores primary key value into inventory_object.
|
11
|
-
#
|
12
|
-
# @param record [ApplicationRecord] record we want to update in DB
|
13
|
-
# @param hash [Hash] data we want to update the record with
|
14
|
-
# @param inventory_object [InventoryRefresh::InventoryObject] InventoryObject instance where we will store primary
|
15
|
-
# key value
|
16
|
-
def update_record!(record, hash, inventory_object)
|
17
|
-
assign_attributes_for_update!(hash, time_now)
|
18
|
-
record.assign_attributes(hash.except(:id))
|
19
|
-
|
20
|
-
if !inventory_object.inventory_collection.check_changed? || record.changed?
|
21
|
-
update_query = inventory_object.inventory_collection.model_class.where(:id => record.id)
|
22
|
-
if hash[:remote_data_timestamp]
|
23
|
-
timestamp_field = inventory_collection.model_class.arel_table[:remote_data_timestamp]
|
24
|
-
update_query = update_query.where(timestamp_field.lt(hash[:remote_data_timestamp]))
|
25
|
-
end
|
26
|
-
|
27
|
-
update_query.update_all(hash)
|
28
|
-
inventory_collection.store_updated_records(record)
|
29
|
-
end
|
30
|
-
|
31
|
-
inventory_object.id = record.id
|
32
|
-
end
|
33
|
-
|
34
|
-
# Creates a new record in the DB using the passed hash data
|
35
|
-
#
|
36
|
-
# @param hash [Hash] hash with data we want to persist to DB
|
37
|
-
# @param inventory_object [InventoryRefresh::InventoryObject] InventoryObject instance where we will store primary
|
38
|
-
# key value
|
39
|
-
def create_record!(hash, inventory_object)
|
40
|
-
all_attribute_keys = hash.keys
|
41
|
-
data = inventory_collection.model_class.new(hash).attributes.symbolize_keys
|
42
|
-
|
43
|
-
# TODO(lsmola) abstract common behavior into base class
|
44
|
-
all_attribute_keys << :type if supports_sti?
|
45
|
-
all_attribute_keys << :created_at if supports_created_at?
|
46
|
-
all_attribute_keys << :updated_at if supports_updated_at?
|
47
|
-
all_attribute_keys << :created_on if supports_created_on?
|
48
|
-
all_attribute_keys << :updated_on if supports_updated_on?
|
49
|
-
hash_for_creation = if inventory_collection.use_ar_object?
|
50
|
-
record = inventory_collection.model_class.new(data)
|
51
|
-
values_for_database!(all_attribute_keys,
|
52
|
-
record.attributes.symbolize_keys)
|
53
|
-
elsif serializable_keys?
|
54
|
-
values_for_database!(all_attribute_keys,
|
55
|
-
data)
|
56
|
-
else
|
57
|
-
data
|
58
|
-
end
|
59
|
-
|
60
|
-
assign_attributes_for_create!(hash_for_creation, time_now)
|
61
|
-
|
62
|
-
result_id = ActiveRecord::Base.connection.execute(
|
63
|
-
build_insert_query(all_attribute_keys, [hash_for_creation])
|
64
|
-
)
|
65
|
-
|
66
|
-
inventory_object.id = result_id.to_a.try(:first).try(:[], "id")
|
67
|
-
inventory_collection.store_created_records(inventory_object)
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|