summaryrefslogtreecommitdiff
path: root/lib/gitlab/usage/metrics/instrumentations/count_imported_projects_total_metric.rb
blob: 109d22456352c19005db36ca1ed5b9081150a63d (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
# frozen_string_literal: true

module Gitlab
  module Usage
    module Metrics
      module Instrumentations
        class CountImportedProjectsTotalMetric < DatabaseMetric
          # Relation and operation are not used, but are included to satisfy expectations
          # of other metric generation logic.
          relation { Project }
          operation :count

          IMPORT_TYPES = %w(gitlab_project gitlab github bitbucket bitbucket_server gitea git manifest
                            gitlab_migration).freeze

          def value
            count(project_relation) + count(entity_relation)
          end

          def to_sql
            project_relation_sql = Gitlab::Usage::Metrics::Query.for(:count, project_relation)
            entity_relation_sql = Gitlab::Usage::Metrics::Query.for(:count, entity_relation)

            "SELECT (#{project_relation_sql}) + (#{entity_relation_sql})"
          end

          private

          def project_relation
            Project.imported_from(IMPORT_TYPES).where(time_constraints)
          end

          def entity_relation
            BulkImports::Entity.where(source_type: :project_entity).where(time_constraints)
          end
        end
      end
    end
  end
end