summaryrefslogtreecommitdiff
path: root/app/models/cycle_analytics/queries.rb
blob: f14f44c387625bbdf14a5ebd12fee9f6ebd92599 (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
class CycleAnalytics
  module Queries
    class << self
      def issues
        Issue.all.to_a.map { |issue| { issue: issue } }
      end

      def merge_requests_closing_issues
        issues.map do |data_point|
          merge_requests = data_point[:issue].closed_by_merge_requests(nil, check_if_open: false)
          merge_requests.map { |merge_request| { issue: data_point[:issue], merge_request: merge_request } }
        end.flatten
      end

      def issue_first_associated_with_milestone_or_first_added_to_list_label_time
        lambda do |data_point|
          issue = data_point[:issue]
          if issue.metrics.present?
            issue.metrics.first_associated_with_milestone_at.presence ||
              issue.metrics.first_added_to_board_at.presence
          end
        end
      end

      def mr_first_closed_or_merged_at
        lambda do |data_point|
          merge_request = data_point[:merge_request]
          if merge_request.metrics.present?
            merge_request.metrics.merged_at.presence || merge_request.metrics.first_closed_at.presence
          end
        end
      end

      def mr_merged_at
        lambda do |data_point|
          merge_request = data_point[:merge_request]
          if merge_request.metrics.present?
            merge_request.metrics.merged_at
          end
        end
      end

      def mr_deployed_to_any_environment_at
        lambda do |data_point|
          merge_request = data_point[:merge_request]
          if merge_request.metrics.present?
            deployments = Deployment.where(ref: merge_request.target_branch).where("created_at > ?", merge_request.metrics.merged_at)
            deployment = deployments.order(:created_at).first
            deployment.created_at if deployment
          end
        end
      end

      def mr_deployed_to_production_at
        lambda do |data_point|
          merge_request = data_point[:merge_request]
          if merge_request.metrics.present?
            deployments = Deployment.joins(:environment).where(ref: merge_request.target_branch, "environments.name" => "production").
                          where("deployments.created_at > ?", merge_request.metrics.merged_at)
            deployment = deployments.order(:created_at).first
            deployment.created_at if deployment
          end
        end
      end

      def issue_closing_merge_request_opened_time
        lambda do |data_point|
          issue = data_point[:issue]
          merge_requests = issue.closed_by_merge_requests(nil, check_if_open: false)
          merge_requests.map(&:created_at).min if merge_requests.present?
        end
      end

      def mr_wip_flag_removed_or_assigned_to_user_other_than_author_time
        lambda do |data_point|
          merge_request = data_point[:merge_request]
          if merge_request.metrics.present?
            merge_request.metrics.wip_flag_first_removed_at.presence ||
              merge_request.metrics.first_assigned_to_user_other_than_author.presence
          end
        end
      end
    end
  end
end