rosa-build/app/presenters/statistic_presenter.rb

112 lines
3.2 KiB
Ruby
Raw Normal View History

class StatisticPresenter < ApplicationPresenter
attr_accessor :range_start, :range_end, :unit
def initialize(range_start: nil, range_end: nil, unit: nil)
@range_start = range_start
@range_end = range_end
@unit = unit
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)
},
issues: {
open: prepare_collection(issues_open),
closed: prepare_collection(issues_closed),
open_count: issues_open.sum(&:count),
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)
}
}
end
private
def scope
2014-10-17 20:14:42 +01:00
@scope ||= Statistic.for_period(range_start, range_end).
select("SUM(counter) as count, date_trunc('#{ unit }', activity_at) as activity_at").
group("date_trunc('#{ unit }', activity_at)").order('activity_at')
end
def issues_open
@issues_open ||= scope.issues_open.to_a
end
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
def build_lists_started
2014-10-17 20:14:42 +01:00
@build_lists_started ||= scope.build_lists_started.to_a
end
def build_lists_success
2014-10-17 20:14:42 +01:00
@build_lists_success ||= scope.build_lists_success.to_a
end
def build_lists_error
2014-10-17 20:14:42 +01:00
@build_lists_error ||= scope.build_lists_error.to_a
end
def build_lists_published
2014-10-17 20:14:42 +01:00
@build_lists_published ||= scope.build_lists_published.to_a
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 }
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'
end
end