Commit abca34d0 authored by Małgorzata Ksionek's avatar Małgorzata Ksionek

Modify base class for cycle analytics fetching

parent 68a5e205
...@@ -9,7 +9,7 @@ module Gitlab ...@@ -9,7 +9,7 @@ module Gitlab
MAX_EVENTS = 50 MAX_EVENTS = 50
def initialize(project:, stage:, options:) def initialize(project: nil, stage:, options:)
@project = project @project = project
@stage = stage @stage = stage
@options = options @options = options
...@@ -59,13 +59,17 @@ module Gitlab ...@@ -59,13 +59,17 @@ module Gitlab
def allowed_ids def allowed_ids
@allowed_ids ||= allowed_ids_finder_class @allowed_ids ||= allowed_ids_finder_class
.new(@options[:current_user], project_id: @project.id) .new(@options[:current_user], allowed_ids_source)
.execute.where(id: event_result_ids).pluck(:id) .execute.where(id: event_result_ids).pluck(:id)
end end
def event_result_ids def event_result_ids
event_result.map { |event| event['id'] } event_result.map { |event| event['id'] }
end end
def allowed_ids_source
{ project_id: @project.id }
end
end end
end end
end end
...@@ -10,7 +10,7 @@ module Gitlab ...@@ -10,7 +10,7 @@ module Gitlab
private private
def base_query def base_query
@base_query ||= stage_query(@project.id) # rubocop:disable Gitlab/ModuleWithInstanceVariables @base_query ||= stage_query(projects.map(&:id)) # rubocop:disable Gitlab/ModuleWithInstanceVariables
end end
def stage_query(project_ids) def stage_query(project_ids)
......
...@@ -5,7 +5,7 @@ module Gitlab ...@@ -5,7 +5,7 @@ module Gitlab
class BaseStage class BaseStage
include BaseQuery include BaseQuery
def initialize(project:, options:) def initialize(project: nil, options:)
@project = project @project = project
@options = options @options = options
end end
...@@ -14,8 +14,8 @@ module Gitlab ...@@ -14,8 +14,8 @@ module Gitlab
event_fetcher.fetch event_fetcher.fetch
end end
def as_json def as_json(serializer: AnalyticsStageSerializer)
AnalyticsStageSerializer.new.represent(self) serializer.new.represent(self)
end end
def title def title
...@@ -24,20 +24,11 @@ module Gitlab ...@@ -24,20 +24,11 @@ module Gitlab
def median def median
BatchLoader.for(@project.id).batch(key: name) do |project_ids, loader| BatchLoader.for(@project.id).batch(key: name) do |project_ids, loader|
cte_table = Arel::Table.new("cte_table_for_#{name}")
# Build a `SELECT` query. We find the first of the `end_time_attrs` that isn't `NULL` (call this end_time).
# Next, we find the first of the start_time_attrs that isn't `NULL` (call this start_time).
# We compute the (end_time - start_time) interval, and give it an alias based on the current
# cycle analytics stage.
interval_query = Arel::Nodes::As.new(cte_table,
subtract_datetimes(stage_query(project_ids), start_time_attrs, end_time_attrs, name.to_s))
if project_ids.one? if project_ids.one?
loader.call(@project.id, median_datetime(cte_table, interval_query, name)) loader.call(@project.id, median_query(project_ids))
else else
begin begin
median_datetimes(cte_table, interval_query, name, :project_id)&.each do |project_id, median| median_datetimes(cte_table, interval_query(project_ids), name, :project_id)&.each do |project_id, median|
loader.call(project_id, median) loader.call(project_id, median)
end end
rescue NotSupportedError rescue NotSupportedError
...@@ -47,10 +38,28 @@ module Gitlab ...@@ -47,10 +38,28 @@ module Gitlab
end end
end end
def median_query(project_ids)
# Build a `SELECT` query. We find the first of the `end_time_attrs` that isn't `NULL` (call this end_time).
# Next, we find the first of the start_time_attrs that isn't `NULL` (call this start_time).
# We compute the (end_time - start_time) interval, and give it an alias based on the current
# cycle analytics stage.
median_datetime(cte_table, interval_query(project_ids), name)
end
def name def name
raise NotImplementedError.new("Expected #{self.name} to implement name") raise NotImplementedError.new("Expected #{self.name} to implement name")
end end
def cte_table
Arel::Table.new("cte_table_for_#{name}")
end
def interval_query(project_ids)
Arel::Nodes::As.new(cte_table,
subtract_datetimes(stage_query(project_ids), start_time_attrs, end_time_attrs, name.to_s))
end
private private
def event_fetcher def event_fetcher
...@@ -62,6 +71,10 @@ module Gitlab ...@@ -62,6 +71,10 @@ module Gitlab
def event_options def event_options
@options.merge(start_time_attrs: start_time_attrs, end_time_attrs: end_time_attrs) @options.merge(start_time_attrs: start_time_attrs, end_time_attrs: end_time_attrs)
end end
def projects
[@project]
end
end end
end end
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