Skip to content

Commit

Permalink
[58520] Fixed hashed subtree flattening
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Pfohl committed Nov 4, 2024
1 parent 382268e commit a1c2c27
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ def soft_delete_item(item)
raise NotImplementedError
end

def hashed_subtree(item:, depth:)
Success(item.hash_tree(limit_depth: depth + 1))
end

private

def create_root_item(custom_field)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ module V3
module CustomFields
module Hierarchy
class HierarchyItemCollectionRepresenter < ::API::Decorators::UnpaginatedCollection
property :count, getter: ->(*) { count.size }

def model_count(models)
super.size
end
end
end
end
Expand Down
31 changes: 14 additions & 17 deletions lib/api/v3/custom_fields/hierarchy/items_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ module V3
module CustomFields
module Hierarchy
class ItemsAPI < ::API::OpenProjectAPI
include Dry::Monads[:result]

helpers do
def start_item(root, parent)
return root if parent.nil?
Expand All @@ -42,28 +44,18 @@ def start_item(root, parent)
start
end

def item_list(item, depth)
return item.self_and_descendants_preordered if depth.nil?

d = parse_int(depth)
raise ::API::Errors::InvalidQuery.new("Depth must be a natural number.") if d.nil? || d < 0

# t = flatten_tree_hash item.hash_tree(limit_depth: d + 1)

item.self_and_descendants_preordered
end

def flatten_tree_hash(hash)
flat_list = hash.keys

queue = hash.values
flat_list = []
queue = [hash]

# {:a => {:b => {:c1 => {:d1 => {}}, :c2 => {:d2 => {}}}, :b2 => {}}}

while queue.any?
current = queue.shift
flat_list += current.keys
queue.unshift(current.values)
item, children = current.shift
flat_list << item
queue.unshift(current) unless current.empty?
queue.unshift(children) unless children.empty?
end

flat_list
Expand Down Expand Up @@ -99,7 +91,12 @@ def parse_int(str)

self_link = api_v3_paths.custom_field_items(@custom_field.id, params[:parent], params[:depth])
start = start_item(root_item.first, params[:parent])
models = item_list(start, params[:depth])

models = ::CustomFields::Hierarchy::HierarchicalItemService
.new.hashed_subtree(item: start,
depth: parse_int(params[:depth])).fmap do |subtree|
flatten_tree_hash(subtree)
end.value!
HierarchyItemCollectionRepresenter.new(models, self_link:, current_user:)
end
end
Expand Down

0 comments on commit a1c2c27

Please sign in to comment.