2016-11-18 15:38:02 +00:00
|
|
|
module Gitlab
|
|
|
|
module CycleAnalytics
|
|
|
|
class BaseStage
|
2016-12-09 11:41:15 +00:00
|
|
|
include BaseQuery
|
2016-11-21 16:15:25 +00:00
|
|
|
|
2016-12-01 10:21:24 +00:00
|
|
|
def initialize(project:, options:)
|
2016-11-18 15:38:02 +00:00
|
|
|
@project = project
|
|
|
|
@options = options
|
2016-12-02 16:09:29 +00:00
|
|
|
end
|
|
|
|
|
2016-11-18 15:38:02 +00:00
|
|
|
def events
|
2017-01-12 11:48:01 +00:00
|
|
|
event_fetcher.fetch
|
2016-11-18 15:38:02 +00:00
|
|
|
end
|
|
|
|
|
2017-01-12 11:48:01 +00:00
|
|
|
def as_json
|
2017-01-27 13:37:14 +00:00
|
|
|
AnalyticsStageSerializer.new.represent(self)
|
2016-11-22 09:33:19 +00:00
|
|
|
end
|
|
|
|
|
2016-12-01 10:21:24 +00:00
|
|
|
def title
|
2016-12-09 11:41:15 +00:00
|
|
|
name.to_s.capitalize
|
2016-12-01 10:21:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def median
|
2016-12-09 11:41:15 +00:00
|
|
|
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,
|
2017-01-12 11:48:01 +00:00
|
|
|
subtract_datetimes(base_query.dup, start_time_attrs, end_time_attrs, name.to_s))
|
2016-12-09 11:41:15 +00:00
|
|
|
|
|
|
|
median_datetime(cte_table, interval_query, name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
|
|
|
raise NotImplementedError.new("Expected #{self.name} to implement name")
|
2016-12-01 10:21:24 +00:00
|
|
|
end
|
|
|
|
|
2016-11-18 15:38:02 +00:00
|
|
|
private
|
|
|
|
|
2017-01-12 11:48:01 +00:00
|
|
|
def event_fetcher
|
|
|
|
@event_fetcher ||= Gitlab::CycleAnalytics::EventFetcher[name].new(project: @project,
|
2017-01-12 13:32:30 +00:00
|
|
|
stage: name,
|
|
|
|
options: event_options)
|
2017-01-12 11:48:01 +00:00
|
|
|
end
|
|
|
|
|
2016-12-09 11:41:15 +00:00
|
|
|
def event_options
|
2017-01-12 11:48:01 +00:00
|
|
|
@options.merge(start_time_attrs: start_time_attrs, end_time_attrs: end_time_attrs)
|
2016-11-18 15:38:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|