2014-10-13 21:21:32 +01:00
|
|
|
class StatisticPresenter < ApplicationPresenter
|
|
|
|
|
2014-10-21 21:56:36 +01:00
|
|
|
attr_accessor :range_start, :range_end, :unit, :users_or_groups
|
2014-10-13 21:21:32 +01:00
|
|
|
|
2014-10-21 21:56:36 +01:00
|
|
|
def initialize(range_start: nil, range_end: nil, unit: nil, users_or_groups: nil)
|
|
|
|
@range_start = range_start
|
|
|
|
@range_end = range_end
|
|
|
|
@unit = unit
|
|
|
|
@users_or_groups = users_or_groups.to_s.split(/,/).map(&:strip).select(&:present?).first(3)
|
2014-10-13 21:21:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def as_json(options = nil)
|
|
|
|
{
|
|
|
|
build_lists: {
|
|
|
|
build_started: prepare_collection(build_lists_started),
|
|
|
|
success: prepare_collection(build_lists_success),
|
|
|
|
build_error: prepare_collection(build_lists_error),
|
|
|
|
build_published: prepare_collection(build_lists_published),
|
|
|
|
|
|
|
|
build_started_count: build_lists_started.sum(&:count),
|
|
|
|
success_count: build_lists_success.sum(&:count),
|
|
|
|
build_error_count: build_lists_error.sum(&:count),
|
|
|
|
build_published_count: build_lists_published.sum(&:count),
|
2014-10-17 20:14:42 +01:00
|
|
|
},
|
|
|
|
commits: {
|
|
|
|
chart: prepare_collection(commits_chart),
|
|
|
|
commits_count: commits_chart.sum(&:count)
|
2014-10-20 20:18:34 +01:00
|
|
|
},
|
|
|
|
issues: {
|
|
|
|
open: prepare_collection(issues_open),
|
2014-10-22 20:10:52 +01:00
|
|
|
reopen: prepare_collection(issues_reopen),
|
2014-10-20 20:18:34 +01:00
|
|
|
closed: prepare_collection(issues_closed),
|
|
|
|
|
|
|
|
open_count: issues_open.sum(&:count),
|
2014-10-22 20:10:52 +01:00
|
|
|
reopen_count: issues_reopen.sum(&:count),
|
2014-10-20 20:18:34 +01:00
|
|
|
closed_count: issues_closed.sum(&:count)
|
|
|
|
},
|
|
|
|
pull_requests: {
|
|
|
|
open: prepare_collection(pull_requests_open),
|
|
|
|
merged: prepare_collection(pull_requests_merged),
|
|
|
|
closed: prepare_collection(pull_requests_closed),
|
|
|
|
|
|
|
|
open_count: pull_requests_open.sum(&:count),
|
|
|
|
merged_count: pull_requests_merged.sum(&:count),
|
|
|
|
closed_count: pull_requests_closed.sum(&:count)
|
2014-10-13 21:21:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2014-10-21 21:56:36 +01:00
|
|
|
def user_ids
|
|
|
|
@user_ids ||= User.where(uname: users_or_groups).pluck(:id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def group_ids
|
2014-10-21 22:19:50 +01:00
|
|
|
@group_ids ||= Group.where(uname: users_or_groups).pluck(:id)
|
2014-10-21 21:56:36 +01:00
|
|
|
end
|
|
|
|
|
2014-10-13 21:21:32 +01:00
|
|
|
def scope
|
2014-10-17 20:14:42 +01:00
|
|
|
@scope ||= Statistic.for_period(range_start, range_end).
|
2014-10-21 21:56:36 +01:00
|
|
|
for_users(user_ids).for_groups(group_ids).
|
2014-10-13 21:21:32 +01:00
|
|
|
select("SUM(counter) as count, date_trunc('#{ unit }', activity_at) as activity_at").
|
|
|
|
group("date_trunc('#{ unit }', activity_at)").order('activity_at')
|
|
|
|
end
|
|
|
|
|
2014-10-20 20:18:34 +01:00
|
|
|
def issues_open
|
|
|
|
@issues_open ||= scope.issues_open.to_a
|
|
|
|
end
|
|
|
|
|
2014-10-22 20:10:52 +01:00
|
|
|
def issues_reopen
|
|
|
|
@issues_reopen ||= scope.issues_reopen.to_a
|
|
|
|
end
|
|
|
|
|
2014-10-20 20:18:34 +01:00
|
|
|
def issues_closed
|
|
|
|
@issues_closed ||= scope.issues_closed.to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
def pull_requests_open
|
|
|
|
@pull_requests_open ||= scope.pull_requests_open.to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
def pull_requests_merged
|
|
|
|
@pull_requests_merged ||= scope.pull_requests_merged.to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
def pull_requests_closed
|
|
|
|
@pull_requests_closed ||= scope.pull_requests_closed.to_a
|
|
|
|
end
|
|
|
|
|
2014-10-17 20:14:42 +01:00
|
|
|
def commits_chart
|
|
|
|
@commits_chart ||= scope.commits.to_a
|
|
|
|
end
|
|
|
|
|
2014-10-13 21:21:32 +01:00
|
|
|
def build_lists_started
|
2014-10-17 20:14:42 +01:00
|
|
|
@build_lists_started ||= scope.build_lists_started.to_a
|
2014-10-13 21:21:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_lists_success
|
2014-10-17 20:14:42 +01:00
|
|
|
@build_lists_success ||= scope.build_lists_success.to_a
|
2014-10-13 21:21:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_lists_error
|
2014-10-17 20:14:42 +01:00
|
|
|
@build_lists_error ||= scope.build_lists_error.to_a
|
2014-10-13 21:21:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_lists_published
|
2014-10-17 20:14:42 +01:00
|
|
|
@build_lists_published ||= scope.build_lists_published.to_a
|
2014-10-13 21:21:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_collection(items)
|
|
|
|
data = []
|
|
|
|
to = range_start
|
|
|
|
while to <= range_end
|
|
|
|
from = to - 1.send(unit)
|
|
|
|
y = items.find{ |i| i.activity_at > from && i.activity_at <= to }.try(:count)
|
2014-10-14 20:34:42 +01:00
|
|
|
data << { x: to.strftime(format), y: y || 0 }
|
2014-10-13 21:21:32 +01:00
|
|
|
to += 1.send(unit)
|
|
|
|
end
|
|
|
|
data
|
|
|
|
end
|
|
|
|
|
|
|
|
def format
|
2014-10-14 20:34:42 +01:00
|
|
|
@format ||= unit == :hour ? '%H:%M' : '%Y-%m-%d'
|
2014-10-13 21:21:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|