summaryrefslogtreecommitdiff
path: root/lib/gitlab/cycle_analytics/events_fetcher.rb
blob: 2686ac353d63c3f213297e292e94a5174656c78e (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
module Gitlab
  module CycleAnalytics
    class EventsFetcher
      include MetricsFetcher

      EVENTS_CONFIG = {
        issue: {
          start_time_attrs: issue_table[:created_at],
          end_time_attrs: [issue_metrics_table[:first_associated_with_milestone_at],
                           issue_metrics_table[:first_added_to_board_at]],
          projections: [issue_table[:title], issue_table[:iid], issue_table[:created_at], user_table[:name], user_table[:email]]
        },
        plan: {
          start_time_attrs: issue_metrics_table[:first_associated_with_milestone_at],
          end_time_attrs: [issue_metrics_table[:first_added_to_board_at],
                           issue_metrics_table[:first_mentioned_in_commit_at]],
          projections: [mr_diff_table[:st_commits].as('commits'), issue_metrics_table[:first_mentioned_in_commit_at]]
        },
        code: {
          start_time_attrs: issue_metrics_table[:first_mentioned_in_commit_at],
          end_time_attrs: mr_table[:created_at],
          projections: [mr_table[:title], mr_table[:iid], mr_table[:created_at], user_table[:name], user_table[:email]],
          order: mr_table[:created_at]
        },
        test: {
          start_time_attrs: mr_metrics_table[:latest_build_started_at],
          end_time_attrs: mr_metrics_table[:latest_build_finished_at],
          projections: mr_metrics_table[:ci_commit_id],
          order: mr_table[:created_at]
        },
        review: {
          start_time_attrs: mr_table[:created_at],
          end_time_attrs: mr_metrics_table[:merged_at],
          projections: [mr_table[:title], mr_table[:iid], mr_table[:created_at], user_table[:name], user_table[:email]]
        },
        staging: {
          start_time_attrs: mr_metrics_table[:merged_at],
          end_time_attrs: mr_metrics_table[:first_deployed_to_production_at],
          projections: mr_metrics_table[:ci_commit_id]
        },
        production: {
          start_time_attrs: issue_table[:created_at],
          end_time_attrs: mr_metrics_table[:first_deployed_to_production_at],
          projections: [issue_table[:title], issue_table[:iid], issue_table[:created_at], user_table[:name], user_table[:email]]
        },
      }.freeze

      def initialize(project:, from:)
        @query = EventsQuery.new(project: project, from: from)
      end

      def fetch(stage:)
        custom_query = "#{stage}_custom_query".to_sym

        @query.execute(stage, EVENTS_CONFIG[stage]) do |base_query|
          public_send(custom_query, base_query) if self.respond_to?(custom_query)
        end
      end

      def issue_custom_query(base_query)
        base_query.join(user_table).on(issue_table[:author_id].eq(user_table[:id]))
      end

      alias_method :code_custom_query, :issue_custom_query
      alias_method :production_custom_query, :issue_custom_query

      def plan_custom_query(base_query)
        base_query.join(mr_diff_table).on(mr_diff_table[:merge_request_id].eq(mr_table[:id]))
      end

      def review_custom_query(base_query)
        base_query.join(user_table).on(mr_table[:author_id].eq(user_table[:id]))
      end
    end
  end
end