From 236ddb2770d6dc321f0d05d93d3a1077c49ce477 Mon Sep 17 00:00:00 2001 From: Wedge Date: Sun, 12 Jun 2016 01:36:23 +0300 Subject: [PATCH] Add stats job --- app/jobs/update_statistics_job.rb | 42 +++++++++++++++++++++++++++++++ app/models/statistic.rb | 27 ++------------------ 2 files changed, 44 insertions(+), 25 deletions(-) create mode 100644 app/jobs/update_statistics_job.rb diff --git a/app/jobs/update_statistics_job.rb b/app/jobs/update_statistics_job.rb new file mode 100644 index 000000000..8fef9de69 --- /dev/null +++ b/app/jobs/update_statistics_job.rb @@ -0,0 +1,42 @@ +class UpdateStatisticsJob < BaseActiveRecordJob + include Sidekiq::Worker + sidekiq_options :queue => :middle + + def perform_with_ar_connection(args = {}) + defaults = {activity_at: nil, + user_id: nil, + project_id: nil, + key: nil, + counter: 1} + 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 \ No newline at end of file diff --git a/app/models/statistic.rb b/app/models/statistic.rb index 7288faea0..109a884a2 100644 --- a/app/models/statistic.rb +++ b/app/models/statistic.rb @@ -58,32 +58,9 @@ class Statistic < ActiveRecord::Base scope :build_lists_error, -> { where(key: KEY_BUILD_LIST_BUILD_ERROR) } scope :build_lists_published, -> { where(key: KEY_BUILD_LIST_BUILD_PUBLISHED) } - def self.now_statsd_increment(activity_at: nil, user_id: nil, project_id: nil, key: nil, counter: 1) - # Truncates a DateTime to the minute - activity_at = activity_at.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 - def self.statsd_increment(options = {}) - Statistic.perform_later(:middle, :now_statsd_increment, options) + options[:activity_at] = options[:activity_at].to_i if options[:activity_at] + UpdateStatisticsJob.perform_async(options) end end