Skip to content
Snippets Groups Projects
Commit d4c46d49 authored by Shinya Maeda's avatar Shinya Maeda
Browse files

Add finished_at

parent 9dd4492a
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -50,6 +50,10 @@ class Deployment < ActiveRecord::Base
event :cancel do
transition any - [:canceled] => :canceled
end
before_transition any => [:success, :failed, :canceled] do |deployment|
deployment.finished_at = Time.now
end
end
 
def update_status
Loading
Loading
@@ -116,15 +120,15 @@ class Deployment < ActiveRecord::Base
end
 
def update_merge_request_metrics!
return unless environment.update_merge_request_metrics?
return unless environment.update_merge_request_metrics? && success?
 
merge_requests = project.merge_requests
.joins(:metrics)
.where(target_branch: self.ref, merge_request_metrics: { first_deployed_to_production_at: nil })
.where("merge_request_metrics.merged_at <= ?", self.created_at)
.where("merge_request_metrics.merged_at <= ?", finished_at)
 
if previous_deployment
merge_requests = merge_requests.where("merge_request_metrics.merged_at >= ?", previous_deployment.created_at)
merge_requests = merge_requests.where("merge_request_metrics.merged_at >= ?", previous_deployment.finished_at)
end
 
# Need to use `map` instead of `select` because MySQL doesn't allow `SELECT`ing from the same table
Loading
Loading
@@ -138,7 +142,7 @@ class Deployment < ActiveRecord::Base
 
MergeRequest::Metrics
.where(merge_request_id: merge_request_ids, first_deployed_to_production_at: nil)
.update_all(first_deployed_to_production_at: self.created_at)
.update_all(first_deployed_to_production_at: finished_at)
end
 
def previous_deployment
Loading
Loading
@@ -156,8 +160,16 @@ class Deployment < ActiveRecord::Base
@stop_action ||= manual_actions.find_by(name: on_stop)
end
 
def finished_at
return self.created_at if status.nil?
self.finished_at
end
def formatted_deployment_time
created_at.to_time.in_time_zone.to_s(:medium)
return unless success?
finished_at.to_time.in_time_zone.to_s(:medium)
end
 
def has_metrics?
Loading
Loading
@@ -165,17 +177,17 @@ class Deployment < ActiveRecord::Base
end
 
def metrics
return {} unless has_metrics?
return {} unless has_metrics? && success?
 
metrics = prometheus_adapter.query(:deployment, self)
metrics&.merge(deployment_time: created_at.to_i) || {}
metrics&.merge(deployment_time: finished_at.to_i) || {}
end
 
def additional_metrics
return {} unless has_metrics?
return {} unless has_metrics? && success?
 
metrics = prometheus_adapter.query(:additional_metrics_deployment, self)
metrics&.merge(deployment_time: created_at.to_i) || {}
metrics&.merge(deployment_time: finished_at.to_i) || {}
end
 
private
Loading
Loading
Loading
Loading
@@ -5,5 +5,6 @@ class AddStatusToDeployments < ActiveRecord::Migration
 
def change
add_column :deployments, :status, :integer, limit: 2
add_column :deployments, :finished_at, :datetime_with_timezone
end
end
Loading
Loading
@@ -773,6 +773,7 @@ ActiveRecord::Schema.define(version: 20181016141739) do
t.datetime "updated_at"
t.string "on_stop"
t.integer "status", limit: 2
t.datetime_with_timezone "finished_at"
end
 
add_index "deployments", ["created_at"], name: "index_deployments_on_created_at", using: :btree
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment