Commit 054dbb3b authored by Jan Provaznik's avatar Jan Provaznik

Merge branch '271538-replace-project-level-vsa-backend' into 'master'

Optionally use new VSA backend on the project level

See merge request gitlab-org/gitlab!47641
parents 96a6e93a efdc6a99
......@@ -4,9 +4,52 @@ module CycleAnalytics
module LevelBase
STAGES = %i[issue plan code test review staging].freeze
# This is a temporary adapter class which makes the new value stream (cycle analytics)
# backend compatible with the old implementation.
class StageAdapter
def initialize(stage, options)
@stage = stage
@options = options
end
# rubocop: disable CodeReuse/Presenter
def as_json(serializer: AnalyticsStageSerializer)
presenter = Analytics::CycleAnalytics::StagePresenter.new(stage)
serializer.new.represent(OpenStruct.new(
title: presenter.title,
description: presenter.description,
legend: presenter.legend,
name: stage.name,
project_median: median,
group_median: median
))
end
# rubocop: enable CodeReuse/Presenter
def events
data_collector.records_fetcher.serialized_records
end
def median
data_collector.median.seconds
end
alias_method :project_median, :median
alias_method :group_median, :median
private
attr_reader :stage, :options
def data_collector
@data_collector ||= Gitlab::Analytics::CycleAnalytics::DataCollector.new(stage: stage, params: options)
end
end
def all_medians_by_stage
STAGES.each_with_object({}) do |stage_name, medians_per_stage|
medians_per_stage[stage_name] = self[stage_name].project_median
medians_per_stage[stage_name] = self[stage_name].median
end
end
......@@ -21,7 +64,15 @@ module CycleAnalytics
end
def [](stage_name)
if Feature.enabled?(:new_project_level_vsa_backend, resource_parent)
StageAdapter.new(build_stage(stage_name), options)
else
Gitlab::CycleAnalytics::Stage[stage_name].new(options: options)
end
end
def stage_params_by_name(name)
Gitlab::Analytics::CycleAnalytics::DefaultStages.find_by_name!(name)
end
end
end
......@@ -20,5 +20,14 @@ module CycleAnalytics
def permissions(user:)
Gitlab::CycleAnalytics::Permissions.get(user: user, project: project)
end
def build_stage(stage_name)
stage_params = stage_params_by_name(stage_name).merge(project: project)
Analytics::CycleAnalytics::ProjectStage.new(stage_params)
end
def resource_parent
project
end
end
end
......@@ -49,7 +49,7 @@
%span.has-tooltip{ "data-placement" => "top", title: _("The value lying at the midpoint of a series of observed values. E.g., between 3, 5, 9, the median is 5. Between 3, 5, 7, 8, the median is (5+7)/2 = 6."), "aria-hidden" => "true" }
= sprite_icon('question-o', css_class: 'gl-text-gray-500')
%li.event-header.pl-3
%span.stage-name.font-weight-bold
%span.stage-name.font-weight-bold{ "v-if" => "currentStage && currentStage.legend" }
{{ currentStage ? __(currentStage.legend) : __('Related Issues') }}
%span.has-tooltip{ "data-placement" => "top", title: _("The collection of events added to the data gathered for that stage."), "aria-hidden" => "true" }
= sprite_icon('question-o', css_class: 'gl-text-gray-500')
......
---
name: new_project_level_vsa_backend
introduced_by_url:
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/282435
milestone: '13.7'
type: development
group: group::optimize
default_enabled: false
......@@ -37,6 +37,15 @@ module Analytics
self[stage_name].as_json(serializer: GroupAnalyticsStageSerializer)
end
end
def build_stage(stage_name)
stage_params = stage_params_by_name(stage_name).merge(group: group)
Analytics::CycleAnalytics::GroupStage.new(stage_params)
end
def resource_parent
group
end
end
end
end
......@@ -22,6 +22,10 @@ module Gitlab
]
end
def self.find_by_name!(name)
all.find { |raw_stage| raw_stage[:name].to_s.eql?(name.to_s) } || raise("Default stage '#{name}' not found")
end
def self.names
all.map { |stage| stage[:name] }
end
......
......@@ -48,6 +48,16 @@ RSpec.describe 'Value Stream Analytics', :js do
@build = create_cycle(user, project, issue, mr, milestone, pipeline)
deploy_master(user, project)
issue.metrics.update!(first_mentioned_in_commit_at: issue.metrics.first_associated_with_milestone_at + 1.day)
merge_request = issue.merge_requests_closing_issues.first.merge_request
merge_request.update!(created_at: issue.metrics.first_associated_with_milestone_at + 1.day)
merge_request.metrics.update!(
latest_build_started_at: 4.hours.ago,
latest_build_finished_at: 3.hours.ago,
merged_at: merge_request.created_at + 1.hour,
first_deployed_to_production_at: merge_request.created_at + 2.hours
)
sign_in(user)
visit project_cycle_analytics_path(project)
end
......
......@@ -40,6 +40,9 @@ RSpec.describe 'value stream analytics events', :aggregate_failures do
before do
create_commit_referencing_issue(context)
# Adding extra duration because the new VSA backend filters out 0 durations between these columns
context.metrics.update!(first_mentioned_in_commit_at: context.metrics.first_associated_with_milestone_at + 1.day)
end
it 'has correct attributes' do
......
......@@ -8,7 +8,7 @@ RSpec.describe 'CycleAnalytics#code' do
let_it_be(:project) { create(:project, :repository) }
let_it_be(:from_date) { 10.days.ago }
let_it_be(:user) { project.owner }
let_it_be(:project_level) { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date }) }
let_it_be(:project_level) { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date, current_user: user }) }
subject { project_level }
......
......@@ -8,7 +8,7 @@ RSpec.describe 'CycleAnalytics#issue' do
let_it_be(:project) { create(:project, :repository) }
let_it_be(:from_date) { 10.days.ago }
let_it_be(:user) { project.owner }
let_it_be(:project_level) { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date }) }
let_it_be(:project_level) { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date, current_user: user }) }
subject { project_level }
......
......@@ -8,7 +8,7 @@ RSpec.describe 'CycleAnalytics#plan' do
let_it_be(:project) { create(:project, :repository) }
let_it_be(:from_date) { 10.days.ago }
let_it_be(:user) { project.owner }
let_it_be(:project_level) { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date }) }
let_it_be(:project_level) { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date, current_user: user }) }
subject { project_level }
......
......@@ -9,7 +9,7 @@ RSpec.describe 'CycleAnalytics#review' do
let_it_be(:from_date) { 10.days.ago }
let_it_be(:user) { project.owner }
subject { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date }) }
subject { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date, current_user: user }) }
generate_cycle_analytics_spec(
phase: :review,
......
......@@ -8,7 +8,7 @@ RSpec.describe 'CycleAnalytics#staging' do
let_it_be(:project) { create(:project, :repository) }
let_it_be(:from_date) { 10.days.ago }
let_it_be(:user) { project.owner }
let_it_be(:project_level) { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date }) }
let_it_be(:project_level) { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date, current_user: user }) }
subject { project_level }
......
......@@ -9,7 +9,7 @@ RSpec.describe 'CycleAnalytics#test' do
let_it_be(:from_date) { 10.days.ago }
let_it_be(:user) { project.owner }
let_it_be(:issue) { create(:issue, project: project) }
let_it_be(:project_level) { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date }) }
let_it_be(:project_level) { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date, current_user: user }) }
let!(:merge_request) { create_merge_request_closing_issue(user, project, issue) }
subject { project_level }
......
......@@ -7,6 +7,7 @@ RSpec.describe 'value stream analytics events' do
let(:project) { create(:project, :repository, public_builds: false) }
let(:issue) { create(:issue, project: project, created_at: 2.days.ago) }
shared_examples 'value stream analytics events examples' do
describe 'GET /:namespace/:project/value_stream_analytics/events/issues' do
before do
project.add_developer(user)
......@@ -50,7 +51,7 @@ RSpec.describe 'value stream analytics events' do
expect(json_response['events'].first['iid']).to eq(first_mr_iid)
end
it 'lists the test events', :sidekiq_might_not_need_inline do
it 'lists the test events', :sidekiq_inline do
get project_cycle_analytics_test_path(project, format: :json)
expect(json_response['events']).not_to be_empty
......@@ -66,24 +67,13 @@ RSpec.describe 'value stream analytics events' do
expect(json_response['events'].first['iid']).to eq(first_mr_iid)
end
it 'lists the staging events', :sidekiq_might_not_need_inline do
it 'lists the staging events', :sidekiq_inline do
get project_cycle_analytics_staging_path(project, format: :json)
expect(json_response['events']).not_to be_empty
expect(json_response['events'].first['date']).not_to be_empty
end
context 'specific branch' do
it 'lists the test events', :sidekiq_might_not_need_inline do
branch = project.merge_requests.first.source_branch
get project_cycle_analytics_test_path(project, format: :json, branch: branch)
expect(json_response['events']).not_to be_empty
expect(json_response['events'].first['date']).not_to be_empty
end
end
context 'with private project and builds' do
before do
project.members.last.update(access_level: Gitlab::Access::GUEST)
......@@ -108,6 +98,23 @@ RSpec.describe 'value stream analytics events' do
end
end
end
end
describe 'when new_project_level_vsa_backend feature flag is off' do
before do
stub_feature_flags(new_project_level_vsa_backend: false, thing: project)
end
it_behaves_like 'value stream analytics events examples'
end
describe 'when new_project_level_vsa_backend feature flag is on' do
before do
stub_feature_flags(new_project_level_vsa_backend: true, thing: project)
end
it_behaves_like 'value stream analytics events examples'
end
def create_cycle
milestone = create(:milestone, project: project)
......@@ -123,5 +130,7 @@ RSpec.describe 'value stream analytics events' do
merge_merge_requests_closing_issue(user, project, issue)
ProcessCommitWorker.new.perform(project.id, user.id, mr.commits.last.to_hash)
mr.metrics.update!(latest_build_started_at: 1.hour.ago, latest_build_finished_at: Time.now)
end
end
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