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

alerts_finder.rb « prometheus « projects « finders « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2105516db5fb820f0ddefca94808190d62a55ac7 (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
# frozen_string_literal: true

module Projects
  module Prometheus
    # Find Prometheus alerts by +project+, +environment+, +id+,
    # or any combo thereof.
    #
    # Optionally filter by +metric+.
    #
    # Arguments:
    #   params:
    #     project: Project | integer
    #     environment: Environment | integer
    #     metric: PrometheusMetric | integer
    class AlertsFinder
      def initialize(params = {})
        unless params[:project] || params[:environment] || params[:id]
          raise ArgumentError,
            'Please provide one or more of the following params: :project, :environment, :id'
        end

        @params = params
      end

      # Find all matching alerts
      #
      # @return [ActiveRecord::Relation<PrometheusAlert>]
      def execute
        relation = by_project(PrometheusAlert)
        relation = by_environment(relation)
        relation = by_metric(relation)
        relation = by_id(relation)
        ordered(relation)
      end

      private

      attr_reader :params

      def by_project(relation)
        return relation unless params[:project]

        relation.for_project(params[:project])
      end

      def by_environment(relation)
        return relation unless params[:environment]

        relation.for_environment(params[:environment])
      end

      def by_metric(relation)
        return relation unless params[:metric]

        relation.for_metric(params[:metric])
      end

      def by_id(relation)
        return relation unless params[:id]

        relation.id_in(params[:id])
      end

      def ordered(relation)
        relation.order_by('id_asc')
      end
    end
  end
end