usage_data.rb 4.8 KB
Newer Older
1 2 3
module Gitlab
  class UsageData
    class << self
4 5
      def data(force_refresh: false)
        Rails.cache.fetch('usage_data', force: force_refresh, expires_in: 2.weeks) { uncached_data }
6 7 8 9
      end

      def uncached_data
        license_usage_data.merge(system_usage_data)
10 11
                          .merge(features_usage_data)
                          .merge(components_usage_data)
12
                          .merge(cycle_analytics_usage_data)
13 14
      end

15 16
      def to_json(force_refresh: false)
        data(force_refresh: force_refresh).to_json
17 18
      end

19 20
      def license_usage_data
        usage_data = {
21
          uuid: Gitlab::CurrentSettings.uuid,
22 23 24 25 26 27 28 29 30 31 32
          hostname: Gitlab.config.gitlab.host,
          version: Gitlab::VERSION,
          active_user_count: User.active.count,
          recorded_at: Time.now,
          mattermost_enabled: Gitlab.config.mattermost.enabled,
          edition: 'CE'
        }

        usage_data
      end

33
      # rubocop:disable Metrics/AbcSize
34 35 36 37 38
      def system_usage_data
        {
          counts: {
            boards: Board.count,
            ci_builds: ::Ci::Build.count,
39 40
            ci_internal_pipelines: ::Ci::Pipeline.internal.count,
            ci_external_pipelines: ::Ci::Pipeline.external.count,
41 42
            ci_pipeline_config_auto_devops: ::Ci::Pipeline.auto_devops_source.count,
            ci_pipeline_config_repository: ::Ci::Pipeline.repository_source.count,
43 44
            ci_runners: ::Ci::Runner.count,
            ci_triggers: ::Ci::Trigger.count,
45
            ci_pipeline_schedules: ::Ci::PipelineSchedule.count,
46 47
            auto_devops_enabled: ::ProjectAutoDevops.enabled.count,
            auto_devops_disabled: ::ProjectAutoDevops.disabled.count,
48 49
            deploy_keys: DeployKey.count,
            deployments: Deployment.count,
50
            environments: ::Environment.count,
51 52 53
            clusters: ::Clusters::Cluster.count,
            clusters_enabled: ::Clusters::Cluster.enabled.count,
            clusters_disabled: ::Clusters::Cluster.disabled.count,
54 55 56 57 58 59
            clusters_platforms_gke: ::Clusters::Cluster.where(provider_type: ::Clusters::Cluster.provider_types[:gcp]).count,
            clusters_platforms_user: ::Clusters::Cluster.where(provider_type: ::Clusters::Cluster.provider_types[:user]).count,
            clusters_applications_helm: ::Clusters::Applications::Helm.count,
            clusters_applications_ingress: ::Clusters::Applications::Ingress.count,
            clusters_applications_prometheus: ::Clusters::Applications::Prometheus.count,
            clusters_applications_runner: ::Clusters::Applications::Runner.count,
60
            in_review_folder: ::Environment.in_review_folder.count,
61 62 63 64 65 66 67 68 69 70
            groups: Group.count,
            issues: Issue.count,
            keys: Key.count,
            labels: Label.count,
            lfs_objects: LfsObject.count,
            merge_requests: MergeRequest.count,
            milestones: Milestone.count,
            notes: Note.count,
            pages_domains: PagesDomain.count,
            projects: Project.count,
71
            projects_imported_from_github: Project.where(import_type: 'github').count,
72 73 74 75
            protected_branches: ProtectedBranch.count,
            releases: Release.count,
            snippets: Snippet.count,
            todos: Todo.count,
R
Robert Speicher 已提交
76
            uploads: Upload.count,
77
            web_hooks: WebHook.count
78
          }.merge(services_usage)
79 80 81
        }
      end

82
      def cycle_analytics_usage_data
83
        Gitlab::CycleAnalytics::UsageData.new.to_json
84 85
      end

86 87 88 89 90 91
      def features_usage_data
        features_usage_data_ce
      end

      def features_usage_data_ce
        {
92
          signup: Gitlab::CurrentSettings.allow_signup?,
93
          ldap: Gitlab.config.ldap.enabled,
94
          gravatar: Gitlab::CurrentSettings.gravatar_enabled?,
95 96 97 98
          omniauth: Gitlab.config.omniauth.enabled,
          reply_by_email: Gitlab::IncomingEmail.enabled?,
          container_registry: Gitlab.config.registry.enabled,
          gitlab_shared_runners: Gitlab.config.gitlab_ci.shared_runners_enabled
S
Sean McGivern 已提交
99
        }
100
      end
101

102 103 104 105 106 107
      def components_usage_data
        {
          gitlab_pages: { enabled: Gitlab.config.pages.enabled, version: Gitlab::Pages::VERSION },
          git: { version: Gitlab::Git.version },
          database: { adapter: Gitlab::Database.adapter_name, version: Gitlab::Database.version }
        }
108
      end
109 110 111 112 113 114 115 116 117 118 119 120

      def services_usage
        types = {
          JiraService: :projects_jira_active,
          SlackService: :projects_slack_notifications_active,
          SlackSlashCommandsService: :projects_slack_slash_active,
          PrometheusService: :projects_prometheus_active
        }

        results = Service.unscoped.where(type: types.keys, active: true).group(:type).count
        results.each_with_object({}) { |(key, value), response| response[types[key.to_sym]] = value  }
      end
121 122 123
    end
  end
end