rosa-build/app/models/concerns/build_list_observer.rb

46 lines
1.4 KiB
Ruby
Raw Normal View History

module BuildListObserver
extend ActiveSupport::Concern
included do
before_update :update_average_build_time
before_update :update_statistic
end
private
def update_statistic
Statistic.statsd_increment(
activity_at: Time.now,
2014-10-17 20:14:42 +01:00
key: "#{Statistic::KEY_BUILD_LIST}.#{status}",
project_id: project_id,
user_id: user_id,
) if status_changed?
end
def update_average_build_time
if status_changed?
self.started_at = Time.now if status == self.class::BUILD_STARTED
if [self.class::BUILD_ERROR,
self.class::SUCCESS,
self.class::BUILD_CANCELING,
self.class::TESTS_FAILED,
self.class::BUILD_CANCELED].include? status
# 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
if status == self.class::SUCCESS
# 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
rescue ActiveRecord::RecordNotUnique
retry
end
build_count = statistic.build_count.to_i
new_av_time = ( statistic.average_build_time * build_count + duration.to_i ) / ( build_count + 1 )
2014-03-19 22:01:42 +00:00
statistic.update_attributes({average_build_time: new_av_time, build_count: build_count + 1}, without_protection: true)
end
end
end
end
end