2016-03-20 09:24:23 +00:00
|
|
|
class StatisticPresenter
|
2014-10-13 21:21:32 +01:00
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|