2016-10-21 11:01:53 -04:00
|
|
|
module Gitlab
|
|
|
|
module CycleAnalytics
|
|
|
|
class EventsQuery
|
2016-11-17 07:22:27 -05:00
|
|
|
attr_reader :project
|
2016-10-21 11:01:53 -04:00
|
|
|
|
2016-11-04 07:57:23 -04:00
|
|
|
def initialize(project:, options: {})
|
2016-10-21 11:01:53 -04:00
|
|
|
@project = project
|
2016-11-04 07:57:23 -04:00
|
|
|
@from = options[:from]
|
|
|
|
@branch = options[:branch]
|
2016-11-17 07:22:27 -05:00
|
|
|
@fetcher = Gitlab::CycleAnalytics::MetricsFetcher.new(project: project, from: @from, branch: @branch)
|
2016-10-21 11:01:53 -04:00
|
|
|
end
|
|
|
|
|
2016-11-17 07:22:27 -05:00
|
|
|
def execute(stage_class)
|
|
|
|
@stage_class = stage_class
|
2016-10-21 11:01:53 -04:00
|
|
|
|
2016-11-09 10:03:47 -05:00
|
|
|
ActiveRecord::Base.connection.exec_query(query.to_sql)
|
2016-10-21 11:01:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-11-17 07:22:27 -05:00
|
|
|
def query
|
|
|
|
base_query = @fetcher.base_query_for(@stage_class.stage)
|
|
|
|
diff_fn = @fetcher.subtract_datetimes_diff(base_query, @stage_class.start_time_attrs, @stage_class.end_time_attrs)
|
2016-10-21 11:01:53 -04:00
|
|
|
|
2016-11-17 12:00:37 -05:00
|
|
|
@stage_class.custom_query(base_query)
|
2016-10-21 11:01:53 -04:00
|
|
|
|
2016-11-17 07:22:27 -05:00
|
|
|
base_query.project(extract_epoch(diff_fn).as('total_time'), *@stage_class.projections).order(@stage_class.order.desc)
|
2016-10-21 11:01:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def extract_epoch(arel_attribute)
|
|
|
|
return arel_attribute unless Gitlab::Database.postgresql?
|
|
|
|
|
|
|
|
Arel.sql(%Q{EXTRACT(EPOCH FROM (#{arel_attribute.to_sql}))})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|