2014-10-06 20:16:12 +01:00
|
|
|
class Statistic < ActiveRecord::Base
|
2014-10-17 20:14:42 +01:00
|
|
|
KEYS = [
|
|
|
|
KEY_COMMIT = 'commit',
|
|
|
|
KEY_BUILD_LIST = 'build_list',
|
|
|
|
KEY_BUILD_LIST_BUILD_STARTED = "#{KEY_BUILD_LIST}.#{BuildList::BUILD_STARTED}",
|
|
|
|
KEY_BUILD_LIST_SUCCESS = "#{KEY_BUILD_LIST}.#{BuildList::SUCCESS}",
|
|
|
|
KEY_BUILD_LIST_BUILD_ERROR = "#{KEY_BUILD_LIST}.#{BuildList::BUILD_ERROR}",
|
2014-10-20 20:18:34 +01:00
|
|
|
KEY_BUILD_LIST_BUILD_PUBLISHED = "#{KEY_BUILD_LIST}.#{BuildList::BUILD_PUBLISHED}",
|
|
|
|
KEY_ISSUE = 'issue',
|
|
|
|
KEY_ISSUES_OPEN = "#{KEY_ISSUE}.#{Issue::STATUS_OPEN}",
|
2014-10-22 20:10:52 +01:00
|
|
|
KEY_ISSUES_REOPEN = "#{KEY_ISSUE}.#{Issue::STATUS_REOPEN}",
|
2014-10-20 20:18:34 +01:00
|
|
|
KEY_ISSUES_CLOSED = "#{KEY_ISSUE}.#{Issue::STATUS_CLOSED}",
|
|
|
|
KEY_PULL_REQUEST = 'pull_request',
|
|
|
|
KEY_PULL_REQUESTS_OPEN = "#{KEY_PULL_REQUEST}.#{PullRequest::STATUS_OPEN}",
|
|
|
|
KEY_PULL_REQUESTS_MERGED = "#{KEY_PULL_REQUEST}.#{PullRequest::STATUS_MERGED}",
|
|
|
|
KEY_PULL_REQUESTS_CLOSED = "#{KEY_PULL_REQUEST}.#{PullRequest::STATUS_CLOSED}",
|
2014-10-17 20:14:42 +01:00
|
|
|
]
|
|
|
|
|
2014-10-06 20:16:12 +01:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :project
|
|
|
|
|
|
|
|
validates :user_id,
|
2014-10-08 22:01:03 +01:00
|
|
|
uniqueness: { scope: [:project_id, :key, :activity_at] },
|
2014-10-06 20:16:12 +01:00
|
|
|
presence: true
|
|
|
|
|
|
|
|
validates :email,
|
|
|
|
presence: true
|
|
|
|
|
|
|
|
validates :project_id,
|
|
|
|
presence: true
|
|
|
|
|
|
|
|
validates :project_name_with_owner,
|
|
|
|
presence: true
|
|
|
|
|
2014-10-08 22:01:03 +01:00
|
|
|
validates :key,
|
2014-10-06 20:16:12 +01:00
|
|
|
presence: true
|
|
|
|
|
|
|
|
validates :counter,
|
|
|
|
presence: true
|
|
|
|
|
|
|
|
validates :activity_at,
|
|
|
|
presence: true
|
|
|
|
|
|
|
|
attr_accessible :user_id,
|
|
|
|
:email,
|
|
|
|
:project_id,
|
|
|
|
:project_name_with_owner,
|
2014-10-08 22:01:03 +01:00
|
|
|
:key,
|
2014-10-06 20:16:12 +01:00
|
|
|
:counter,
|
|
|
|
:activity_at
|
2014-10-06 20:35:50 +01:00
|
|
|
|
2014-10-21 21:56:36 +01:00
|
|
|
scope :for_period, -> (start_date, end_date) {
|
|
|
|
where(activity_at: (start_date..end_date))
|
|
|
|
}
|
|
|
|
scope :for_users, -> (user_ids) {
|
|
|
|
where(user_id: user_ids) if user_ids.present?
|
|
|
|
}
|
|
|
|
scope :for_groups, -> (group_ids) {
|
|
|
|
where(["project_id = ANY (
|
|
|
|
ARRAY (
|
|
|
|
SELECT target_id
|
|
|
|
FROM relations
|
|
|
|
INNER JOIN projects ON projects.id = relations.target_id
|
|
|
|
WHERE relations.target_type = 'Project' AND
|
2014-10-21 22:19:50 +01:00
|
|
|
projects.owner_type = 'Group' AND
|
|
|
|
relations.actor_type = 'Group' AND
|
|
|
|
relations.actor_id IN (:groups)
|
2014-10-21 21:56:36 +01:00
|
|
|
)
|
2014-10-21 22:19:50 +01:00
|
|
|
)", { groups: group_ids }
|
2014-10-21 21:56:36 +01:00
|
|
|
]) if group_ids.present?
|
|
|
|
}
|
2014-10-08 22:01:03 +01:00
|
|
|
|
2014-10-17 20:14:42 +01:00
|
|
|
scope :build_lists_started, -> { where(key: KEY_BUILD_LIST_BUILD_STARTED) }
|
|
|
|
scope :build_lists_success, -> { where(key: KEY_BUILD_LIST_SUCCESS) }
|
|
|
|
scope :build_lists_error, -> { where(key: KEY_BUILD_LIST_BUILD_ERROR) }
|
|
|
|
scope :build_lists_published, -> { where(key: KEY_BUILD_LIST_BUILD_PUBLISHED) }
|
|
|
|
scope :commits, -> { where(key: KEY_COMMIT) }
|
2014-10-20 20:18:34 +01:00
|
|
|
scope :issues_open, -> { where(key: KEY_ISSUES_OPEN) }
|
2014-10-22 20:10:52 +01:00
|
|
|
scope :issues_reopen, -> { where(key: KEY_ISSUES_REOPEN) }
|
2014-10-20 20:18:34 +01:00
|
|
|
scope :issues_closed, -> { where(key: KEY_ISSUES_CLOSED) }
|
|
|
|
scope :pull_requests_open, -> { where(key: KEY_PULL_REQUESTS_OPEN) }
|
|
|
|
scope :pull_requests_merged, -> { where(key: KEY_PULL_REQUESTS_MERGED) }
|
|
|
|
scope :pull_requests_closed, -> { where(key: KEY_PULL_REQUESTS_CLOSED) }
|
2014-10-08 22:01:03 +01:00
|
|
|
|
|
|
|
|
2014-10-17 20:14:42 +01:00
|
|
|
def self.now_statsd_increment(activity_at: nil, user_id: nil, project_id: nil, key: nil, counter: 1)
|
2014-10-06 20:35:50 +01:00
|
|
|
# Truncates a DateTime to the minute
|
2014-10-08 22:01:03 +01:00
|
|
|
activity_at = activity_at.utc.change(min: 0)
|
|
|
|
user = User.find user_id
|
|
|
|
project = Project.find project_id
|
2014-10-06 20:35:50 +01:00
|
|
|
Statistic.create(
|
2014-10-08 22:01:03 +01:00
|
|
|
user_id: user_id,
|
2014-10-06 20:35:50 +01:00
|
|
|
email: user.email,
|
2014-10-08 22:01:03 +01:00
|
|
|
project_id: project_id,
|
2014-10-06 20:35:50 +01:00
|
|
|
project_name_with_owner: project.name_with_owner,
|
2014-10-08 22:01:03 +01:00
|
|
|
key: key,
|
2014-10-06 20:35:50 +01:00
|
|
|
activity_at: activity_at
|
|
|
|
)
|
2014-10-15 21:30:12 +01:00
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
# Do nothing, see: ensure
|
2014-10-06 20:35:50 +01:00
|
|
|
ensure
|
|
|
|
Statistic.where(
|
2014-10-08 22:01:03 +01:00
|
|
|
user_id: user_id,
|
|
|
|
project_id: project_id,
|
|
|
|
key: key,
|
2014-10-06 20:35:50 +01:00
|
|
|
activity_at: activity_at
|
2014-10-20 17:45:18 +01:00
|
|
|
).update_all(['counter = counter + ?', counter]) if user_id.present? && project_id.present?
|
2014-10-06 20:35:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.statsd_increment(options = {})
|
|
|
|
Statistic.perform_later(:middle, :now_statsd_increment, options)
|
|
|
|
end
|
|
|
|
|
2014-10-06 20:16:12 +01:00
|
|
|
end
|