debian-mirror-gitlab/lib/gitlab/cycle_analytics/base_stage.rb

84 lines
2.3 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Gitlab
module CycleAnalytics
class BaseStage
include BaseQuery
2019-10-12 21:52:04 +05:30
include GroupProjectsProvider
2017-08-17 22:00:37 +05:30
2019-10-12 21:52:04 +05:30
attr_reader :options
2019-09-30 21:07:59 +05:30
2019-10-12 21:52:04 +05:30
def initialize(options:)
2017-08-17 22:00:37 +05:30
@options = options
end
def events
event_fetcher.fetch
end
2019-09-30 21:07:59 +05:30
def as_json(serializer: AnalyticsStageSerializer)
serializer.new.represent(self)
2017-08-17 22:00:37 +05:30
end
def title
raise NotImplementedError.new("Expected #{self.name} to implement title")
end
2019-10-12 21:52:04 +05:30
def project_median
2019-09-30 21:07:59 +05:30
return if project.nil?
2017-08-17 22:00:37 +05:30
2019-09-30 21:07:59 +05:30
BatchLoader.for(project.id).batch(key: name) do |project_ids, loader|
2018-03-27 19:54:05 +05:30
if project_ids.one?
2019-09-30 21:07:59 +05:30
loader.call(project.id, median_query(project_ids))
2018-03-27 19:54:05 +05:30
else
begin
2019-09-30 21:07:59 +05:30
median_datetimes(cte_table, interval_query(project_ids), name, :project_id)&.each do |project_id, median|
2018-03-27 19:54:05 +05:30
loader.call(project_id, median)
end
rescue NotSupportedError
{}
end
end
end
2017-08-17 22:00:37 +05:30
end
2019-10-12 21:52:04 +05:30
def group_median
median_query(projects.map(&:id))
end
2019-09-30 21:07:59 +05:30
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
2017-08-17 22:00:37 +05:30
def name
raise NotImplementedError.new("Expected #{self.name} to implement name")
end
2019-09-30 21:07:59 +05:30
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
2017-08-17 22:00:37 +05:30
private
def event_fetcher
2019-10-12 21:52:04 +05:30
@event_fetcher ||= Gitlab::CycleAnalytics::EventFetcher[name].new(stage: name,
2017-08-17 22:00:37 +05:30
options: event_options)
end
def event_options
2019-09-30 21:07:59 +05:30
options.merge(start_time_attrs: start_time_attrs, end_time_attrs: end_time_attrs)
end
2017-08-17 22:00:37 +05:30
end
end
end