Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Format comments and simplify code

Christophe Bliard860c3d739104
diff --git a/app/models/work_package.rb b/app/models/work_package.rb
index 376c5a51719..852c0fee1b5 100644
--- a/app/models/work_package.rb
+++ b/app/models/work_package.rb
@@ -156,6 +156,7 @@ class WorkPackage < ApplicationRecord
# sort by id so that limited eager loading doesn't break with postgresql
order_column: "#{table_name}.id"
+ # makes virtual modal WorkPackageHierarchy available
has_closure_tree
##################### WARNING #####################
diff --git a/app/models/work_package/scheduling_rules.rb b/app/models/work_package/scheduling_rules.rb
index 9fb429d25a8..22ba045b009 100644
--- a/app/models/work_package/scheduling_rules.rb
+++ b/app/models/work_package/scheduling_rules.rb
@@ -35,18 +35,18 @@ module WorkPackage::SchedulingRules
# TODO: move into work package contract (possibly a module included into the contract)
# Calculates the minimum date that
- # will not violate the precedes relations (max(finish date, start date) + delay)
+ # will not violate the precedes relations (max(finish date, start date) + relation delay)
# of this work package or its ancestors
# e.g.
- # AP(due_date: 2017/07/24, delay: 1)-precedes-A
+ # AP(due_date: 2017/07/24)-precedes(delay: 1)-A
# |
# parent
# |
- # BP(due_date: 2017/07/22, delay: 2)-precedes-B
+ # BP(due_date: 2017/07/22)-precedes(delay: 2)-B
# |
# parent
# |
- # CP(due_date: 2017/07/25, delay: 2)-precedes-C
+ # CP(due_date: 2017/07/25)-precedes(delay: 2)-C
#
# Then soonest_start for:
# C is 2017/07/27
diff --git a/app/models/work_packages/scopes/for_scheduling.rb b/app/models/work_packages/scopes/for_scheduling.rb
index a6bfdb751d5..b6c20d64736 100644
--- a/app/models/work_packages/scopes/for_scheduling.rb
+++ b/app/models/work_packages/scopes/for_scheduling.rb
@@ -32,49 +32,77 @@ module WorkPackages::Scopes
extend ActiveSupport::Concern
class_methods do
- # Fetches all work packages that need to be evaluated for eventual rescheduling after a related (i.e. follows/precedes
- # and hierarchy) work package is modified or created.
- #
- # The SQL relies on a recursive CTE which will fetch all work packages that are connected to the rescheduled work package
- # via relations (follows/precedes and/or hierarchy) either directly or transitively. It will do so by increasing the
- # relation path length one at a time and will stop on that path if the work package evaluated to be added is either:
- # * itself scheduled manually
- # * having all of it's children scheduled manually
- #
- # The children themselves are scheduled manually if all of their children are scheduled manually which repeats itself down
- # to the leaf work packages. So another way of putting it, and that is how the sql statement works, is that a work package
- # is considered to be scheduled manually if *all* of its descendants are scheduled manually.
+ # Fetches all work packages that need to be evaluated for eventual
+ # rescheduling after a related (i.e. follows/precedes and hierarchy) work
+ # package is modified or created.
+ #
+ # The SQL relies on a recursive CTE which will fetch all work packages
+ # that are connected to the rescheduled work packages via relations
+ # (follows/precedes and/or hierarchy) either directly or transitively. It
+ # will do so by increasing the relation path length one at a time and will
+ # stop on that path if the work package evaluated to be added is either:
+ #
+ # * itself scheduled manually
+ # * having all of it's children scheduled manually
+ #
+ # The children themselves are scheduled manually if all of their children
+ # are scheduled manually which repeats itself down to the leaf work
+ # packages. So another way of putting it, and that is how the sql
+ # statement works, is that a work package is considered to be scheduled
+ # manually if *all* of its descendants are scheduled manually.
+ #
# For example in case of the hierarchy:
- # A and B <- hierarchy (C is parent of both A and B) - C <- hierarchy - D
- # if A and B are both scheduled manually, C is also scheduled manually and so is D. But if only A is scheduled manually,
- # B, C and D are scheduled automatically. If only C is scheduled manually, then D is still scheduled automatically since
- # A and B are scheduled manually.
+ # A and B <- hierarchy (C is parent of both A and B) - C <- hierarchy - D
+ # * A and B are work packages
+ # * C is parent of A and B
+ # * D is parent of C
+ #
+ # * If A and B are both scheduled manually, then C is also scheduled
+ # manually and so is D.
+ # * If only A is scheduled manually, then B, C and D are scheduled
+ # automatically.
+ # * If only C is scheduled manually, then D is still scheduled
+ # automatically since A and B are scheduled automatically.
#
- # The recursion will of course also stop if no more work packages can be added.
+ # The recursion will of course also stop if no more work packages can be
+ # added.
+ #
+ # The work packages can either be connected via a follows relationship, a
+ # hierarchy relationship or a combination of both.
#
- # The work packages can either be connected via a follows relationship, a hierarchy relationship
- # or a combination of both.
# E.g. in a graph of
# A <- follows - B <- hierarchy (C is parent of B) - C <- follows D
+ # * B is a successor of A
+ # * C is parent of B
+ # * D is successor of C
+ #
+ # When considering A, D would also be subject to reschedule.
#
- # D would also be subject to reschedule.
+ # At least for hierarchical relationships, we need to follow the
+ # relationship in both directions.
#
- # At least for hierarchical relationships, we need to follow the relationship in both directions.
# E.g. in a graph of
# A <- follows - B - hierarchy (B is parent of C) -> C <- follows D
+ # * B is successor of A
+ # * B is parent of C
+ # * D is successor of C
#
- # D would also be subject to reschedule.
+ # When considering A, D would also be subject to reschedule.
#
- # That possible switch in direction means that we cannot simply get all possibly affected work packages by one
- # SQL query which the DAG implementation would have allowed us to do otherwise.
+ # That possible switch in direction means that we cannot simply get all
+ # possibly affected work packages by one SQL query which the DAG
+ # implementation would have allowed us to do otherwise.
#
- # Currently, we do not rely on DAG for increasing the path length at all. We are still employing it in the check
- # for whether all paths to the leaf have a manually scheduled work package.
+ # Currently, we do not rely on DAG for increasing the path length at all.
+ # We are still employing it in the check for whether all paths to the leaf
+ # have a manually scheduled work package.
#
- # A further improvement in performance might be reachable by also employing DAG mechanisms to increase the path length.
+ # A further improvement in performance might be reachable by also
+ # employing DAG mechanisms to increase the path length.
#
- # @param work_packages WorkPackage[] A set of work packages for which the set of related work packages that might
- # be subject to reschedule is fetched.
+ # @param work_packages WorkPackage[] A set of work packages for which the
+ # set of related work packages that might be subject to reschedule is
+ # fetched.
def for_scheduling(work_packages)
return none if work_packages.empty?
@@ -95,26 +123,37 @@ module WorkPackages::Scopes
private
- # This recursive CTE fetches all work packages that are in a direct or transitive follows and/or hierarchy
- # relationship with the provided work package.
+ # This recursive CTE fetches all work packages that are in a direct or
+ # transitive follows and/or hierarchy relationship with the provided work
+ # package.
+ #
+ # Hierarchy relationships are followed up as well as down (from and to)
+ # but follows relations are only followed from the predecessor to the
+ # successor (from_id to to_id).
#
- # Hierarchy relationships are followed up as well as down (from and to) but follows relations are only followed
- # from the predecessor to the successor (from_id to to_id).
+ # The CTE starts from the provided work packages and returns for each of
+ # them:
#
- # The CTE starts from the provided work package and for that returns:
- # * the id of the work package
- # * the information, that the starting work package is not manually scheduled.
- # Whether the starting work package is manually scheduled or in fact automatically scheduled does make no
- # difference but we need those four columns later on.
+ # * id: the id of the work package
+ # * manually: the information that the starting work package is not
+ # manually scheduled.
#
- # For each recursive step, we return all work packages that are directly related to our current set of work
- # packages by a hierarchy (up or down) or follows relationship (only successors). For each such work package
+ # Whether the starting work package is manually scheduled or in fact
+ # automatically scheduled does make no difference but we need this
+ # information later on.
+ #
+ # For each recursive step, we return all work packages that are directly
+ # related to our current set of work packages by a hierarchy (up or down)
+ # or follows relationship (only successors). For each such work package
# the statement returns:
+ #
# * id of the work package that is currently at the end of a path.
- # * the flag indicating whether the added work package is automatically or manually scheduled. This also includes
- # whether *all* of the added work package's descendants are automatically or manually scheduled.
+ # * the flag indicating whether the added work package is automatically
+ # or manually scheduled. This also includes whether *all* of the added
+ # work package's descendants are automatically or manually scheduled.
#
- # Paths whose ending work package is marked to be manually scheduled are not joined with any more.
+ # Paths whose ending work package is marked to be manually scheduled are
+ # not joined with any more.
def scheduling_paths_sql(work_packages)
values = work_packages.map do |wp|
::OpenProject::SqlSanitization
diff --git a/app/services/work_packages/schedule_dependency.rb b/app/services/work_packages/schedule_dependency.rb
index 709bf18664d..9283c8d8bad 100644
--- a/app/services/work_packages/schedule_dependency.rb
+++ b/app/services/work_packages/schedule_dependency.rb
@@ -35,15 +35,15 @@ class WorkPackages::ScheduleDependency
# Those variables are pure optimizations.
# We want to reuse the already loaded work packages as much as possible
# and we want to have them readily available as hashes.
- self.known_work_packages_by_id = (self.work_packages + following).group_by(&:id).transform_values(&:first)
+ self.known_work_packages_by_id = (self.work_packages + following).index_by(&:id)
self.known_work_packages_by_parent_id = fetch_descendants.group_by(&:parent_id)
self.dependencies = create_dependencies(following)
end
# Returns each dependency in the order necessary for scheduling:
- # * successors after predecessors
- # * ancestors after descendants
+ # * successors after predecessors
+ # * ancestors after descendants
def in_schedule_order
schedule_order = []
@@ -87,11 +87,15 @@ class WorkPackages::ScheduleDependency
dependent_work_packages.index_with { |work_package| Dependency.new(work_package, self) }
end
- # Use a mixture of work packages that are already loaded to be scheduled themselves but also load
- # all the rest of the descendants. There are two cases in which descendants are not loaded for scheduling:
- # * manual scheduling: A descendant is either scheduled manually itself or all of its descendants are scheduled manually.
- # * sibling: the descendant is not below a work package to be scheduled (e.g. one following another) but below an ancestor of
- # a schedule work package.
+ # Use a mixture of work packages that are already loaded to be scheduled
+ # themselves but also load all the rest of the descendants.
+ #
+ # There are two cases in which descendants are not loaded for scheduling:
+ # * manual scheduling: A descendant is either scheduled manually itself or
+ # all of its descendants are scheduled manually.
+ # * sibling: the descendant is not below a work package to be scheduled
+ # (e.g. one following another) but below an ancestor of a schedule work
+ # package.
def fetch_descendants
descendants = known_work_packages_by_id.values
diff --git a/app/services/work_packages/set_schedule_service.rb b/app/services/work_packages/set_schedule_service.rb
index dcdf264f5aa..dc0d0df4b2f 100644
--- a/app/services/work_packages/set_schedule_service.rb
+++ b/app/services/work_packages/set_schedule_service.rb
@@ -34,14 +34,14 @@ class WorkPackages::SetScheduleService
self.work_packages = Array(work_package)
end
- def call(attributes = %i(start_date due_date))
- altered = if (%i(parent parent_id) & attributes).any?
- schedule_by_parent
- else
- []
- end
-
- if (%i(start_date due_date parent parent_id) & attributes).any?
+ def call(changed_attributes = %i(start_date due_date))
+ altered = []
+
+ if (%i(parent parent_id) & changed_attributes).any?
+ altered += schedule_by_parent
+ end
+
+ if (%i(start_date due_date parent parent_id) & changed_attributes).any?
altered += schedule_following
end
@@ -64,16 +64,22 @@ class WorkPackages::SetScheduleService
.each { |wp| wp.start_date = wp.parent.soonest_start }
end
- # Finds all work packages that need to be rescheduled because of a rescheduling of the service's work package
- # and reschedules them.
- # The order of the rescheduling is important as successors' dates are calculated based on their predecessors' dates and
- # ancestors' dates based on their children's dates.
- # Thus, the work packages following (having a follows relation, direct or transitively) the service's work package
- # are first all loaded, and then sorted by their need to be scheduled before one another:
+ # Finds all work packages that need to be rescheduled because of a
+ # rescheduling of the service's work package and reschedules them.
+ #
+ # The order of the rescheduling is important as successors' dates are
+ # calculated based on their predecessors' dates and ancestors' dates based on
+ # their children's dates.
+ #
+ # Thus, the work packages following (having a follows relation, direct or
+ # transitively) the service's work package are first all loaded, and then
+ # sorted by their need to be scheduled before one another:
+ #
# - predecessors are scheduled before their successors
# - children/descendants are scheduled before their parents/ancestors
- # Manually scheduled work packages are not encountered at this point as they are filtered out when fetching the
- # work packages eligible for rescheduling.
+ #
+ # Manually scheduled work packages are not encountered at this point as they
+ # are filtered out when fetching the work packages eligible for rescheduling.
def schedule_following
altered = []
@@ -88,7 +94,8 @@ class WorkPackages::SetScheduleService
# Schedules work packages based on either
# - their descendants if they are parents
- # - their predecessors (or predecessors of their ancestors) if they are leaves
+ # - their predecessors (or predecessors of their ancestors) if they are
+ # leaves
def reschedule(scheduled, dependency)
if dependency.descendants.any?
reschedule_ancestor(scheduled, dependency)
@@ -97,23 +104,32 @@ class WorkPackages::SetScheduleService
end
end
- # Inherits the start/due_date from the descendants of this work package. Only parent work packages are scheduled like this.
- # start_date receives the minimum of the dates (start_date and due_date) of the descendants
- # due_date receives the maximum of the dates (start_date and due_date) of the descendants
+ # Inherits the start/due_date from the descendants of this work package.
+ #
+ # Only parent work packages are scheduled like this. start_date receives the
+ # minimum of the dates (start_date and due_date) of the descendants due_date
+ # receives the maximum of the dates (start_date and due_date) of the
+ # descendants
def reschedule_ancestor(scheduled, dependency)
set_dates(scheduled, dependency.start_date, dependency.due_date)
end
- # Calculates the dates of a work package based on its follows relations. The follows relations of
- # ancestors are considered to be equal to own follows relations as they inhibit moving a work package
- # just the same. Only leaf work packages are calculated like this.
- # * work package is moved to a later date (delta positive):
- # - all following work packages are moved by the same amount unless there is still a time buffer between work package and
- # its predecessors (predecessors can also be acquired transitively by ancestors)
- # * work package moved to an earlier date (delta negative):
- # - all following work packages are moved by the same amount unless a follows relation of the work package or one of its
- # ancestors limits moving it. Then it is moved to the earliest date possible. This limitation is propagated transtitively
- # to all following work packages.
+ # Calculates the dates of a work package based on its follows relations.
+ #
+ # The follows relations of ancestors are considered to be equal to own follows
+ # relations as they inhibit moving a work package just the same. Only leaf
+ # work packages are calculated like this.
+ #
+ # work package is moved to a later date (delta positive):
+ # - all following work packages are moved by the same amount unless there is
+ # still a time buffer between work package and its predecessors
+ # (predecessors can also be acquired transitively by ancestors)
+ #
+ # work package moved to an earlier date (delta negative):
+ # - all following work packages are moved by the same amount unless a
+ # follows relation of the work package or one of its ancestors limits
+ # moving it. Then it is moved to the earliest date possible. This
+ # limitation is propagated transitively to all following work packages.
def reschedule_by_follows(scheduled, dependency)
delta = follows_delta(dependency)
min_start_date = dependency.max_date_of_followed