Commit 02e1e481 authored by James Lopez's avatar James Lopez

more refactoring and fixing old specs

parent 8639ea1b
module CycleAnalyticsParams
extend ActiveSupport::Concern
def options
@options ||= { from: start_date(events_params), current_user: current_user }
end
def start_date(params)
params[:start_date] == '30' ? 30.days.ago : 90.days.ago
end
......
......@@ -51,10 +51,6 @@ module Projects
@events ||= Gitlab::CycleAnalytics::Events.new(project: project, options: options)
end
def options
@options ||= { from: start_date(events_params), current_user: current_user }
end
def events_params
return {} unless params[:events].present?
......
......@@ -6,7 +6,9 @@ class Projects::CycleAnalyticsController < Projects::ApplicationController
before_action :authorize_read_cycle_analytics!
def show
@cycle_analytics = ::CycleAnalytics.new(@project, current_user, from: start_date(cycle_analytics_params))
@cycle_analytics = ::CycleAnalytics.new(@project, options: options)
@cycle_analytics_no_data = @cycle_analytics.no_stats?
respond_to do |format|
format.html
......@@ -25,8 +27,8 @@ class Projects::CycleAnalyticsController < Projects::ApplicationController
def cycle_analytics_json
{
summary: @cycle_analytics.summary,
stats: nil, # TODO
permissions: @cycle_analytics.permissions(user: current_user)# TODO
stats: @cycle_analytics.stats,
permissions: @cycle_analytics.permissions(user: current_user)
}
end
end
class CycleAnalytics
STAGES = %i[issue plan code test review staging production].freeze
def initialize(project, from:)
def initialize(project, options:)
@project = project
@options = options
end
......@@ -10,22 +10,28 @@ class CycleAnalytics
@summary ||= Gitlab::CycleAnalytics::Summary.new(@project, from: @options[:from]).data
end
def method_missing(method_sym, *arguments, &block)
classify_stage(method_sym).new(project: @project, options: @options, stage: method_sym)
def stats
@stats ||= stats_per_stage
end
def no_stats?
stats.map(&:value).compact.empty?
end
def permissions(user:)
Gitlab::CycleAnalytics::Permissions.get(user: user, project: @project)
end
def issue
@fetcher.calculate_metric(:issue,
Issue.arel_table[:created_at],
[Issue::Metrics.arel_table[:first_associated_with_milestone_at],
Issue::Metrics.arel_table[:first_added_to_board_at]])
private
def stats_per_stage
STAGES.map do |stage_name|
classify_stage(method_sym).new(project: @project, options: @options, stage: stage_name).median_data
end
end
def classify_stage(method_sym)
"Gitlab::CycleAnalytics::#{method_sym.to_s.capitalize}Stage".constantize
def classify_stage(stage_name)
"Gitlab::CycleAnalytics::#{stage_name.to_s.capitalize}Stage".constantize
end
end
......@@ -9,9 +9,9 @@ module Gitlab
end
def data
[serialize(issue),
serialize(commit),
serialize(deploy)]
[serialize(Summary::Issue.new(project: @project, from: @from)),
serialize(Summary::Commit.new(project: @project, from: @from)),
serialize(Summary::Deploy.new(project: @project, from: @from))]
end
private
......@@ -19,18 +19,6 @@ module Gitlab
def serialize(summary_object)
AnalyticsSummarySerializer.new.represent(summary_object).as_json
end
def issue
Summary::Issue.new(project: @project, from: @from)
end
def deploy
Summary::Deploy.new(project: @project, from: @from)
end
def commit
Summary::Commit.new(project: @project, from: @from)
end
end
end
end
......@@ -6,7 +6,7 @@ describe 'CycleAnalytics#code', feature: true do
let(:project) { create(:project) }
let(:from_date) { 10.days.ago }
let(:user) { create(:user, :admin) }
subject { CycleAnalytics.new(project, user, from: from_date) }
subject { CycleAnalyticsTest.new(project, options: { from: from_date }) }
context 'with deployment' do
generate_cycle_analytics_spec(
......
......@@ -6,7 +6,7 @@ describe 'CycleAnalytics#issue', models: true do
let(:project) { create(:project) }
let(:from_date) { 10.days.ago }
let(:user) { create(:user, :admin) }
subject { CycleAnalytics.new(project, user, from: from_date) }
subject { CycleAnalyticsTest.new(project, options: { from: from_date }) }
generate_cycle_analytics_spec(
phase: :issue,
......
......@@ -6,7 +6,7 @@ describe 'CycleAnalytics#plan', feature: true do
let(:project) { create(:project) }
let(:from_date) { 10.days.ago }
let(:user) { create(:user, :admin) }
subject { CycleAnalytics.new(project, user, from: from_date) }
subject { CycleAnalyticsTest.new(project, options: { from: from_date }) }
generate_cycle_analytics_spec(
phase: :plan,
......
......@@ -6,7 +6,7 @@ describe 'CycleAnalytics#production', feature: true do
let(:project) { create(:project) }
let(:from_date) { 10.days.ago }
let(:user) { create(:user, :admin) }
subject { CycleAnalytics.new(project, user, from: from_date) }
subject { CycleAnalyticsTest.new(project, options: { from: from_date }) }
generate_cycle_analytics_spec(
phase: :production,
......
......@@ -6,7 +6,7 @@ describe 'CycleAnalytics#review', feature: true do
let(:project) { create(:project) }
let(:from_date) { 10.days.ago }
let(:user) { create(:user, :admin) }
subject { CycleAnalytics.new(project, user, from: from_date) }
subject { CycleAnalyticsTest.new(project, options: { from: from_date }) }
generate_cycle_analytics_spec(
phase: :review,
......
......@@ -6,7 +6,7 @@ describe 'CycleAnalytics#staging', feature: true do
let(:project) { create(:project) }
let(:from_date) { 10.days.ago }
let(:user) { create(:user, :admin) }
subject { CycleAnalytics.new(project, user, from: from_date) }
subject { CycleAnalyticsTest.new(project, options: { from: from_date }) }
generate_cycle_analytics_spec(
phase: :staging,
......
......@@ -4,7 +4,7 @@ describe CycleAnalytics::Summary, models: true do
let(:project) { create(:project) }
let(:from) { Time.now }
let(:user) { create(:user, :admin) }
subject { described_class.new(project, user, from: from) }
subject { CycleAnalyticsTest.new(project, options: { from: from_date }) }
describe "#new_issues" do
it "finds the number of issues created after the 'from date'" do
......
......@@ -6,7 +6,7 @@ describe 'CycleAnalytics#test', feature: true do
let(:project) { create(:project) }
let(:from_date) { 10.days.ago }
let(:user) { create(:user, :admin) }
subject { CycleAnalytics.new(project, user, from: from_date) }
subject { CycleAnalyticsTest.new(project, options: { from: from_date }) }
generate_cycle_analytics_spec(
phase: :test,
......
class CycleAnalyticsTest < CycleAnalytics
def method_missing(method_sym, *arguments, &block)
classify_stage(method_sym).new(project: @project, options: @options, stage: method_sym).median
end
end
# rubocop:disable Metrics/AbcSize
# Note: The ABC size is large here because we have a method generating test cases with
# multiple nested contexts. This shouldn't count as a violation.
module CycleAnalyticsHelpers
module TestGeneration
# Generate the most common set of specs that all cycle analytics phases need to have.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment