summaryrefslogtreecommitdiff
path: root/lib/gitlab/cycle_analytics/base_stage.rb
diff options
context:
space:
mode:
authorJames Lopez <james@jameslopez.es>2016-12-09 12:41:15 +0100
committerJames Lopez <james@jameslopez.es>2017-01-17 11:32:55 +0100
commit982d5a050667c517bbc996a08ca0922f2c5fbfb4 (patch)
tree785ca35f5b8497fc1d946e77da1f22056a3b1b60 /lib/gitlab/cycle_analytics/base_stage.rb
parent834bcacbaec837d8ec0a269f111bca769843bcb4 (diff)
downloadgitlab-ce-982d5a050667c517bbc996a08ca0922f2c5fbfb4.tar.gz
refactored metrics fetcher - merged into stage and events
Diffstat (limited to 'lib/gitlab/cycle_analytics/base_stage.rb')
-rw-r--r--lib/gitlab/cycle_analytics/base_stage.rb36
1 files changed, 22 insertions, 14 deletions
diff --git a/lib/gitlab/cycle_analytics/base_stage.rb b/lib/gitlab/cycle_analytics/base_stage.rb
index c2605364ff0..afec16d1818 100644
--- a/lib/gitlab/cycle_analytics/base_stage.rb
+++ b/lib/gitlab/cycle_analytics/base_stage.rb
@@ -1,23 +1,17 @@
module Gitlab
module CycleAnalytics
class BaseStage
- include MetricsTables
-
- attr_accessor :start_time_attrs, :end_time_attrs
+ include BaseQuery
def initialize(project:, options:)
@project = project
@options = options
- @fetcher = Gitlab::CycleAnalytics::MetricsFetcher.new(project: project,
- from: options[:from],
- branch: options[:branch],
- stage: self)
end
def event
- @event ||= Gitlab::CycleAnalytics::Event[stage].new(fetcher: @fetcher,
- options: @options,
- stage: stage)
+ @event ||= Gitlab::CycleAnalytics::Event[name].new(project: @project,
+ stage: name,
+ options: event_options)
end
def events
@@ -29,17 +23,31 @@ module Gitlab
end
def title
- stage.to_s.capitalize
+ name.to_s.capitalize
end
def median
- @fetcher.median
+ 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(base_query, @start_time_attrs, @end_time_attrs, name.to_s))
+
+ median_datetime(cte_table, interval_query, name)
+ end
+
+ def name
+ raise NotImplementedError.new("Expected #{self.name} to implement name")
end
private
- def stage
- class_name_for('Stage')
+ def event_options
+ @options.merge(start_time_attrs: @start_time_attrs, end_time_attrs: @end_time_attrs)
end
end
end