Commit e132a222 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch '229046-iterations-charts-refactoring' into 'master'

Refactor BurnupChartService to handle iterations

See merge request gitlab-org/gitlab!41280
parents 9714640d ea25271f
......@@ -195,6 +195,10 @@ module Timebox
end
end
def weight_available?
resource_parent&.feature_available?(:issue_weights)
end
private
def timebox_format_reference(format = :iid)
......
---
title: Add index to resource_iteration_events for add actions
merge_request: 41280
author:
type: other
# frozen_string_literal: true
class AddIndexToResourceIterationEventsAddEvents < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
INDEX_NAME = 'index_resource_iteration_events_on_iteration_id_and_add_action'
ADD_ACTION = '1'
def up
# Index add iteration events
add_concurrent_index :resource_iteration_events, :iteration_id, where: "action = #{ADD_ACTION}", name: INDEX_NAME
end
def down
remove_concurrent_index :resource_iteration_events, :iteration_id, name: INDEX_NAME
end
end
e6c3d5352feed1adc82b14218a6f47fa55df9e0add8a59228d128e4e7f39614b
\ No newline at end of file
......@@ -20817,6 +20817,8 @@ CREATE INDEX index_resource_iteration_events_on_merge_request_id ON public.resou
CREATE INDEX index_resource_iteration_events_on_user_id ON public.resource_iteration_events USING btree (user_id);
CREATE INDEX index_resource_iterationn_events_on_iteration_id_and_add_action ON public.resource_iteration_events USING btree (iteration_id) WHERE (action = 1);
CREATE INDEX index_resource_label_events_issue_id_label_id_action ON public.resource_label_events USING btree (issue_id, label_id, action);
CREATE INDEX index_resource_label_events_on_epic_id ON public.resource_label_events USING btree (epic_id);
......
......@@ -9,7 +9,7 @@ module Resolvers
def resolve(*args)
return [] unless milestone.burnup_charts_available?
response = Milestones::BurnupChartService.new(milestone).execute
response = TimeboxBurnupChartService.new(milestone).execute
raise GraphQL::ExecutionError, response.message if response.error?
......
......@@ -42,6 +42,14 @@ module EE
self.title
end
def supports_timebox_charts?
resource_parent&.feature_available?(:iterations) && weight_available?
end
def burnup_charts_available?
::Feature.enabled?(:iteration_charts, resource_parent)
end
private
def timebox_format_reference(format = :id)
......
......@@ -10,14 +10,12 @@ module EE
has_many :boards
end
def weight_available?
resource_parent&.feature_available?(:issue_weights)
end
def supports_milestone_charts?
resource_parent&.feature_available?(:milestone_charts) && weight_available?
end
alias_method :supports_timebox_charts?, :supports_milestone_charts?
def burnup_charts_available?
::Feature.enabled?(:burnup_charts, resource_parent)
end
......
# frozen_string_literal: true
# This service computes the milestone's daily total number of issues and their weights.
# For each day, this returns the totals for all issues that are assigned to the milestone at that point in time.
# This represents the scope for this milestone. This also returns separate totals for closed issues which represent the completed work.
# This service computes the timebox's(milestone, iteration) daily total number of issues and their weights.
# For each day, this returns the totals for all issues that are assigned to the timebox(milestone, iteration) at that point in time.
# This represents the scope for this timebox(milestone, iteration). This also returns separate totals for closed issues which represent the completed work.
#
# This is implemented by iterating over all relevant resource events ordered by time. We need to do this
# so that we can keep track of the issue's state during that point in time and handle the events based on that.
class Milestones::BurnupChartService
class TimeboxBurnupChartService
include Gitlab::Utils::StrongMemoize
EVENT_COUNT_LIMIT = 50_000
def initialize(milestone)
@milestone = milestone
def initialize(timebox)
@timebox = timebox
end
def execute
return ServiceResponse.error(message: _('Milestone does not support burnup charts')) unless milestone.supports_milestone_charts?
return ServiceResponse.error(message: _('Milestone must have a start and due date')) if milestone.start_date.blank? || milestone.due_date.blank?
return ServiceResponse.error(message: _('%{timebox_type} does not support burnup charts' % { timebox_type: timebox_type })) unless timebox.supports_timebox_charts?
return ServiceResponse.error(message: _('%{timebox_type} must have a start and due date' % { timebox_type: timebox_type })) if timebox.start_date.blank? || timebox.due_date.blank?
return ServiceResponse.error(message: _('Burnup chart could not be generated due to too many events')) if resource_events.num_tuples > EVENT_COUNT_LIMIT
@issue_states = {}
......@@ -26,8 +26,8 @@ class Milestones::BurnupChartService
resource_events.each do |event|
case event['event_type']
when 'milestone'
handle_milestone_event(event)
when 'timebox'
handle_resource_timebox_event(event)
when 'state'
handle_state_event(event)
when 'weight'
......@@ -40,25 +40,25 @@ class Milestones::BurnupChartService
private
attr_reader :milestone, :issue_states, :chart_data
attr_reader :timebox, :issue_states, :chart_data
def handle_milestone_event(event)
def handle_resource_timebox_event(event)
issue_state = find_issue_state(event['issue_id'])
return if issue_state[:milestone] == milestone.id && event['action'] == ResourceMilestoneEvent.actions[:add] && event['value'] == milestone.id
return if issue_state[:timebox] == timebox.id && event['action'] == ResourceTimeboxEvent.actions[:add] && event['value'] == timebox.id
if event['action'] == ResourceMilestoneEvent.actions[:add] && event['value'] == milestone.id
handle_add_milestone_event(event)
elsif issue_state[:milestone] == milestone.id
# If the issue is currently assigned to the milestone, then treat any event here as a removal.
# We do not have a separate `:remove` event when replacing milestones with another one.
handle_remove_milestone_event(event)
if event['action'] == ResourceTimeboxEvent.actions[:add] && event['value'] == timebox.id
handle_add_timebox_event(event)
elsif issue_state[:timebox] == timebox.id
# If the issue is currently assigned to the timebox(milestone or iteration), then treat any event here as a removal.
# We do not have a separate `:remove` event when replacing timebox(milestone or iteration) with another one.
handle_remove_timebox_event(event)
end
issue_state[:milestone] = event['value']
issue_state[:timebox] = event['value']
end
def handle_add_milestone_event(event)
def handle_add_timebox_event(event)
issue_state = find_issue_state(event['issue_id'])
increment_scope(event['created_at'], issue_state[:weight])
......@@ -68,7 +68,7 @@ class Milestones::BurnupChartService
end
end
def handle_remove_milestone_event(event)
def handle_remove_timebox_event(event)
issue_state = find_issue_state(event['issue_id'])
decrement_scope(event['created_at'], issue_state[:weight])
......@@ -83,7 +83,7 @@ class Milestones::BurnupChartService
old_state = issue_state[:state]
issue_state[:state] = event['value']
return if issue_state[:milestone] != milestone.id
return if issue_state[:timebox] != timebox.id
if old_state == ResourceStateEvent.states[:closed] && event['value'] == ResourceStateEvent.states[:reopened]
decrement_completed(event['created_at'], issue_state[:weight])
......@@ -97,7 +97,7 @@ class Milestones::BurnupChartService
old_weight = issue_state[:weight]
issue_state[:weight] = event['value'] || 0
return if issue_state[:milestone] != milestone.id
return if issue_state[:timebox] != timebox.id
add_chart_data(event['created_at'], :scope_weight, issue_state[:weight] - old_weight)
......@@ -128,7 +128,7 @@ class Milestones::BurnupChartService
def add_chart_data(timestamp, field, value)
date = timestamp.to_date
date = milestone.start_date if date < milestone.start_date
date = timebox.start_date if date < timebox.start_date
if chart_data.empty?
chart_data.push({
......@@ -150,7 +150,7 @@ class Milestones::BurnupChartService
def find_issue_state(issue_id)
issue_states[issue_id] ||= {
milestone: nil,
timebox: nil,
weight: 0,
state: ResourceStateEvent.states[:opened]
}
......@@ -159,16 +159,16 @@ class Milestones::BurnupChartService
# rubocop: disable CodeReuse/ActiveRecord
def resource_events
strong_memoize(:resource_events) do
union = Gitlab::SQL::Union.new([milestone_events, state_events, weight_events]) # rubocop: disable Gitlab/Union
union = Gitlab::SQL::Union.new([resource_timebox_events, state_events, weight_events]) # rubocop: disable Gitlab/Union
ActiveRecord::Base.connection.execute("(#{union.to_sql}) ORDER BY created_at LIMIT #{EVENT_COUNT_LIMIT + 1}")
end
end
# rubocop: enable CodeReuse/ActiveRecord
def milestone_events
ResourceMilestoneEvent.by_issue_ids_and_created_at_earlier_or_equal_to(issue_ids, end_time)
.select('\'milestone\' AS event_type, created_at, milestone_id AS value, action, issue_id')
def resource_timebox_events
resource_timebox_event_class.by_issue_ids_and_created_at_earlier_or_equal_to(issue_ids, end_time)
.select("'timebox' AS event_type, created_at, #{timebox_fk} AS value, action, issue_id")
end
def state_events
......@@ -186,10 +186,10 @@ class Milestones::BurnupChartService
# We find all issues that have this milestone added before this milestone's due date.
# We cannot just filter by `issues.milestone_id` because there might be issues that have
# since been moved to other milestones and we still need to include these in this graph.
ResourceMilestoneEvent
resource_timebox_event_class
.select(:issue_id)
.where({
milestone_id: milestone.id,
"#{timebox_fk}": timebox.id,
action: :add
})
.where('created_at <= ?', end_time)
......@@ -197,6 +197,23 @@ class Milestones::BurnupChartService
# rubocop: enable CodeReuse/ActiveRecord
def end_time
@end_time ||= milestone.due_date.end_of_day
@end_time ||= timebox.due_date.end_of_day
end
def timebox_type
timebox.class.name
end
def timebox_fk
timebox_type.downcase.foreign_key
end
def resource_timebox_event_class
case timebox
when Milestone then ResourceMilestoneEvent
when Iteration then ResourceIterationEvent
else
raise ArgumentError, 'Cannot handle timebox type'
end
end
end
......@@ -56,7 +56,7 @@ RSpec.describe Resolvers::MilestoneBurnupTimeSeriesResolver do
context 'when the service returns an error' do
before do
stub_const('Milestones::BurnupChartService::EVENT_COUNT_LIMIT', 1)
stub_const('TimeboxBurnupChartService::EVENT_COUNT_LIMIT', 1)
end
it 'raises a GraphQL exception' do
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Milestones::BurnupChartService do
let_it_be(:project) { create(:project) }
let_it_be(:milestone, reload: true) { create(:milestone, project: project, start_date: '2020-01-01', due_date: '2020-01-15') }
let_it_be(:issues) { create_list(:issue, 5, project: project) }
let(:response) { described_class.new(milestone).execute }
context 'when license is not available' do
before do
stub_licensed_features(milestone_charts: false)
end
it 'returns an error message' do
expect(response.error?).to eq(true)
expect(response.message).to eq('Milestone does not support burnup charts')
end
end
context 'when license is available' do
before do
stub_licensed_features(milestone_charts: true, issue_weights: true)
end
context 'when milestone does not have a start and due date' do
let(:milestone) { build(:milestone, project: project) }
it 'returns an error message' do
expect(response.error?).to eq(true)
expect(response.message).to eq('Milestone must have a start and due date')
end
end
it 'returns an error when the number of events exceeds the limit' do
stub_const('Milestones::BurnupChartService::EVENT_COUNT_LIMIT', 1)
create(:resource_milestone_event, issue: issues[0], milestone: milestone, action: :add, created_at: '2019-12-15')
create(:resource_milestone_event, issue: issues[1], milestone: milestone, action: :add, created_at: '2019-12-16')
expect(response.error?).to eq(true)
expect(response.message).to eq('Burnup chart could not be generated due to too many events')
end
it 'aggregates events before the start date to the start date' do
create(:resource_milestone_event, issue: issues[0], milestone: milestone, action: :add, created_at: '2019-12-15')
create(:resource_weight_event, issue: issues[0], weight: 2, created_at: '2019-12-18')
create(:resource_milestone_event, issue: issues[1], milestone: milestone, action: :add, created_at: '2019-12-16')
create(:resource_weight_event, issue: issues[1], weight: 1, created_at: '2019-12-18')
create(:resource_milestone_event, issue: issues[2], milestone: milestone, action: :add, created_at: '2019-12-16')
create(:resource_weight_event, issue: issues[2], weight: 3, created_at: '2019-12-18')
create(:resource_state_event, issue: issues[2], state: :closed, created_at: '2019-12-25')
create(:resource_milestone_event, issue: issues[3], milestone: milestone, action: :add, created_at: '2019-12-17')
create(:resource_weight_event, issue: issues[3], weight: 4, created_at: '2019-12-18')
create(:resource_state_event, issue: issues[3], state: :closed, created_at: '2019-12-26')
expect(response.success?).to eq(true)
expect(response.payload).to eq([
{
date: Date.parse('2020-01-01'),
scope_count: 4,
scope_weight: 10,
completed_count: 2,
completed_weight: 7
}
])
end
it 'updates counts and weight when the milestone is added or removed' do
# Add milestone to an open issue with no weight.
create(:resource_milestone_event, issue: issues[0], milestone: milestone, action: :add, created_at: '2020-01-05 03:00')
# Ignore duplicate add event.
create(:resource_milestone_event, issue: issues[0], milestone: milestone, action: :add, created_at: '2020-01-05 03:00')
# Add milestone to an open issue with weight 2 on the same day. This should increment the scope totals for the same day.
create(:resource_weight_event, issue: issues[1], weight: 2, created_at: '2020-01-01')
create(:resource_milestone_event, issue: issues[1], milestone: milestone, action: :add, created_at: '2020-01-05 05:00')
# Add milestone to already closed issue with weight 3. This should increment both the scope and completed totals.
create(:resource_weight_event, issue: issues[2], weight: 3, created_at: '2020-01-01')
create(:resource_state_event, issue: issues[2], state: :closed, created_at: '2020-01-05')
create(:resource_milestone_event, issue: issues[2], milestone: milestone, action: :add, created_at: '2020-01-06')
# Remove milestone from the 2nd open issue. This should decrement the scope totals.
create(:resource_milestone_event, issue: issues[1], milestone: milestone, action: :remove, created_at: '2020-01-07')
# Remove milestone from the closed issue. This should decrement both the scope and completed totals.
create(:resource_milestone_event, issue: issues[2], milestone: milestone, action: :remove, created_at: '2020-01-08')
# Adding a different milestone should not affect the data.
create(:resource_milestone_event, issue: issues[3], milestone: create(:milestone, project: project), action: :add, created_at: '2020-01-08')
# Adding the milestone after the due date should not affect the data.
create(:resource_milestone_event, issue: issues[4], milestone: milestone, action: :add, created_at: '2020-01-30')
# Removing the milestone after the due date should not affect the data.
create(:resource_milestone_event, issue: issues[0], milestone: milestone, action: :remove, created_at: '2020-01-30')
expect(response.success?).to eq(true)
expect(response.payload).to eq([
{
date: Date.parse('2020-01-05'),
scope_count: 2,
scope_weight: 2,
completed_count: 0,
completed_weight: 0
},
{
date: Date.parse('2020-01-06'),
scope_count: 3,
scope_weight: 5,
completed_count: 1,
completed_weight: 3
},
{
date: Date.parse('2020-01-07'),
scope_count: 2,
scope_weight: 3,
completed_count: 1,
completed_weight: 3
},
{
date: Date.parse('2020-01-08'),
scope_count: 1,
scope_weight: 0,
completed_count: 0,
completed_weight: 0
}
])
end
it 'updates the completed counts when issue state is changed' do
# Close an issue assigned to the milestone with weight 2. This should increment the completed totals.
create(:resource_milestone_event, issue: issues[0], milestone: milestone, action: :add, created_at: '2020-01-01 01:00')
create(:resource_weight_event, issue: issues[0], weight: 2, created_at: '2020-01-01 02:00')
create(:resource_state_event, issue: issues[0], state: :closed, created_at: '2020-01-02')
# Closing an issue that is already closed should be ignored.
create(:resource_state_event, issue: issues[0], state: :closed, created_at: '2020-01-03')
# Re-opening the issue should decrement the completed totals.
create(:resource_state_event, issue: issues[0], state: :reopened, created_at: '2020-01-04')
# Closing and re-opening an issue on the same day should not change the totals.
create(:resource_milestone_event, issue: issues[1], milestone: milestone, action: :add, created_at: '2020-01-05 01:00')
create(:resource_weight_event, issue: issues[1], weight: 3, created_at: '2020-01-05 02:00')
create(:resource_state_event, issue: issues[1], state: :closed, created_at: '2020-01-06 05:00')
create(:resource_state_event, issue: issues[1], state: :reopened, created_at: '2020-01-06 08:00')
# Re-opening an issue that is already open should be ignored.
create(:resource_state_event, issue: issues[1], state: :reopened, created_at: '2020-01-07')
# Closing a re-opened issue should increment the completed totals.
create(:resource_state_event, issue: issues[1], state: :closed, created_at: '2020-01-08')
# Changing state when the milestone is already removed should not affect the data.
create(:resource_milestone_event, issue: issues[1], action: :remove, created_at: '2020-01-09')
create(:resource_state_event, issue: issues[1], state: :closed, created_at: '2020-01-10')
expect(response.success?).to eq(true)
expect(response.payload).to eq([
{
date: Date.parse('2020-01-01'),
scope_count: 1,
scope_weight: 2,
completed_count: 0,
completed_weight: 0
},
{
date: Date.parse('2020-01-02'),
scope_count: 1,
scope_weight: 2,
completed_count: 1,
completed_weight: 2
},
{
date: Date.parse('2020-01-04'),
scope_count: 1,
scope_weight: 2,
completed_count: 0,
completed_weight: 0
},
{
date: Date.parse('2020-01-05'),
scope_count: 2,
scope_weight: 5,
completed_count: 0,
completed_weight: 0
},
{
date: Date.parse('2020-01-06'),
scope_count: 2,
scope_weight: 5,
completed_count: 0,
completed_weight: 0
},
{
date: Date.parse('2020-01-08'),
scope_count: 2,
scope_weight: 5,
completed_count: 1,
completed_weight: 3
},
{
date: Date.parse('2020-01-09'),
scope_count: 1,
scope_weight: 2,
completed_count: 0,
completed_weight: 0
}
])
end
it 'updates the weight totals when issue weight is changed' do
# Issue starts out with no weight and should increment once the weight is changed to 2.
create(:resource_milestone_event, issue: issues[0], milestone: milestone, action: :add, created_at: '2020-01-01')
create(:resource_weight_event, issue: issues[0], weight: 2, created_at: '2020-01-02')
# A closed issue is added and weight is set to 5 and should add to the weight totals.
create(:resource_milestone_event, issue: issues[1], milestone: milestone, action: :add, created_at: '2020-01-03 01:00')
create(:resource_state_event, issue: issues[1], state: :closed, created_at: '2020-01-03 02:00')
create(:resource_weight_event, issue: issues[1], weight: 5, created_at: '2020-01-03 03:00')
# Lowering the weight of the 2nd issue should decrement the weight totals.
create(:resource_weight_event, issue: issues[1], weight: 1, created_at: '2020-01-04')
# After the first issue is assigned to another milestone, weight changes shouldn't affect the data.
create(:resource_milestone_event, issue: issues[0], milestone: create(:milestone, project: project), action: :add, created_at: '2020-01-05')
create(:resource_weight_event, issue: issues[0], weight: 10, created_at: '2020-01-06')
expect(response.success?).to eq(true)
expect(response.payload).to eq([
{
date: Date.parse('2020-01-01'),
scope_count: 1,
scope_weight: 0,
completed_count: 0,
completed_weight: 0
},
{
date: Date.parse('2020-01-02'),
scope_count: 1,
scope_weight: 2,
completed_count: 0,
completed_weight: 0
},
{
date: Date.parse('2020-01-03'),
scope_count: 2,
scope_weight: 7,
completed_count: 1,
completed_weight: 5
},
{
date: Date.parse('2020-01-04'),
scope_count: 2,
scope_weight: 3,
completed_count: 1,
completed_weight: 1
},
{
date: Date.parse('2020-01-05'),
scope_count: 1,
scope_weight: 1,
completed_count: 1,
completed_weight: 1
}
])
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.shared_examples 'timebox chart' do |timebox_type|
let_it_be(:issues) { create_list(:issue, 5, project: project) }
context 'when license is not available' do
before do
stub_licensed_features(milestone_charts: false, iterations: false)
end
it 'returns an error message' do
expect(response.error?).to eq(true)
expect(response.message).to eq("#{timebox_type.capitalize} does not support burnup charts")
end
end
context 'when license is available' do
before do
stub_licensed_features(milestone_charts: true, issue_weights: true, iterations: true)
end
context 'when milestone does not have a start and due date' do
let(:timebox) { timebox_without_dates }
it 'returns an error message' do
expect(response.error?).to eq(true)
expect(response.message).to eq("#{timebox_type.capitalize} must have a start and due date")
end
end
it 'returns an error when the number of events exceeds the limit' do
stub_const('TimeboxBurnupChartService::EVENT_COUNT_LIMIT', 1)
create(:"resource_#{timebox_type}_event", issue: issues[0], "#{timebox_type}" => timebox, action: :add, created_at: timebox_start_date - 21.days)
create(:"resource_#{timebox_type}_event", issue: issues[1], "#{timebox_type}" => timebox, action: :add, created_at: timebox_start_date - 20.days)
expect(response.error?).to eq(true)
expect(response.message).to eq('Burnup chart could not be generated due to too many events')
end
it 'aggregates events before the start date to the start date' do
create(:"resource_#{timebox_type}_event", issue: issues[0], "#{timebox_type}" => timebox, action: :add, created_at: timebox_start_date - 21.days)
create(:resource_weight_event, issue: issues[0], weight: 2, created_at: timebox_start_date - 14.days)
create(:"resource_#{timebox_type}_event", issue: issues[1], "#{timebox_type}" => timebox, action: :add, created_at: timebox_start_date - 20.days)
create(:resource_weight_event, issue: issues[1], weight: 1, created_at: timebox_start_date - 14.days)
create(:"resource_#{timebox_type}_event", issue: issues[2], "#{timebox_type}" => timebox, action: :add, created_at: timebox_start_date - 20.days)
create(:resource_weight_event, issue: issues[2], weight: 3, created_at: timebox_start_date - 14.days)
create(:resource_state_event, issue: issues[2], state: :closed, created_at: timebox_start_date - 7.days)
create(:"resource_#{timebox_type}_event", issue: issues[3], "#{timebox_type}" => timebox, action: :add, created_at: timebox_start_date - 19.days)
create(:resource_weight_event, issue: issues[3], weight: 4, created_at: timebox_start_date - 14.days)
create(:resource_state_event, issue: issues[3], state: :closed, created_at: timebox_start_date - 6.days)
expect(response.success?).to eq(true)
expect(response.payload).to eq([
{
date: timebox_start_date,
scope_count: 4,
scope_weight: 10,
completed_count: 2,
completed_weight: 7
}
])
end
it 'updates counts and weight when the milestone is added or removed' do
# Add milestone to an open issue with no weight.
create(:"resource_#{timebox_type}_event", issue: issues[0], "#{timebox_type}" => timebox, action: :add, created_at: timebox_start_date + 4.days + 3.hours)
# Ignore duplicate add event.
create(:"resource_#{timebox_type}_event", issue: issues[0], "#{timebox_type}" => timebox, action: :add, created_at: timebox_start_date + 4.days + 3.hours)
# Add milestone to an open issue with weight 2 on the same day. This should increment the scope totals for the same day.
create(:resource_weight_event, issue: issues[1], weight: 2, created_at: timebox_start_date)
create(:"resource_#{timebox_type}_event", issue: issues[1], "#{timebox_type}" => timebox, action: :add, created_at: timebox_start_date + 4.days + 5.hours)
# Add milestone to already closed issue with weight 3. This should increment both the scope and completed totals.
create(:resource_weight_event, issue: issues[2], weight: 3, created_at: timebox_start_date)
create(:resource_state_event, issue: issues[2], state: :closed, created_at: timebox_start_date + 4.days)
create(:"resource_#{timebox_type}_event", issue: issues[2], "#{timebox_type}" => timebox, action: :add, created_at: timebox_start_date + 5.days)
# Remove milestone from the 2nd open issue. This should decrement the scope totals.
create(:"resource_#{timebox_type}_event", issue: issues[1], "#{timebox_type}" => timebox, action: :remove, created_at: timebox_start_date + 6.days)
# Remove milestone from the closed issue. This should decrement both the scope and completed totals.
create(:"resource_#{timebox_type}_event", issue: issues[2], "#{timebox_type}" => timebox, action: :remove, created_at: timebox_start_date + 7.days)
# Adding a different milestone should not affect the data.
create(:"resource_#{timebox_type}_event", issue: issues[3], "#{timebox_type}" => another_timebox, action: :add, created_at: timebox_start_date + 7.days)
# Adding the milestone after the due date should not affect the data.
create(:"resource_#{timebox_type}_event", issue: issues[4], "#{timebox_type}" => timebox, action: :add, created_at: timebox_start_date + 21.days)
# Removing the milestone after the due date should not affect the data.
create(:"resource_#{timebox_type}_event", issue: issues[0], "#{timebox_type}" => timebox, action: :remove, created_at: timebox_start_date + 21.days)
expect(response.success?).to eq(true)
expect(response.payload).to eq([
{
date: timebox_start_date + 4.days,
scope_count: 2,
scope_weight: 2,
completed_count: 0,
completed_weight: 0
},
{
date: timebox_start_date + 5.days,
scope_count: 3,
scope_weight: 5,
completed_count: 1,
completed_weight: 3
},
{
date: timebox_start_date + 6.days,
scope_count: 2,
scope_weight: 3,
completed_count: 1,
completed_weight: 3
},
{
date: timebox_start_date + 7.days,
scope_count: 1,
scope_weight: 0,
completed_count: 0,
completed_weight: 0
}
])
end
it 'updates the completed counts when issue state is changed' do
# Close an issue assigned to the milestone with weight 2. This should increment the completed totals.
create(:"resource_#{timebox_type}_event", issue: issues[0], "#{timebox_type}" => timebox, action: :add, created_at: timebox_start_date + 1.hour)
create(:resource_weight_event, issue: issues[0], weight: 2, created_at: timebox_start_date + 2.hours)
create(:resource_state_event, issue: issues[0], state: :closed, created_at: timebox_start_date + 1.day)
# Closing an issue that is already closed should be ignored.
create(:resource_state_event, issue: issues[0], state: :closed, created_at: timebox_start_date + 2.days)
# Re-opening the issue should decrement the completed totals.
create(:resource_state_event, issue: issues[0], state: :reopened, created_at: timebox_start_date + 3.days)
# Closing and re-opening an issue on the same day should not change the totals.
create(:"resource_#{timebox_type}_event", issue: issues[1], "#{timebox_type}" => timebox, action: :add, created_at: timebox_start_date + 4.days + 1.hour)
create(:resource_weight_event, issue: issues[1], weight: 3, created_at: timebox_start_date + 4.days + 2.hours)
create(:resource_state_event, issue: issues[1], state: :closed, created_at: timebox_start_date + 5.days + 5.hours)
create(:resource_state_event, issue: issues[1], state: :reopened, created_at: timebox_start_date + 5.days + 8.hours)
# Re-opening an issue that is already open should be ignored.
create(:resource_state_event, issue: issues[1], state: :reopened, created_at: timebox_start_date + 6.days)
# Closing a re-opened issue should increment the completed totals.
create(:resource_state_event, issue: issues[1], state: :closed, created_at: timebox_start_date + 7.days)
# Changing state when the milestone is already removed should not affect the data.
create(:"resource_#{timebox_type}_event", issue: issues[1], action: :remove, created_at: timebox_start_date + 8.days)
create(:resource_state_event, issue: issues[1], state: :closed, created_at: timebox_start_date + 9.days)
expect(response.success?).to eq(true)
expect(response.payload).to eq([
{
date: timebox_start_date,
scope_count: 1,
scope_weight: 2,
completed_count: 0,
completed_weight: 0
},
{
date: timebox_start_date + 1.day,
scope_count: 1,
scope_weight: 2,
completed_count: 1,
completed_weight: 2
},
{
date: timebox_start_date + 3.days,
scope_count: 1,
scope_weight: 2,
completed_count: 0,
completed_weight: 0
},
{
date: timebox_start_date + 4.days,
scope_count: 2,
scope_weight: 5,
completed_count: 0,
completed_weight: 0
},
{
date: timebox_start_date + 5.days,
scope_count: 2,
scope_weight: 5,
completed_count: 0,
completed_weight: 0
},
{
date: timebox_start_date + 7.days,
scope_count: 2,
scope_weight: 5,
completed_count: 1,
completed_weight: 3
},
{
date: timebox_start_date + 8.days,
scope_count: 1,
scope_weight: 2,
completed_count: 0,
completed_weight: 0
}
])
end
it 'updates the weight totals when issue weight is changed' do
# Issue starts out with no weight and should increment once the weight is changed to 2.
create(:"resource_#{timebox_type}_event", issue: issues[0], "#{timebox_type}" => timebox, action: :add, created_at: timebox_start_date)
create(:resource_weight_event, issue: issues[0], weight: 2, created_at: timebox_start_date + 1.day)
# A closed issue is added and weight is set to 5 and should add to the weight totals.
create(:"resource_#{timebox_type}_event", issue: issues[1], "#{timebox_type}" => timebox, action: :add, created_at: timebox_start_date + 2.days + 1.hour)
create(:resource_state_event, issue: issues[1], state: :closed, created_at: timebox_start_date + 2.days + 2.hours)
create(:resource_weight_event, issue: issues[1], weight: 5, created_at: timebox_start_date + 2.days + 3.hours)
# Lowering the weight of the 2nd issue should decrement the weight totals.
create(:resource_weight_event, issue: issues[1], weight: 1, created_at: timebox_start_date + 3.days)
# After the first issue is assigned to another milestone, weight changes shouldn't affect the data.
create(:"resource_#{timebox_type}_event", issue: issues[0], "#{timebox_type}" => another_timebox, action: :add, created_at: timebox_start_date + 4.days)
create(:resource_weight_event, issue: issues[0], weight: 10, created_at: timebox_start_date + 5.days)
expect(response.success?).to eq(true)
expect(response.payload).to eq([
{
date: timebox_start_date,
scope_count: 1,
scope_weight: 0,
completed_count: 0,
completed_weight: 0
},
{
date: timebox_start_date + 1.day,
scope_count: 1,
scope_weight: 2,
completed_count: 0,
completed_weight: 0
},
{
date: timebox_start_date + 2.days,
scope_count: 2,
scope_weight: 7,
completed_count: 1,
completed_weight: 5
},
{
date: timebox_start_date + 3.days,
scope_count: 2,
scope_weight: 3,
completed_count: 1,
completed_weight: 1
},
{
date: timebox_start_date + 4.days,
scope_count: 1,
scope_weight: 1,
completed_count: 1,
completed_weight: 1
}
])
end
end
end
RSpec.describe TimeboxBurnupChartService do
let_it_be(:group) { create(:group) }
let_it_be(:project) { create(:project, group: group) }
let_it_be(:timebox_start_date) { Date.today }
let_it_be(:timebox_end_date) { timebox_start_date + 2.weeks }
let(:response) { described_class.new(timebox).execute }
context 'milestone charts' do
let_it_be(:timebox, reload: true) { create(:milestone, project: project, start_date: timebox_start_date, due_date: timebox_end_date) }
let_it_be(:another_timebox) { create(:milestone, project: project) }
let(:timebox_without_dates) { build(:milestone, project: project) }
it_behaves_like 'timebox chart', 'milestone'
end
context 'iteration charts' do
let_it_be(:timebox, reload: true) { create(:iteration, group: group, start_date: timebox_start_date, due_date: timebox_end_date) }
let_it_be(:another_timebox) { create(:iteration, group: group, start_date: timebox_end_date + 1.day, due_date: timebox_end_date + 15.days) }
let(:timebox_without_dates) { build(:iteration, group: group, start_date: nil, due_date: nil) }
it_behaves_like 'timebox chart', 'iteration'
end
end
......@@ -773,6 +773,12 @@ msgstr ""
msgid "%{timebox_name} should belong either to a project or a group."
msgstr ""
msgid "%{timebox_type} does not support burnup charts"
msgstr ""
msgid "%{timebox_type} must have a start and due date"
msgstr ""
msgid "%{title} %{operator} %{threshold}"
msgstr ""
......@@ -15857,18 +15863,12 @@ msgid_plural "Milestones"
msgstr[0] ""
msgstr[1] ""
msgid "Milestone does not support burnup charts"
msgstr ""
msgid "Milestone lists not available with your current license"
msgstr ""
msgid "Milestone lists show all issues from the selected milestone."
msgstr ""
msgid "Milestone must have a start and due date"
msgstr ""
msgid "MilestoneSidebar|Closed:"
msgstr ""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment