Commit aa04757a authored by Felipe Artur's avatar Felipe Artur

Code improvements on burndown model and view partial

parent 47ce88a3
......@@ -7,15 +7,12 @@ class Burndown
@end_date = @milestone.due_date
@end_date = Date.today if @end_date.present? && @end_date > Date.today
milestone_issues = @milestone.issues
@issues_count = milestone_issues.count
@issues_weight = milestone_issues.sum(:weight)
@issues_count, @issues_weight = milestone.issues.reorder(nil).pluck('COUNT(*), COALESCE(SUM(weight), 0)').first
end
# Returns the chart data in the following format:
# [date, issue count, issue weight] eg: [["2017-03-01", 33, 127], ["2017-03-02", 35, 73], ["2017-03-03", 28, 50]...]
def chart_data
def as_json(opts = nil)
return [] unless valid?
open_issues_count = issues_count
......@@ -27,7 +24,10 @@ class Burndown
closed_issues_count = closed.count
closed_issues_weight = sum_issues_weight(closed)
chart_data << [date.strftime("%Y-%m-%d"), open_issues_count -= closed_issues_count, open_issues_weight -= closed_issues_weight]
open_issues_count -= closed_issues_count
open_issues_weight -= closed_issues_weight
chart_data << [date.strftime("%Y-%m-%d"), open_issues_count, open_issues_weight]
reopened_count = reopened.count
reopened_weight = sum_issues_weight(reopened)
......@@ -44,22 +44,22 @@ class Burndown
private
def sum_issues_weight(issues)
issues.map(&:weight).compact.reduce(:+) || 0
issues.map(&:weight).sum
end
def closed_and_reopened_issues_by(date)
current_date = date.beginning_of_day
current_date = date.to_date
closed = issues_with_closed_at.select { |issue| issue.closed_at.beginning_of_day.to_i == current_date.to_i }
closed = issues_with_closed_at.select { |issue| issue.closed_at.to_date == current_date }
reopened = closed.select { |issue| issue.state == 'reopened' }
return closed, reopened
end
def issues_with_closed_at
@issues ||=
@issues_with_closed_at ||=
@milestone.issues.select('closed_at, weight, state').
where('closed_at IS NOT NULL').
order('closed_at ASC')
where('closed_at IS NOT NULL').
order('closed_at ASC')
end
end
......@@ -16,7 +16,7 @@
Issues
%button.btn.btn-xs{ data: { show: 'weight' } }
Issue weight
.burndown-chart{ data: { start_date: milestone.start_date.strftime("%Y-%m-%d"), due_date: milestone.due_date.strftime("%Y-%m-%d"), chart_data: burndown.chart_data.to_json } }
.burndown-chart{ data: { start_date: burndown.start_date.strftime("%Y-%m-%d"), due_date: burndown.end_date.strftime("%Y-%m-%d"), chart_data: burndown.to_json } }
- elsif can?(current_user, :admin_milestone, @project) && cookies['hide_burndown_message'].nil?
.burndown-hint.content-block.container-fluid
......
......@@ -27,7 +27,7 @@ describe Burndown, models: true do
Timecop.return
end
subject { described_class.new(milestone).chart_data }
subject { described_class.new(milestone).to_json }
it "generates an array with date, issue count and weight" do
expect(subject).to eq([
......@@ -36,25 +36,25 @@ describe Burndown, models: true do
["2017-03-03", 28, 56],
["2017-03-04", 32, 64],
["2017-03-05", 21, 42]
])
].to_json)
end
it "returns empty array if milestone start date is nil" do
milestone.update(start_date: nil)
expect(subject).to eq([])
expect(subject).to eq([].to_json)
end
it "returns empty array if milestone due date is nil" do
milestone.update(due_date: nil)
expect(subject).to eq([])
expect(subject).to eq([].to_json)
end
it "it counts until today if milestone due date > Date.today" do
Timecop.travel(milestone.due_date - 1.day)
expect(subject.last[0]).to eq(Time.now.strftime("%Y-%m-%d"))
expect(JSON.parse(subject).last[0]).to eq(Time.now.strftime("%Y-%m-%d"))
end
# Creates, closes and reopens issues only for odd days numbers
......
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