Welcome to mirror list, hosted at ThFree Co, Russian Federation.

alert_status_counts.rb « alert_management « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 382026236e02ab9dd1bb2fafec3b92b872ee70f6 (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
# frozen_string_literal: true

module Gitlab
  module AlertManagement
    # Represents counts of each status or category of statuses
    class AlertStatusCounts
      include Gitlab::Utils::StrongMemoize

      STATUSES = ::AlertManagement::Alert::STATUSES

      attr_reader :project

      def self.declarative_policy_class
        'AlertManagement::AlertPolicy'
      end

      def initialize(current_user, project, params)
        @project = project
        @current_user = current_user
        @params = params
      end

      # Define method for each status
      STATUSES.each_key do |status|
        define_method(status) { counts[status] }
      end

      def open
        counts[:triggered] + counts[:acknowledged]
      end

      def all
        counts.values.sum # rubocop:disable CodeReuse/ActiveRecord
      end

      private

      attr_reader :current_user, :params

      def counts
        strong_memoize(:counts) do
          Hash.new(0).merge(counts_by_status)
        end
      end

      def counts_by_status
        ::AlertManagement::AlertsFinder
          .counts_by_status(current_user, project, params)
          .transform_keys { |status_id| STATUSES.key(status_id) }
      end
    end
  end
end