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

[63191] Show % Complete sum in more cases

Christophe Bliardd0cbe8790413
diff --git a/app/models/queries/work_packages/selects/property_select.rb b/app/models/queries/work_packages/selects/property_select.rb
index a34d172626a..4b9c6e261a4 100644
--- a/app/models/queries/work_packages/selects/property_select.rb
+++ b/app/models/queries/work_packages/selects/property_select.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -116,11 +118,13 @@ class Queries::WorkPackages::Selects::PropertySelect < Queries::WorkPackages::Se
sortable: false,
summable: false
},
- done_ratio: {
+ done_ratio_for_weighted_average: {
+ if: -> { WorkPackage.work_weighted_average_mode? },
+ name: :done_ratio,
sortable: "#{WorkPackage.table_name}.done_ratio",
groupable: true,
summable: true,
- summable_select: <<~SQL.squish
+ summable_select: <<~SQL.squish,
CASE
WHEN estimated_hours IS NULL OR remaining_hours IS NULL OR estimated_hours <= 0 THEN NULL
WHEN remaining_hours <= 0 THEN 100
@@ -130,6 +134,25 @@ class Queries::WorkPackages::Selects::PropertySelect < Queries::WorkPackages::Se
ELSE ROUND( ((1 - (remaining_hours / estimated_hours)) * 100)::numeric )::integer
END as done_ratio
SQL
+ summable_work_packages_select: false
+ },
+ done_ratio_for_simple_average: {
+ if: -> { WorkPackage.simple_average_mode? },
+ name: :done_ratio,
+ sortable: "#{WorkPackage.table_name}.done_ratio",
+ groupable: true,
+ summable: true,
+ summable_select: <<~SQL.squish,
+ CASE
+ WHEN done_ratio_count = 0 THEN NULL
+ WHEN done_ratio >= done_ratio_count * 100 THEN 100
+ WHEN done_ratio >= done_ratio_count * 99 THEN 99
+ WHEN done_ratio <= 0 THEN 0
+ WHEN done_ratio <= done_ratio_count THEN 1
+ ELSE ROUND(done_ratio::numeric / done_ratio_count)::integer
+ END as done_ratio
+ SQL
+ summable_work_packages_count_select: true
},
created_at: {
sortable: "#{WorkPackage.table_name}.created_at",
@@ -145,8 +168,13 @@ class Queries::WorkPackages::Selects::PropertySelect < Queries::WorkPackages::Se
}
def self.instances(_context = nil)
- property_selects.filter_map do |name, options|
- new(name, options)
+ active_selects = property_selects.reject do |_, options|
+ condition = options[:if]
+ condition && !condition.call
+ end
+ active_selects.filter_map do |default_name, options|
+ name = options[:name] || default_name
+ new(name, options.without(:if, :name))
end
end
end
diff --git a/app/models/queries/work_packages/selects/work_package_select.rb b/app/models/queries/work_packages/selects/work_package_select.rb
index 56f1a288580..54ed6ed9099 100644
--- a/app/models/queries/work_packages/selects/work_package_select.rb
+++ b/app/models/queries/work_packages/selects/work_package_select.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -38,6 +40,7 @@ class Queries::WorkPackages::Selects::WorkPackageSelect
:association,
:null_handling,
:summable_select,
+ :summable_work_packages_count_select,
:summable_work_packages_select
def self.instances(_context = nil)
@@ -74,7 +77,7 @@ class Queries::WorkPackages::Selects::WorkPackageSelect
end
def displayable
- @displayable.nil? ? true : @displayable
+ @displayable.nil? || @displayable
end
def sortable
@@ -128,6 +131,7 @@ class Queries::WorkPackages::Selects::WorkPackageSelect
groupable_join
summable
summable_select
+ summable_work_packages_count_select
summable_work_packages_select
association
group_by_column_name
diff --git a/app/models/query/results/sums.rb b/app/models/query/results/sums.rb
index 9cdbf1494fc..5702e61d82e 100644
--- a/app/models/query/results/sums.rb
+++ b/app/models/query/results/sums.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -117,13 +119,19 @@ module ::Query::Results::Sums
[]
end
- group_statement + summed_columns
+ group_statement + summed_columns + counted_summed_columns
end
def summed_columns
query.summed_up_columns.filter_map(&:summable_work_packages_select).map { |c| "SUM(#{c}) #{c}" }
end
+ def counted_summed_columns
+ query.summed_up_columns
+ .select(&:summable_work_packages_count_select)
+ .map { |col| "COUNT(#{col.name}) #{col.name}_count" }
+ end
+
def callable_summed_up_columns
query.summed_up_columns.select { |column| column.summable.respond_to?(:call) }
end
diff --git a/spec/models/query/results_sums_integration_spec.rb b/spec/models/query/results_sums_integration_spec.rb
index 70137089596..b1c7c652c32 100644
--- a/spec/models/query/results_sums_integration_spec.rb
+++ b/spec/models/query/results_sums_integration_spec.rb
@@ -120,6 +120,11 @@ RSpec.describe Query::Results, "sums" do
float_cf.attribute_name => 3.414,
story_points: 7)
end
+ shared_let(:work_package_with_done_ratio_only) do
+ create(:work_package,
+ type:,
+ done_ratio: 100)
+ end
shared_let(:invisible_work_package1) do
create(:work_package,
type:,
@@ -153,13 +158,13 @@ RSpec.describe Query::Results, "sums" do
it "is a hash of all summable columns" do
expect(stringify_column_keys(query_results.all_total_sums))
.to eq("estimated_hours" => 20.0,
+ "remaining_hours" => 14.0,
+ "done_ratio" => 30,
int_cf.column_name => 30,
float_cf.column_name => 10.24,
"material_costs" => 400.0,
"labor_costs" => 600.0,
"overall_costs" => 1000.0,
- "remaining_hours" => 14.0,
- "done_ratio" => 30,
"story_points" => 21)
end
@@ -171,16 +176,154 @@ RSpec.describe Query::Results, "sums" do
it "is a hash of all summable columns and includes only the work packages matching the filter" do
expect(stringify_column_keys(query_results.all_total_sums))
.to eq("estimated_hours" => 10.0,
+ "remaining_hours" => 5.0,
+ "done_ratio" => 50,
int_cf.column_name => 20,
float_cf.column_name => 6.83,
"material_costs" => 200.0,
"labor_costs" => 300.0,
"overall_costs" => 500.0,
- "remaining_hours" => 5.0,
- "done_ratio" => 50,
"story_points" => 14)
end
end
+
+ describe "% complete sum" do
+ subject(:percent_complete_sum) { stringify_column_keys(query_results.all_total_sums)["done_ratio"] }
+
+ context "when using work weighted average mode",
+ with_settings: { total_percent_complete_mode: "work_weighted_average" } do
+ it "is computed from work and remaining work sums " \
+ "and ignores work packages having only % complete set" do
+ # work sum: 10 + 5 + 5 = 20
+ # remaining work sum: 9 + 2.5 + 2.5 = 14
+ # % complete sum: 100 * (20 - 14) / 20 = 30
+ expect(percent_complete_sum).to eq(30)
+ end
+
+ context "when work and remaining work are 0h" do
+ before do
+ WorkPackage.update_all(estimated_hours: 0, remaining_hours: 0, done_ratio: nil)
+ end
+
+ it "is unset" do
+ expect(percent_complete_sum).to be_nil
+ end
+ end
+
+ context "when work sum is more than 0h and remaining work sum is 0h" do
+ before do
+ WorkPackage.update_all(estimated_hours: 5, remaining_hours: 0, done_ratio: 100)
+ end
+
+ it "is 100%" do
+ expect(percent_complete_sum).to eq(100)
+ end
+ end
+
+ context "when work sum is equal to remaining work sum" do
+ before do
+ WorkPackage.update_all(estimated_hours: 5, remaining_hours: 5, done_ratio: 0)
+ end
+
+ it "is 0%" do
+ expect(percent_complete_sum).to eq(0)
+ end
+ end
+
+ context "when % complete sum is not exactly 100% (like 99.9%)" do
+ before do
+ WorkPackage.update_all(estimated_hours: 1000, remaining_hours: 1, done_ratio: 99)
+ end
+
+ it "is 99% because it's not strictly 100%" do
+ expect(percent_complete_sum).to eq(99)
+ end
+ end
+
+ context "when % complete sum is not exactly 0% (like 0.1%)" do
+ before do
+ WorkPackage.update_all(estimated_hours: 1000, remaining_hours: 999, done_ratio: 1)
+ end
+
+ it "is 1% because it's not strictly 0%" do
+ expect(percent_complete_sum).to eq(1)
+ end
+ end
+ end
+
+ context "when using simple average mode",
+ with_settings: { total_percent_complete_mode: "simple_average" } do
+ it "is computed from % complete values of work packages having % complete set " \
+ "and ignores work and remaining work values" do
+ # % complete sum: (10 + 50 + 50 + 100) / 4.0 = 52.5 => 53% rounded
+ expect(percent_complete_sum).to eq(53)
+ end
+
+ context "when none have % complete set" do
+ before do
+ WorkPackage.update_all(estimated_hours: 0, remaining_hours: 0, done_ratio: nil)
+ end
+
+ it "is unset" do
+ expect(percent_complete_sum).to be_nil
+ end
+ end
+
+ context "when all % complete are 100%" do
+ before do
+ WorkPackage.update_all(estimated_hours: 5, remaining_hours: 0, done_ratio: 100)
+ end
+
+ it "is 100%" do
+ expect(percent_complete_sum).to eq(100)
+ end
+ end
+
+ context "when all % complete are 0%" do
+ before do
+ WorkPackage.update_all(estimated_hours: 5, remaining_hours: 5, done_ratio: 0)
+ end
+
+ it "is 0%" do
+ expect(percent_complete_sum).to eq(0)
+ end
+ end
+
+ context "when % complete average is not exactly 100% (like 99.9%)" do
+ before do
+ WorkPackage.update_all(estimated_hours: 1000, remaining_hours: 0, done_ratio: 100)
+ WorkPackage.first.update(estimated_hours: 1000, remaining_hours: 1, done_ratio: 99)
+ end
+
+ it "is 99% because it's not strictly 100%" do
+ expect(percent_complete_sum).to eq(99)
+ end
+ end
+
+ context "when % complete sum is not exactly 0% (like 0.1%)" do
+ before do
+ WorkPackage.update_all(estimated_hours: 1000, remaining_hours: 1000, done_ratio: 0)
+ WorkPackage.first.update(estimated_hours: 1000, remaining_hours: 999, done_ratio: 1)
+ end
+
+ it "is 1% because it's not strictly 0%" do
+ expect(percent_complete_sum).to eq(1)
+ end
+ end
+
+ context "when % complete sum decimal part is above 0.5 (like 2.6%)" do
+ before do
+ # 4x3% and 1x1% => 13 / 5 = 2.6%
+ WorkPackage.update_all(estimated_hours: 100, remaining_hours: 97, done_ratio: 3)
+ WorkPackage.first.update(estimated_hours: 100, remaining_hours: 99, done_ratio: 1)
+ end
+
+ it "is rounded up (to 3%)" do
+ expect(percent_complete_sum).to eq(3)
+ end
+ end
+ end
+ end
end
describe "#all_sums_for_group" do
@@ -191,23 +334,23 @@ RSpec.describe Query::Results, "sums" do
expect(query_results.all_group_sums.keys).to contain_exactly(current_user, nil)
expect(stringify_column_keys(query_results.all_group_sums[current_user]))
.to eq("estimated_hours" => 10.0,
+ "remaining_hours" => 5.0,
+ "done_ratio" => 50,
int_cf.column_name => 20,
float_cf.column_name => 6.83,
"material_costs" => 200.0,
"labor_costs" => 300.0,
"overall_costs" => 500.0,
- "remaining_hours" => 5.0,
- "done_ratio" => 50,
"story_points" => 14)
expect(stringify_column_keys(query_results.all_group_sums[nil]))
.to eq("estimated_hours" => 10.0,
+ "remaining_hours" => 9.0,
+ "done_ratio" => 10,
int_cf.column_name => 10,
float_cf.column_name => 3.41,
"material_costs" => 200.0,
"labor_costs" => 300.0,
"overall_costs" => 500.0,
- "remaining_hours" => 9.0,
- "done_ratio" => 10,
"story_points" => 7)
end
@@ -220,13 +363,13 @@ RSpec.describe Query::Results, "sums" do
expect(query_results.all_group_sums.keys).to contain_exactly(current_user)
expect(stringify_column_keys(query_results.all_group_sums[current_user]))
.to eq("estimated_hours" => 5.0,
+ "remaining_hours" => 2.5,
+ "done_ratio" => 50,
int_cf.column_name => 10,
float_cf.column_name => 3.41,
"material_costs" => 0.0,
"labor_costs" => 0.0,
"overall_costs" => 0.0,
- "remaining_hours" => 2.5,
- "done_ratio" => 50,
"story_points" => 7)
end
end
@@ -236,26 +379,36 @@ RSpec.describe Query::Results, "sums" do
let(:group_by) { :done_ratio }
it "is a hash of sums grouped by done_ratio values and grouped columns" do
- expect(query_results.all_group_sums.keys).to contain_exactly(50, 10)
+ expect(query_results.all_group_sums.keys).to contain_exactly(100, 50, 10)
+ expect(stringify_column_keys(query_results.all_group_sums[100]))
+ .to eq("estimated_hours" => nil,
+ "remaining_hours" => nil,
+ "done_ratio" => nil,
+ int_cf.column_name => nil,
+ float_cf.column_name => nil,
+ "material_costs" => 0.0,
+ "labor_costs" => 0.0,
+ "overall_costs" => 0.0,
+ "story_points" => nil)
expect(stringify_column_keys(query_results.all_group_sums[50]))
.to eq("estimated_hours" => 10.0,
+ "remaining_hours" => 5.0,
+ "done_ratio" => 50,
int_cf.column_name => 20,
float_cf.column_name => 6.83,
"material_costs" => 200.0,
"labor_costs" => 300.0,
"overall_costs" => 500.0,
- "remaining_hours" => 5.0,
- "done_ratio" => 50,
"story_points" => 14)
expect(stringify_column_keys(query_results.all_group_sums[10]))
.to eq("estimated_hours" => 10.0,
+ "remaining_hours" => 9.0,
+ "done_ratio" => 10,
int_cf.column_name => 10,
float_cf.column_name => 3.41,
"material_costs" => 200.0,
"labor_costs" => 300.0,
"overall_costs" => 500.0,
- "remaining_hours" => 9.0,
- "done_ratio" => 10,
"story_points" => 7)
end
@@ -268,13 +421,13 @@ RSpec.describe Query::Results, "sums" do
expect(query_results.all_group_sums.keys).to contain_exactly(50)
expect(stringify_column_keys(query_results.all_group_sums[50]))
.to eq("estimated_hours" => 5.0,
+ "remaining_hours" => 2.5,
+ "done_ratio" => 50,
int_cf.column_name => 10,
float_cf.column_name => 3.41,
"material_costs" => 0.0,
"labor_costs" => 0.0,
"overall_costs" => 0.0,
- "remaining_hours" => 2.5,
- "done_ratio" => 50,
"story_points" => 7)
end
end