summaryrefslogtreecommitdiff
path: root/app/models/cycle_analytics.rb
blob: 8ed4a56b19b86443aa08981f51d80511f1f1b5df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
class CycleAnalytics
  include Gitlab::Database::Median
  include Gitlab::Database::DateTime

  DEPLOYMENT_METRIC_STAGES = %i[production staging]

  def initialize(project, from:)
    @project = project
    @from = from
  end

  def summary
    @summary ||= Summary.new(@project, from: @from)
  end

  def issue
    calculate_metric(:issue,
                     Issue.arel_table[:created_at],
                     [Issue::Metrics.arel_table[:first_associated_with_milestone_at],
                      Issue::Metrics.arel_table[:first_added_to_board_at]])
  end

  def plan
    calculate_metric(:plan,
                     [Issue::Metrics.arel_table[:first_associated_with_milestone_at],
                      Issue::Metrics.arel_table[:first_added_to_board_at]],
                     Issue::Metrics.arel_table[:first_mentioned_in_commit_at])
  end

  def code
    calculate_metric(:code,
                     Issue::Metrics.arel_table[:first_mentioned_in_commit_at],
                     MergeRequest.arel_table[:created_at])
  end

  def test
    calculate_metric(:test,
                     MergeRequest::Metrics.arel_table[:latest_build_started_at],
                     MergeRequest::Metrics.arel_table[:latest_build_finished_at])
  end

  def review
    calculate_metric(:review,
                     MergeRequest.arel_table[:created_at],
                     MergeRequest::Metrics.arel_table[:merged_at])
  end

  def staging
    calculate_metric(:staging,
                     MergeRequest::Metrics.arel_table[:merged_at],
                     MergeRequest::Metrics.arel_table[:first_deployed_to_production_at])
  end

  def production
    calculate_metric(:production,
                     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