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