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

more refactoring and fixing old specs

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