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

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

module Gitlab
  module Usage
    module Metrics
      module Instrumentations
        class DatabaseMetric < BaseMetric
          # Usage Example
          #
          # class CountUsersCreatingIssuesMetric < DatabaseMetric
          #   operation :distinct_count, column: :author_id
          #
          #   relation do |database_time_constraints|
          #     ::Issue.where(database_time_constraints)
          #   end
          # end
          class << self
            def start(&block)
              @metric_start = block
            end

            def finish(&block)
              @metric_finish = block
            end

            def relation(&block)
              @metric_relation = block
            end

            def operation(symbol, column: nil)
              @metric_operation = symbol
              @column = column
            end

            attr_reader :metric_operation, :metric_relation, :metric_start, :metric_finish, :column
          end

          def value
            method(self.class.metric_operation)
              .call(relation,
                    self.class.column,
                    start: self.class.metric_start&.call,
                    finish: self.class.metric_finish&.call)
          end

          def relation
            self.class.metric_relation.call.where(time_constraints)
          end

          private

          def time_constraints
            case time_frame
            when '28d'
              { created_at: 30.days.ago..2.days.ago }
            when 'all'
              {}
            when 'none'
              nil
            else
              raise "Unknown time frame: #{time_frame} for DatabaseMetric"
            end
          end
        end
      end
    end
  end
end