2018-11-06 04:45:35 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-01-30 11:28:15 +00:00
|
|
|
module Gitlab
|
|
|
|
module CycleAnalytics
|
|
|
|
class UsageData
|
|
|
|
PROJECTS_LIMIT = 10
|
|
|
|
|
|
|
|
attr_reader :projects, :options
|
|
|
|
|
2018-02-15 13:23:39 +00:00
|
|
|
def initialize
|
|
|
|
@projects = Project.sorted_by_activity.limit(PROJECTS_LIMIT)
|
|
|
|
@options = { from: 7.days.ago }
|
2018-01-30 11:28:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_json
|
|
|
|
total = 0
|
|
|
|
|
2018-02-15 13:23:39 +00:00
|
|
|
values =
|
|
|
|
medians_per_stage.each_with_object({}) do |(stage_name, medians), hsh|
|
|
|
|
calculations = stage_values(medians)
|
2018-01-30 11:28:15 +00:00
|
|
|
|
2018-02-15 13:23:39 +00:00
|
|
|
total += calculations.values.compact.sum
|
|
|
|
hsh[stage_name] = calculations
|
|
|
|
end
|
2018-01-30 11:28:15 +00:00
|
|
|
|
|
|
|
values[:total] = total
|
|
|
|
|
|
|
|
{ avg_cycle_analytics: values }
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def medians_per_stage
|
2018-02-12 12:24:42 +00:00
|
|
|
projects.each_with_object({}) do |project, hsh|
|
|
|
|
::CycleAnalytics.new(project, options).all_medians_per_stage.each do |stage_name, median|
|
|
|
|
hsh[stage_name] ||= []
|
|
|
|
hsh[stage_name] << median
|
|
|
|
end
|
|
|
|
end
|
2018-01-30 11:28:15 +00:00
|
|
|
end
|
|
|
|
|
2018-02-15 13:23:39 +00:00
|
|
|
def stage_values(medians)
|
|
|
|
medians = medians.map(&:presence).compact
|
|
|
|
average = calc_average(medians)
|
|
|
|
|
|
|
|
{
|
|
|
|
average: average,
|
|
|
|
sd: standard_deviation(medians, average),
|
|
|
|
missing: projects.length - medians.length
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-01-30 11:28:15 +00:00
|
|
|
def calc_average(values)
|
|
|
|
return if values.empty?
|
|
|
|
|
|
|
|
(values.sum / values.length).to_i
|
|
|
|
end
|
|
|
|
|
2018-02-15 13:23:39 +00:00
|
|
|
def standard_deviation(values, average)
|
|
|
|
Math.sqrt(sample_variance(values, average)).to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def sample_variance(values, average)
|
2018-01-30 11:28:15 +00:00
|
|
|
return 0 if values.length <= 1
|
|
|
|
|
|
|
|
sum = values.inject(0) do |acc, val|
|
2018-02-15 13:23:39 +00:00
|
|
|
acc + (val - average)**2
|
2018-01-30 11:28:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
sum / (values.length - 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|