Skip to content

Commit

Permalink
Add small tree model and mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
stitching-coder committed Apr 30, 2024
1 parent 5ee409f commit 6642e92
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
7 changes: 7 additions & 0 deletions backend/app/model/bulk_updater_small_tree.rb
@@ -0,0 +1,7 @@
class BulkUpdaterSmallTree

def self.for_resource(resource_id)
Resource.get_or_die(resource_id).bulk_updater_quick_tree
end

end
96 changes: 96 additions & 0 deletions backend/app/model/mixins/trees.rb
Expand Up @@ -301,6 +301,102 @@ def trigger_index_of_entire_tree
update(:system_mtime => Time.now)
end

def bulk_updater_quick_tree
links = {}
properties = {}

root_type = self.class.root_type
node_type = self.class.node_type

top_nodes = []

container_info = bulk_updater_fetch_container_info

query = build_node_query

offset = 0
loop do
nodes = query.limit(NODE_PAGE_SIZE, offset)

nodes.each do |node|
if node.parent_id
links[node.parent_id] ||= []
links[node.parent_id] << [node.position, node.id]
else
top_nodes << [node.position, node.id]
end

properties[node.id] = {
:title => node.display_string,
:uri => self.class.uri_for(node_type, node.id),
:ref_id => node[:ref_id],
:component_id => node[:component_id],
:container => container_info.fetch(node.id, nil),
}

# Drop out nils to keep the object size as small as possible
properties[node.id].keys.each do |key|
properties[node.id].delete(key) if properties[node.id][key].nil?
end
end

if nodes.empty?
break
else
offset += NODE_PAGE_SIZE
end
end

result = {
:title => self.title,
:identifier => Identifiers.format(Identifiers.parse(self.identifier)),
:children => top_nodes.sort_by(&:first).map {|_, node| self.class.assemble_tree(node, links, properties)},
:uri => self.class.uri_for(root_type, self.id)
}

result
end

private

def bulk_updater_containers_ds
TopContainer.linked_instance_ds
.join(:archival_object, :id => :instance__archival_object_id)
.left_join(:enumeration_value___top_container_type, :id => :top_container__type_id)
.left_join(:enumeration_value___sub_container_type_2, :id => :sub_container__type_2_id)
.left_join(:enumeration_value___sub_container_type_3, :id => :sub_container__type_3_id)
.filter(:archival_object__root_record_id => self.id)
.select(Sequel.as(:archival_object__id, :archival_object_id),
Sequel.as(:top_container__barcode, :top_container_barcode),
Sequel.as(:top_container_type__value, :top_container_type),
Sequel.as(:top_container__indicator, :top_container_indicator),
Sequel.as(:sub_container_type_2__value, :sub_container_type_2),
Sequel.as(:sub_container__indicator_2, :sub_container_indicator_2),
Sequel.as(:sub_container_type_3__value, :sub_container_type_3),
Sequel.as(:sub_container__indicator_3, :sub_container_indicator_3))
end


def bulk_updater_fetch_container_info
result = {}

bulk_updater_containers_ds.each do |row|
result[row[:archival_object_id]] = [
# BoxType Indicator [Barcode]
[row[:top_container_type],
row[:top_container_indicator],
row[:top_container_barcode] ? ('[' + row[:top_container_barcode] + ']') : nil].compact.join(': '),

# BoxType_2 Indicator_2
[row[:sub_container_type_2], row[:sub_container_indicator_2]].compact.join(': '),

# BoxType_3 Indicator_3
[row[:sub_container_type_3], row[:sub_container_indicator_3]].compact.join(': '),
].reject(&:empty?).join(', ')
end

result
end

module ClassMethods

Expand Down
7 changes: 7 additions & 0 deletions backend/spec/model_tree_spec.rb
Expand Up @@ -107,4 +107,11 @@
expect(Resource[create(:json_resource).id].children?).to be_falsey
end

it "creates the bulk updater tree" do
quick_tree = Resource[resource.id].bulk_updater_quick_tree
expect(quick_tree[:title]).to eq(Resource[resource.id].title)
expect(quick_tree[:uri]).to eq(Resource[resource.id].uri)
expect(quick_tree[:identifier]).to eq(Identifiers.format(Identifiers.parse(Resource[resource.id].identifier)))
expect(quick_tree[:children].map {|r| r[:uri]} - Resource[resource.id].ordered_records.map {|r| r['ref']}).to eq([])
end
end

0 comments on commit 6642e92

Please sign in to comment.