Commit 1f6efa35 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Rename to periods since it's easier to understand

parent bd78e6af
module Gitlab module Gitlab
module Ci module Ci
class PipelineDuration class PipelineDuration
SegmentStruct = Struct.new(:first, :last) PeriodStruct = Struct.new(:first, :last)
class Segment < SegmentStruct class Period < SegmentStruct
def duration def duration
last - first last - first
end end
...@@ -11,34 +11,34 @@ module Gitlab ...@@ -11,34 +11,34 @@ module Gitlab
def self.from_builds(builds) def self.from_builds(builds)
now = Time.now now = Time.now
segments = builds.map do |b| periods = builds.map do |b|
Segment.new(b.started_at || now, b.finished_at || now) Period.new(b.started_at || now, b.finished_at || now)
end end
new(segments) new(periods)
end end
attr_reader :duration, :pending_duration attr_reader :duration, :pending_duration
def initialize(segments) def initialize(periods)
process(segments.sort_by(&:first)) process(periods.sort_by(&:first))
end end
private private
def process(segments) def process(periods)
merged = process_segments(segments) merged = process_periods(periods)
@duration = process_duration(merged) @duration = process_duration(merged)
@pending_duration = process_pending_duration(merged, @duration) @pending_duration = process_pending_duration(merged, @duration)
end end
def process_segments(segments) def process_periods(periods)
if segments.empty? if periods.empty?
segments periods
else else
segments.drop(1).inject([segments.first]) do |result, current| periods.drop(1).inject([periods.first]) do |result, current|
merged = try_merge_segment(result.last, current) merged = try_merge_period(result.last, current)
if merged if merged
result[-1] = merged result[-1] = merged
...@@ -50,22 +50,22 @@ module Gitlab ...@@ -50,22 +50,22 @@ module Gitlab
end end
end end
def try_merge_segment(previous, current) def try_merge_period(previous, current)
if current.first <= previous.last if current.first <= previous.last
Segment.new(previous.first, [previous.last, current.last].max) Period.new(previous.first, [previous.last, current.last].max)
end end
end end
def process_duration(segments) def process_duration(periods)
segments.inject(0) do |result, seg| periods.inject(0) do |result, per|
result + seg.duration result + per.duration
end end
end end
def process_pending_duration(segments, duration) def process_pending_duration(periods, duration)
return 0 if segments.empty? return 0 if periods.empty?
total = segments.last.last - segments.first.first total = periods.last.last - periods.first.first
total - duration total - duration
end end
end end
......
...@@ -87,7 +87,7 @@ describe Gitlab::Ci::PipelineDuration do ...@@ -87,7 +87,7 @@ describe Gitlab::Ci::PipelineDuration do
def create_calculator(data) def create_calculator(data)
segments = data.shuffle.map do |(first, last)| segments = data.shuffle.map do |(first, last)|
Gitlab::Ci::PipelineDuration::Segment.new(first, last) Gitlab::Ci::PipelineDuration::Period.new(first, last)
end end
Gitlab::Ci::PipelineDuration.new(segments) Gitlab::Ci::PipelineDuration.new(segments)
......
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