rosa-build/app/jobs/update_statistics_job.rb

42 lines
1.4 KiB
Ruby
Raw Permalink Normal View History

2016-06-11 23:36:23 +01:00
class UpdateStatisticsJob < BaseActiveRecordJob
include Sidekiq::Worker
sidekiq_options :queue => :middle
def perform_with_ar_connection(args = {})
2016-06-11 23:45:31 +01:00
defaults = {'activity_at' => nil,
'user_id' => nil,
'project_id' => nil,
'key' => nil,
'counter' => 1}
2016-06-11 23:36:23 +01:00
options = defaults.merge(args)
statsd_increment(activity_at: options['activity_at'], user_id: options['user_id'],
project_id: options['project_id'], key: options['key'], counter: options['counter'])
end
private
def statsd_increment(activity_at: nil, user_id: nil, project_id: nil, key: nil, counter: 1)
# Truncates a DateTime to the minute
activity_at = Time.at(activity_at.to_i).utc.change(min: 0)
user = User.find user_id
project = Project.find project_id
Statistic.create(
user_id: user_id,
email: user.email,
project_id: project_id,
project_name_with_owner: project.name_with_owner,
key: key,
activity_at: activity_at
)
rescue ActiveRecord::RecordNotUnique
# Do nothing, see: ensure
ensure
Statistic.where(
user_id: user_id,
project_id: project_id,
key: key,
activity_at: activity_at
).update_all(['counter = counter + ?', counter]) if user_id.present? && project_id.present?
end
end