Skip to content

Commit

Permalink
[frontend] Refactor calculate_repo_cycle
Browse files Browse the repository at this point in the history
To make this code a little bit more clear we based this on the intersecting
arrays and introduced some comments.

Co-authored-by: David Kang <dkang@suse.com>
Co-authored-by: Moises Deniz Aleman <mdeniz@suse.com>
  • Loading branch information
3 people committed Feb 19, 2018
1 parent 07b488e commit 1aa2abe
Showing 1 changed file with 16 additions and 28 deletions.
44 changes: 16 additions & 28 deletions src/api/app/controllers/webui/repositories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,39 +232,27 @@ def remove_flag

# TODO: Move to model
def calculate_repo_cycle(arch)
cycles = []
# skip all packages via package=- to speed up the api call, we only parse the cycles anyway
deps = BuilddepInfo.find(project: @project.name, package: '-', repository: @repository.name, arch: arch)
if deps && deps.has_element?(:cycle)
packagecycles = {}
deps.each(:cycle) do |cycle|
package_cycle = cycle.each(:package).map(&:text)

# no cycle with another package
next if package_cycle.length <= 1

# find all existing cycles of the packages or create a new one
new_cycle = package_cycle.map do |package|
if packagecycles.key? package
packagecycles[package]
else
[package]
end
end.flatten.sort.uniq

# every package in the cycle has this cycle
new_cycle.sort.each do |package|
packagecycles[package] = new_cycle
end
end

# take only the first item of each cycle, the rest are duplicates since they are identical
packagecycles.keys.map { |p| packagecycles[p][0] }.sort.uniq.each do |key|
cycles << packagecycles[key]
cycles = deps.each(:cycle).map { |cycle| cycle.each(:package).map(&:text) }

merged_cycles = []
cycles.each do |cycle|
# We look up all other cycles that intersect with this cycle
intersecting_cycles = merged_cycles.select { |another_cycle| (cycle & another_cycle).any? }
intersecting_cycles.each do |intersecting_cycle|
# We remove the intersecting cycle from the merged cycles
deleted = merged_cycles.delete(intersecting_cycle)
# We merge the intersecting cycle with this cycle
cycle.concat(deleted)
end
# We remove duplicates from the cycle
cycle.uniq!
# We add it to the merged cycles
merged_cycles.push(cycle)
end

@repocycles[arch] = cycles unless cycles.empty?
@repocycles[arch] = merged_cycles unless merged_cycles.empty?
end

def find_repository_parent
Expand Down

0 comments on commit 1aa2abe

Please sign in to comment.