summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Lopez <james@jameslopez.es>2016-10-13 17:11:28 +0200
committerJames Lopez <james@jameslopez.es>2016-11-17 08:22:54 +0100
commitec7db295d230ba1cec0c75ae199d60bd8f89c1d6 (patch)
tree62b1412698ba2a7ed2e5fb3d3c687fc9cdf0d0ca
parent60306053a2a14ff881bb56eadd4968bc4d4f48dc (diff)
downloadgitlab-ce-ec7db295d230ba1cec0c75ae199d60bd8f89c1d6.tar.gz
Started refactoring stuff to add events to cycle analytics
- Refactored cycle analytics class to extract DB logic - Reuse logic in new events fetcher - Started adding cycle analytics events class and spec (still not functional)
-rw-r--r--app/models/cycle_analytics.rb49
-rw-r--r--lib/gitlab/cycle_analytics/events.rb15
-rw-r--r--lib/gitlab/cycle_analytics/events_fetcher.rb27
-rw-r--r--lib/gitlab/cycle_analytics/metrics_fetcher.rb72
-rw-r--r--spec/lib/gitlab/cycle_analytics/events_spec.rb26
5 files changed, 141 insertions, 48 deletions
diff --git a/app/models/cycle_analytics.rb b/app/models/cycle_analytics.rb
index 8ed4a56b19b..30ca3273a2c 100644
--- a/app/models/cycle_analytics.rb
+++ b/app/models/cycle_analytics.rb
@@ -1,8 +1,5 @@
class CycleAnalytics
- include Gitlab::Database::Median
- include Gitlab::Database::DateTime
-
- DEPLOYMENT_METRIC_STAGES = %i[production staging]
+ include Gitlab::CycleAnalytics::MetricsFetcher
def initialize(project, from:)
@project = project
@@ -56,48 +53,4 @@ class CycleAnalytics
Issue.arel_table[:created_at],
MergeRequest::Metrics.arel_table[:first_deployed_to_production_at])
end
-
- private
-
- def calculate_metric(name, start_time_attrs, end_time_attrs)
- 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_for(name), end_time_attrs, start_time_attrs, name.to_s))
-
- median_datetime(cte_table, interval_query, name)
- end
-
- # Join table with a row for every <issue,merge_request> pair (where the merge request
- # closes the given issue) with issue and merge request metrics included. The metrics
- # are loaded with an inner join, so issues / merge requests without metrics are
- # automatically excluded.
- def base_query_for(name)
- arel_table = MergeRequestsClosingIssues.arel_table
-
- # Load issues
- query = arel_table.join(Issue.arel_table).on(Issue.arel_table[:id].eq(arel_table[:issue_id])).
- join(Issue::Metrics.arel_table).on(Issue.arel_table[:id].eq(Issue::Metrics.arel_table[:issue_id])).
- where(Issue.arel_table[:project_id].eq(@project.id)).
- where(Issue.arel_table[:deleted_at].eq(nil)).
- where(Issue.arel_table[:created_at].gteq(@from))
-
- # Load merge_requests
- query = query.join(MergeRequest.arel_table, Arel::Nodes::OuterJoin).
- on(MergeRequest.arel_table[:id].eq(arel_table[:merge_request_id])).
- join(MergeRequest::Metrics.arel_table).
- on(MergeRequest.arel_table[:id].eq(MergeRequest::Metrics.arel_table[:merge_request_id]))
-
- if DEPLOYMENT_METRIC_STAGES.include?(name)
- # Limit to merge requests that have been deployed to production after `@from`
- query.where(MergeRequest::Metrics.arel_table[:first_deployed_to_production_at].gteq(@from))
- end
-
- query
- end
end
diff --git a/lib/gitlab/cycle_analytics/events.rb b/lib/gitlab/cycle_analytics/events.rb
new file mode 100644
index 00000000000..0cf3067d48e
--- /dev/null
+++ b/lib/gitlab/cycle_analytics/events.rb
@@ -0,0 +1,15 @@
+module Gitlab
+ module CycleAnalytics
+ class Events
+ def initialize(project:, from:)
+ @project = project
+ @from = from
+ @fetcher = EventsFetcher.new(project: project, from: from)
+ end
+
+ def issue_events
+ @fetcher.fetch_issues
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/cycle_analytics/events_fetcher.rb b/lib/gitlab/cycle_analytics/events_fetcher.rb
new file mode 100644
index 00000000000..9e62eef706c
--- /dev/null
+++ b/lib/gitlab/cycle_analytics/events_fetcher.rb
@@ -0,0 +1,27 @@
+module Gitlab
+ module CycleAnalytics
+ class EventsFetcher
+ include MetricsFetcher
+
+ def initialize(project:, from:)
+ @project = project
+ @from = from
+ end
+
+ def fetch_issues
+ cte_table = Arel::Table.new("cte_table_for_issue")
+
+ interval_query = Arel::Nodes::As.new(
+ cte_table,
+ subtract_datetimes(base_query_for(:issue), *attributes, 'issue'))
+
+ #TODO ActiveRecord::Base.connection.execute(interval_query)
+ end
+
+ def attributes
+ [issue_metrics_table[:first_associated_with_milestone_at],
+ issue_metrics_table[:first_added_to_board_at]]
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/cycle_analytics/metrics_fetcher.rb b/lib/gitlab/cycle_analytics/metrics_fetcher.rb
new file mode 100644
index 00000000000..51b4963cf08
--- /dev/null
+++ b/lib/gitlab/cycle_analytics/metrics_fetcher.rb
@@ -0,0 +1,72 @@
+module Gitlab
+ module CycleAnalytics
+ module MetricsFetcher
+ include Gitlab::Database::Median
+ include Gitlab::Database::DateTime
+
+ DEPLOYMENT_METRIC_STAGES = %i[production staging]
+
+ private
+
+ def calculate_metric(name, start_time_attrs, end_time_attrs)
+ 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_for(name), end_time_attrs, start_time_attrs, name.to_s))
+
+ median_datetime(cte_table, interval_query, name)
+ end
+
+ # Join table with a row for every <issue,merge_request> pair (where the merge request
+ # closes the given issue) with issue and merge request metrics included. The metrics
+ # are loaded with an inner join, so issues / merge requests without metrics are
+ # automatically excluded.
+ def base_query_for(name)
+ # Load issues
+ query = issue_metrics_table.join(issue_table).on(issue_table[:id].eq(mr_closing_issues_table[:issue_id])).
+ join(issue_metrics_table).on(issue_table[:id].eq(issue_metrics_table[:issue_id])).
+ where(issue_table[:project_id].eq(@project.id)).
+ where(issue_table[:deleted_at].eq(nil)).
+ where(issue_table[:created_at].gteq(@from))
+
+ # Load merge_requests
+ query = query.join(merge_request_table, Arel::Nodes::OuterJoin).
+ on(merge_request_table[:id].eq(mr_closing_issues_table[:merge_request_id])).
+ join(merge_request_metrics_table).
+ on(merge_request_table[:id].eq(merge_request_metrics_table[:merge_request_id]))
+
+ if DEPLOYMENT_METRIC_STAGES.include?(name)
+ # Limit to merge requests that have been deployed to production after `@from`
+ query.where(merge_request_metrics_table[:first_deployed_to_production_at].gteq(@from))
+ end
+
+ query
+ end
+
+ def merge_request_metrics_table
+ MergeRequest::Metrics.arel_table
+ end
+
+ def merge_request_table
+ MergeRequest.arel_table
+ end
+
+ def mr_closing_issues_table
+ MergeRequestsClosingIssues.arel_table
+ end
+
+ def issue_table
+ Issue.arel_table
+ end
+
+ def issue_metrics_table
+ Issue::Metrics.arel_table
+ end
+ end
+ end
+end
diff --git a/spec/lib/gitlab/cycle_analytics/events_spec.rb b/spec/lib/gitlab/cycle_analytics/events_spec.rb
new file mode 100644
index 00000000000..b68cc16f4e6
--- /dev/null
+++ b/spec/lib/gitlab/cycle_analytics/events_spec.rb
@@ -0,0 +1,26 @@
+require 'spec_helper'
+
+describe Gitlab::CycleAnalytics::Events do
+ let(:project) { create(:project) }
+ let(:from_date) { 10.days.ago }
+ let(:user) { create(:user, :admin) }
+
+ subject { described_class.new(project: project, from: from_date) }
+
+ before do
+ setup(context)
+ end
+
+ describe '#issue' do
+ let!(:context) { create(:issue, project: project) }
+
+ xit 'does something' do
+ expect(subject.issue_events).to eq([])
+ end
+ end
+
+ def setup(context)
+ create(:milestone, project: project)
+ create_merge_request_closing_issue(context)
+ end
+end