diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2016-08-10 22:45:30 +0800 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2016-08-10 23:12:02 +0800 |
commit | 21fdc1edaecf228c1fdc540375bbdad0fd69164a (patch) | |
tree | 8da76adcca752e497daa559ca7667787d20c3003 /app | |
parent | 831b6c8fda1adec32c0719500ad3e830284fb2af (diff) | |
download | gitlab-ce-21fdc1edaecf228c1fdc540375bbdad0fd69164a.tar.gz |
Cleanup the use of duration and optimize some queries
Diffstat (limited to 'app')
-rw-r--r-- | app/helpers/time_helper.rb | 22 | ||||
-rw-r--r-- | app/models/ci/pipeline.rb | 10 | ||||
-rw-r--r-- | app/models/commit_status.rb | 14 | ||||
-rw-r--r-- | app/models/concerns/statuseable.rb | 15 | ||||
-rw-r--r-- | app/views/admin/builds/_build.html.haml | 2 | ||||
-rw-r--r-- | app/views/projects/ci/builds/_build.html.haml | 2 | ||||
-rw-r--r-- | app/views/projects/ci/pipelines/_pipeline.html.haml | 4 | ||||
-rw-r--r-- | app/views/projects/pipelines/_info.html.haml | 4 |
8 files changed, 31 insertions, 42 deletions
diff --git a/app/helpers/time_helper.rb b/app/helpers/time_helper.rb index a031abb8676..271e839692a 100644 --- a/app/helpers/time_helper.rb +++ b/app/helpers/time_helper.rb @@ -15,25 +15,9 @@ module TimeHelper "#{from.to_s(:short)} - #{to.to_s(:short)}" end - def duration_in_numbers(finished_at, started_at) - interval = interval_in_seconds(started_at, finished_at) + def duration_in_numbers(duration) + time_format = duration < 1.hour ? "%M:%S" : "%H:%M:%S" - duration_in_numbers_from_interval(interval) - end - - def duration_in_numbers_from_interval(interval) - time_format = interval < 1.hour ? "%M:%S" : "%H:%M:%S" - - Time.at(interval).utc.strftime(time_format) - end - - private - - def interval_in_seconds(started_at, finished_at = nil) - if started_at && finished_at - finished_at.to_i - started_at.to_i - elsif started_at - Time.now.to_i - started_at.to_i - end + Time.at(duration).utc.strftime(time_format) end end diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index db29df668bf..8b1d80299cc 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -34,6 +34,10 @@ module Ci CommitStatus.where(pipeline: pluck(:id)).stages end + def self.duration + where.not(duration: nil).pluck(:duration).inject(0, &:+) + end + def project_id project.id end @@ -213,10 +217,6 @@ module Ci ] end - def wall_clock_duration - finished_at.to_i - started_at.to_i if finished_at && started_at - end - private def build_builds_for_stages(stages, user, status, trigger_request) @@ -240,7 +240,7 @@ module Ci end self.started_at = statuses.started_at self.finished_at = statuses.finished_at - self.duration = statuses.latest.duration + self.duration = calculate_duration.to_i save end diff --git a/app/models/commit_status.rb b/app/models/commit_status.rb index 2d185c28809..5d0a28ecfcc 100644 --- a/app/models/commit_status.rb +++ b/app/models/commit_status.rb @@ -21,6 +21,7 @@ class CommitStatus < ActiveRecord::Base where(id: max_id.group(:name, :commit_id)) end + scope :retried, -> { where.not(id: latest) } scope :ordered, -> { order(:name) } scope :ignored, -> { where(allow_failure: true, status: [:failed, :canceled]) } @@ -83,18 +84,17 @@ class CommitStatus < ActiveRecord::Base end end + def self.duration + select(:started_at, :finished_at).all. + map(&:duration).compact.inject(0, &:+) + end + def ignored? allow_failure? && (failed? || canceled?) end def duration - duration = - if started_at && finished_at - finished_at - started_at - elsif started_at - Time.now - started_at - end - duration + calculate_duration end def stuck? diff --git a/app/models/concerns/statuseable.rb b/app/models/concerns/statuseable.rb index 44c6b30f278..37efc44b79b 100644 --- a/app/models/concerns/statuseable.rb +++ b/app/models/concerns/statuseable.rb @@ -31,11 +31,6 @@ module Statuseable all.pluck(self.status_sql).first end - def duration - duration_array = all.map(&:duration).compact - duration_array.reduce(:+) - end - def started_at all.minimum(:started_at) end @@ -78,4 +73,14 @@ module Statuseable def complete? canceled? || success? || failed? end + + private + + def calculate_duration + if started_at && finished_at + finished_at - started_at + elsif started_at + Time.now - started_at + end + end end diff --git a/app/views/admin/builds/_build.html.haml b/app/views/admin/builds/_build.html.haml index 352adbedee4..f29d9c94441 100644 --- a/app/views/admin/builds/_build.html.haml +++ b/app/views/admin/builds/_build.html.haml @@ -51,7 +51,7 @@ - if build.duration %p.duration = custom_icon("icon_timer") - = duration_in_numbers(build.finished_at, build.started_at) + = duration_in_numbers(build.duration) - if build.finished_at %p.finished-at diff --git a/app/views/projects/ci/builds/_build.html.haml b/app/views/projects/ci/builds/_build.html.haml index 91081435220..1fdf32466f2 100644 --- a/app/views/projects/ci/builds/_build.html.haml +++ b/app/views/projects/ci/builds/_build.html.haml @@ -63,7 +63,7 @@ - if build.duration %p.duration = custom_icon("icon_timer") - = duration_in_numbers(build.finished_at, build.started_at) + = duration_in_numbers(build.duration) - if build.finished_at %p.finished-at = icon("calendar") diff --git a/app/views/projects/ci/pipelines/_pipeline.html.haml b/app/views/projects/ci/pipelines/_pipeline.html.haml index 60c3c27bd1a..c2fecbf9d4c 100644 --- a/app/views/projects/ci/pipelines/_pipeline.html.haml +++ b/app/views/projects/ci/pipelines/_pipeline.html.haml @@ -46,10 +46,10 @@ \- %td - - if pipeline.wall_clock_duration + - if pipeline.duration %p.duration = custom_icon("icon_timer") - = duration_in_numbers_from_interval(pipeline.wall_clock_duration) + = duration_in_numbers(pipeline.duration) - if pipeline.finished_at %p.finished-at = icon("calendar") diff --git a/app/views/projects/pipelines/_info.html.haml b/app/views/projects/pipelines/_info.html.haml index a0f6c19350d..063e83a407a 100644 --- a/app/views/projects/pipelines/_info.html.haml +++ b/app/views/projects/pipelines/_info.html.haml @@ -7,9 +7,9 @@ - if @pipeline.ref for = link_to @pipeline.ref, namespace_project_commits_path(@project.namespace, @project, @pipeline.ref), class: "monospace" - - if @pipeline.wall_clock_duration + - if @pipeline.duration in - = time_interval_in_words(@pipeline.wall_clock_duration) + = time_interval_in_words(@pipeline.duration) .pull-right = link_to namespace_project_pipeline_path(@project.namespace, @project, @pipeline), class: "ci-status ci-#{@pipeline.status}" do |