Commit 353d55dc authored by Alper Akgun's avatar Alper Akgun

Merge branch '344802-use-aggregated-tables-for-duration-chart' into 'master'

Use the aggregated tables for duration chart

See merge request gitlab-org/gitlab!74958
parents d6e94fa6 3f7e1bba
# frozen_string_literal: true
module EE
module Gitlab
module Analytics
module CycleAnalytics
module Aggregated
module DataCollector
def duration_chart_average_data
::Gitlab::Analytics::CycleAnalytics::Aggregated::DataForDurationChart
.new(stage: stage, params: params, query: query)
.average_by_day
end
end
end
end
end
end
end
......@@ -6,8 +6,12 @@ module EE
module CycleAnalytics
module DataCollector
def duration_chart_average_data
strong_memoize(:duration_chart_average_data) do
duration_chart.average_by_day
strong_memoize(:records_fetcher) do
if use_aggregated_data_collector?
aggregated_data_collector.duration_chart_average_data
else
duration_chart.average_by_day
end
end
end
......
# frozen_string_literal: true
module Gitlab
module Analytics
module CycleAnalytics
module Aggregated
class DataForDurationChart
include StageQueryHelpers
def initialize(stage:, params:, query:)
@stage = stage
@params = params
@query = query
end
# rubocop: disable CodeReuse/ActiveRecord
def average_by_day
@query
.group(date_expression)
.select(date_expression.as('date'), duration.average.as('average_duration_in_seconds'))
end
# rubocop: enable CodeReuse/ActiveRecord
private
attr_reader :stage, :query, :params
def date_expression
Arel::Nodes::NamedFunction.new('DATE', [query.model.arel_table[:end_event_timestamp]])
end
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Analytics::CycleAnalytics::Aggregated::DataForDurationChart do
let_it_be(:stage) { create(:cycle_analytics_group_stage, start_event_identifier: :issue_created, end_event_identifier: :issue_closed) }
let_it_be(:project) { create(:project, group: stage.group) }
let_it_be(:issue_1) { create(:issue, project: project) }
let_it_be(:issue_2) { create(:issue, project: project) }
let_it_be(:issue_3) { create(:issue, project: project) }
subject(:result) do
described_class
.new(stage: stage, params: {}, query: Analytics::CycleAnalytics::IssueStageEvent.all)
.average_by_day
end
it 'calculates the daily average stage duration' do
end_timestamp_1 = Time.zone.local(2020, 5, 6, 12, 0)
end_timestamp_2 = Time.zone.local(2020, 5, 15, 10, 0)
create(:cycle_analytics_issue_stage_event, issue_id: issue_1.id, start_event_timestamp: end_timestamp_1 - 3.days, end_event_timestamp: end_timestamp_1) # 3 days
create(:cycle_analytics_issue_stage_event, issue_id: issue_2.id, start_event_timestamp: end_timestamp_2 - 3.days, end_event_timestamp: end_timestamp_2) # 3 days
create(:cycle_analytics_issue_stage_event, issue_id: issue_3.id, start_event_timestamp: end_timestamp_2 - 1.day, end_event_timestamp: end_timestamp_2) # 1 day
average_two_days_ago = result[0]
average_today = result[1]
expect(average_two_days_ago).to have_attributes(date: end_timestamp_1.to_date, average_duration_in_seconds: 3.days)
expect(average_today).to have_attributes(date: end_timestamp_2.to_date, average_duration_in_seconds: 2.days)
end
end
......@@ -138,6 +138,15 @@ RSpec.describe Gitlab::Analytics::CycleAnalytics::DataCollector do
resource_2_end_time.utc.to_date => 5,
resource_3_end_time.utc.to_date => 15
})
if aggregated_data_collector_enabled
data = aggregated_data_collector.duration_chart_average_data.map { |item| [item.date, round_to_days(item.average_duration_in_seconds)] }
expect(Hash[data]).to eq({
resource_1_end_time.utc.to_date => 10,
resource_2_end_time.utc.to_date => 5,
resource_3_end_time.utc.to_date => 15
})
end
end
end
......
......@@ -280,7 +280,12 @@ RSpec.shared_examples 'Value Stream Analytics Stages controller' do
it 'matches the response schema' do
fake_result = [double(MergeRequest, average_duration_in_seconds: 10, date: Time.current.to_date)]
expect_any_instance_of(Gitlab::Analytics::CycleAnalytics::DataForDurationChart).to receive(:average_by_day).and_return(fake_result)
if Feature.enabled?(:use_vsa_aggregated_tables)
expect_any_instance_of(Gitlab::Analytics::CycleAnalytics::Aggregated::DataForDurationChart).to receive(:average_by_day).and_return(fake_result)
else
expect_any_instance_of(Gitlab::Analytics::CycleAnalytics::DataForDurationChart).to receive(:average_by_day).and_return(fake_result)
end
subject
......
......@@ -52,3 +52,5 @@ module Gitlab
end
end
end
Gitlab::Analytics::CycleAnalytics::Aggregated::DataCollector.prepend_mod_with('Gitlab::Analytics::CycleAnalytics::Aggregated::DataCollector')
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