Commit 3a68c989 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Just use module because there's nothing to save, feedback:

https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/6084#note_14992064
parent b92c75ab
...@@ -2,7 +2,7 @@ module Gitlab ...@@ -2,7 +2,7 @@ module Gitlab
module Ci module Ci
# # Introduction - total running time # # Introduction - total running time
# #
# The problem this class is trying to solve is finding the total running # The problem this module is trying to solve is finding the total running
# time amongst all the jobs, excluding retries and pending (queue) time. # time amongst all the jobs, excluding retries and pending (queue) time.
# We could reduce this problem down to finding the union of periods. # We could reduce this problem down to finding the union of periods.
# #
...@@ -76,7 +76,9 @@ module Gitlab ...@@ -76,7 +76,9 @@ module Gitlab
# (4 - 1) + (7 - 6) => 4 # (4 - 1) + (7 - 6) => 4
# #
# That is 4 is the answer in the example. # That is 4 is the answer in the example.
class PipelineDuration module PipelineDuration
extend self
PeriodStruct = Struct.new(:first, :last) PeriodStruct = Struct.new(:first, :last)
class Period < PeriodStruct class Period < PeriodStruct
def duration def duration
...@@ -84,33 +86,27 @@ module Gitlab ...@@ -84,33 +86,27 @@ module Gitlab
end end
end end
def self.from_pipeline(pipeline) def from_pipeline(pipeline)
status = %w[success failed running canceled] status = %w[success failed running canceled]
builds = pipeline.builds.latest.where(status: status) builds = pipeline.builds.latest.where(status: status)
from_builds(builds, :started_at, :finished_at).duration from_builds(builds, :started_at, :finished_at)
end end
def self.from_builds(builds, from, to, now = Time.now) def from_builds(builds, from, to, now = Time.now)
periods = builds.map do |b| periods = builds.map do |b|
Period.new(b.public_send(from) || now, b.public_send(to) || now) Period.new(b.public_send(from) || now, b.public_send(to) || now)
end end
new(periods) from_periods(periods)
end end
attr_reader :duration def from_periods(periods)
process_duration(process_periods(periods.sort_by(&:first)))
def initialize(periods)
process(periods.sort_by(&:first))
end end
private private
def process(periods)
@duration = process_duration(process_periods(periods))
end
def process_periods(periods) def process_periods(periods)
return periods if periods.empty? return periods if periods.empty?
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Ci::PipelineDuration do describe Gitlab::Ci::PipelineDuration do
let(:calculator) { create_calculator(data) } let(:calculated_duration) { calculate(data) }
shared_examples 'calculating duration' do shared_examples 'calculating duration' do
it do it do
expect(calculator.duration).to eq(duration) expect(calculated_duration).to eq(duration)
end end
end end
...@@ -105,11 +105,11 @@ describe Gitlab::Ci::PipelineDuration do ...@@ -105,11 +105,11 @@ describe Gitlab::Ci::PipelineDuration do
it_behaves_like 'calculating duration' it_behaves_like 'calculating duration'
end end
def create_calculator(data) def calculate(data)
periods = data.shuffle.map do |(first, last)| periods = data.shuffle.map do |(first, last)|
Gitlab::Ci::PipelineDuration::Period.new(first, last) Gitlab::Ci::PipelineDuration::Period.new(first, last)
end end
Gitlab::Ci::PipelineDuration.new(periods) Gitlab::Ci::PipelineDuration.from_periods(periods)
end end
end end
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