summaryrefslogtreecommitdiff
path: root/lib/gitlab/cycle_analytics/stage_summary.rb
blob: 564feb0319fc08be1c8ccfd63b2d979812e88952 (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
# frozen_string_literal: true

module Gitlab
  module CycleAnalytics
    class StageSummary
      def initialize(project, from:, to: nil, current_user:)
        @project = project
        @from = from
        @to = to
        @current_user = current_user
      end

      def data
        summary = [issue_stats]
        summary << commit_stats if user_has_sufficient_access?
        summary << deploy_stats
        summary << deployment_frequency_stats
      end

      private

      def issue_stats
        serialize(Summary::Issue.new(project: @project, from: @from, to: @to, current_user: @current_user))
      end

      def commit_stats
        serialize(Summary::Commit.new(project: @project, from: @from, to: @to))
      end

      def deployments_summary
        @deployments_summary ||=
          Summary::Deploy.new(project: @project, from: @from, to: @to)
      end

      def deploy_stats
        serialize deployments_summary
      end

      def deployment_frequency_stats
        serialize(
          Summary::DeploymentFrequency.new(
            deployments: deployments_summary.value,
            from: @from,
            to: @to),
          with_unit: true
        )
      end

      def user_has_sufficient_access?
        @project.team.member?(@current_user, Gitlab::Access::REPORTER)
      end

      def serialize(summary_object, with_unit: false)
        AnalyticsSummarySerializer.new.represent(
          summary_object, with_unit: with_unit)
      end
    end
  end
end