2014-03-11 07:39:25 +00:00
|
|
|
module BuildListObserver
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
before_update :update_average_build_time
|
2014-10-13 21:21:32 +01:00
|
|
|
before_update :update_statistic
|
2014-03-11 07:39:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2014-10-13 21:21:32 +01:00
|
|
|
def update_statistic
|
|
|
|
Statistic.statsd_increment(
|
|
|
|
activity_at: Time.now,
|
2014-10-17 20:14:42 +01:00
|
|
|
key: "#{Statistic::KEY_BUILD_LIST}.#{status}",
|
2014-10-13 21:21:32 +01:00
|
|
|
project_id: project_id,
|
|
|
|
user_id: user_id,
|
|
|
|
) if status_changed?
|
|
|
|
end
|
|
|
|
|
2014-03-11 07:39:25 +00:00
|
|
|
def update_average_build_time
|
|
|
|
if status_changed?
|
2014-08-20 19:16:15 +01:00
|
|
|
self.started_at = Time.now if status == self.class::BUILD_STARTED
|
2014-03-14 22:54:04 +00:00
|
|
|
if [self.class::BUILD_ERROR,
|
|
|
|
self.class::SUCCESS,
|
|
|
|
self.class::BUILD_CANCELING,
|
|
|
|
self.class::TESTS_FAILED,
|
|
|
|
self.class::BUILD_CANCELED].include? status
|
2014-03-11 07:39:25 +00:00
|
|
|
# stores time interval beetwin build start and finish in seconds
|
2014-08-29 11:40:01 +01:00
|
|
|
self.duration = current_duration if self.started_at
|
2014-03-11 07:39:25 +00:00
|
|
|
|
2014-03-14 22:54:04 +00:00
|
|
|
if status == self.class::SUCCESS
|
2014-03-11 07:39:25 +00:00
|
|
|
# Update project average build time
|
|
|
|
begin
|
2014-03-19 22:01:42 +00:00
|
|
|
statistic = project.project_statistics.where(arch_id: arch_id).first_or_create
|
2014-03-11 07:39:25 +00:00
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
build_count = statistic.build_count.to_i
|
2014-03-14 22:54:04 +00:00
|
|
|
new_av_time = ( statistic.average_build_time * build_count + duration.to_i ) / ( build_count + 1 )
|
2015-05-22 21:31:43 +01:00
|
|
|
statistic.update_attributes(average_build_time: new_av_time, build_count: build_count + 1)
|
2014-03-11 07:39:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|