2016-08-29 12:24:48 +00:00
|
|
|
module Gitlab
|
|
|
|
module Ci
|
2016-09-05 09:55:30 +00:00
|
|
|
# # Introduction - total running time
|
|
|
|
#
|
2016-09-06 11:14:28 +00:00
|
|
|
# The problem this module is trying to solve is finding the total running
|
2016-08-30 19:09:16 +00:00
|
|
|
# time amongst all the jobs, excluding retries and pending (queue) time.
|
|
|
|
# We could reduce this problem down to finding the union of periods.
|
|
|
|
#
|
|
|
|
# So each job would be represented as a `Period`, which consists of
|
2016-09-05 09:55:30 +00:00
|
|
|
# `Period#first` as when the job started and `Period#last` as when the
|
|
|
|
# job was finished. A simple example here would be:
|
2016-08-30 19:09:16 +00:00
|
|
|
#
|
|
|
|
# * A (1, 3)
|
|
|
|
# * B (2, 4)
|
|
|
|
# * C (6, 7)
|
|
|
|
#
|
|
|
|
# Here A begins from 1, and ends to 3. B begins from 2, and ends to 4.
|
|
|
|
# C begins from 6, and ends to 7. Visually it could be viewed as:
|
|
|
|
#
|
|
|
|
# 0 1 2 3 4 5 6 7
|
|
|
|
# AAAAAAA
|
|
|
|
# BBBBBBB
|
|
|
|
# CCCC
|
|
|
|
#
|
|
|
|
# The union of A, B, and C would be (1, 4) and (6, 7), therefore the
|
|
|
|
# total running time should be:
|
|
|
|
#
|
|
|
|
# (4 - 1) + (7 - 6) => 4
|
|
|
|
#
|
2016-09-05 09:55:30 +00:00
|
|
|
# # The Algorithm
|
2016-08-30 19:09:16 +00:00
|
|
|
#
|
|
|
|
# The algorithm used here for union would be described as follow.
|
|
|
|
# First we make sure that all periods are sorted by `Period#first`.
|
|
|
|
# Then we try to merge periods by iterating through the first period
|
|
|
|
# to the last period. The goal would be merging all overlapped periods
|
|
|
|
# so that in the end all the periods are discrete. When all periods
|
|
|
|
# are discrete, we're free to just sum all the periods to get real
|
|
|
|
# running time.
|
|
|
|
#
|
|
|
|
# Here we begin from A, and compare it to B. We could find that
|
|
|
|
# before A ends, B already started. That is `B.first <= A.last`
|
|
|
|
# that is `2 <= 3` which means A and B are overlapping!
|
|
|
|
#
|
|
|
|
# When we found that two periods are overlapping, we would need to merge
|
|
|
|
# them into a new period and disregard the old periods. To make a new
|
|
|
|
# period, we take `A.first` as the new first because remember? we sorted
|
|
|
|
# them, so `A.first` must be smaller or equal to `B.first`. And we take
|
|
|
|
# `[A.last, B.last].max` as the new last because we want whoever ended
|
|
|
|
# later. This could be broken into two cases:
|
|
|
|
#
|
|
|
|
# 0 1 2 3 4
|
|
|
|
# AAAAAAA
|
|
|
|
# BBBBBBB
|
|
|
|
#
|
|
|
|
# Or:
|
|
|
|
#
|
|
|
|
# 0 1 2 3 4
|
|
|
|
# AAAAAAAAAA
|
|
|
|
# BBBB
|
|
|
|
#
|
|
|
|
# So that we need to take whoever ends later. Back to our example,
|
|
|
|
# after merging and discard A and B it could be visually viewed as:
|
|
|
|
#
|
|
|
|
# 0 1 2 3 4 5 6 7
|
|
|
|
# DDDDDDDDDD
|
|
|
|
# CCCC
|
|
|
|
#
|
|
|
|
# Now we could go on and compare the newly created D and the old C.
|
|
|
|
# We could figure out that D and C are not overlapping by checking
|
|
|
|
# `C.first <= D.last` is `false`. Therefore we need to keep both C
|
|
|
|
# and D. The example would end here because there are no more jobs.
|
|
|
|
#
|
2016-09-05 09:55:30 +00:00
|
|
|
# After having the union of all periods, we just need to sum the length
|
|
|
|
# of all periods to get total time.
|
2016-08-30 19:09:16 +00:00
|
|
|
#
|
2016-09-05 09:55:30 +00:00
|
|
|
# (4 - 1) + (7 - 6) => 4
|
2016-08-30 19:09:16 +00:00
|
|
|
#
|
2016-09-05 09:55:30 +00:00
|
|
|
# That is 4 is the answer in the example.
|
2016-09-06 11:14:28 +00:00
|
|
|
module PipelineDuration
|
|
|
|
extend self
|
|
|
|
|
2016-08-30 15:06:29 +00:00
|
|
|
PeriodStruct = Struct.new(:first, :last)
|
2016-08-30 15:11:57 +00:00
|
|
|
class Period < PeriodStruct
|
2016-08-29 12:24:48 +00:00
|
|
|
def duration
|
|
|
|
last - first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-06 11:14:28 +00:00
|
|
|
def from_pipeline(pipeline)
|
2016-09-02 11:15:42 +00:00
|
|
|
status = %w[success failed running canceled]
|
2016-09-06 11:36:07 +00:00
|
|
|
builds = pipeline.builds.latest.
|
|
|
|
where(status: status).where.not(started_at: nil).order(:started_at)
|
2016-09-02 11:15:42 +00:00
|
|
|
|
2016-09-06 11:57:51 +00:00
|
|
|
from_builds(builds)
|
2016-09-02 11:15:42 +00:00
|
|
|
end
|
|
|
|
|
2016-09-06 11:57:51 +00:00
|
|
|
def from_builds(builds)
|
|
|
|
now = Time.now
|
|
|
|
|
2016-08-30 15:06:29 +00:00
|
|
|
periods = builds.map do |b|
|
2016-09-06 11:57:51 +00:00
|
|
|
Period.new(b.started_at, b.finished_at || now)
|
2016-08-29 12:24:48 +00:00
|
|
|
end
|
|
|
|
|
2016-09-06 11:14:28 +00:00
|
|
|
from_periods(periods)
|
2016-08-29 12:24:48 +00:00
|
|
|
end
|
|
|
|
|
2016-09-06 11:36:07 +00:00
|
|
|
# periods should be sorted by `first`
|
2016-09-06 11:14:28 +00:00
|
|
|
def from_periods(periods)
|
2016-09-06 11:36:07 +00:00
|
|
|
process_duration(process_periods(periods))
|
2016-08-29 12:24:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-08-30 15:06:29 +00:00
|
|
|
def process_periods(periods)
|
2016-08-31 08:48:29 +00:00
|
|
|
return periods if periods.empty?
|
2016-08-29 12:24:48 +00:00
|
|
|
|
2016-08-31 08:48:29 +00:00
|
|
|
periods.drop(1).inject([periods.first]) do |result, current|
|
2016-09-08 13:47:19 +00:00
|
|
|
previous = result.last
|
2016-08-31 08:48:29 +00:00
|
|
|
|
2016-09-08 13:47:19 +00:00
|
|
|
if overlap?(previous, current)
|
|
|
|
result[-1] = merge(previous, current)
|
2016-08-31 08:48:29 +00:00
|
|
|
result
|
|
|
|
else
|
|
|
|
result << current
|
2016-08-29 12:24:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-08 13:47:19 +00:00
|
|
|
def overlap?(previous, current)
|
|
|
|
current.first <= previous.last
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge(previous, current)
|
|
|
|
Period.new(previous.first, [previous.last, current.last].max)
|
2016-08-29 12:24:48 +00:00
|
|
|
end
|
|
|
|
|
2016-08-30 15:06:29 +00:00
|
|
|
def process_duration(periods)
|
2016-09-06 10:12:46 +00:00
|
|
|
periods.sum(&:duration)
|
2016-08-29 12:24:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|