usage_data.rb 2.9 KB
Newer Older
1 2 3
module Gitlab
  class UsageData
    class << self
4 5
      include Gitlab::CurrentSettings

6 7
      def data(force_refresh: false)
        Rails.cache.fetch('usage_data', force: force_refresh, expires_in: 2.weeks) { uncached_data }
8 9 10 11 12 13
      end

      def uncached_data
        license_usage_data.merge(system_usage_data)
      end

14 15
      def to_json(force_refresh: false)
        data(force_refresh: force_refresh).to_json
16 17 18 19 20 21 22
      end

      def system_usage_data
        {
          counts: {
            boards: Board.count,
            ci_builds: ::Ci::Build.count,
23 24
            ci_internal_pipelines: ::Ci::Pipeline.internal.count,
            ci_external_pipelines: ::Ci::Pipeline.external.count,
25 26
            ci_pipeline_config_auto_devops: ::Ci::Pipeline.auto_devops_source.count,
            ci_pipeline_config_repository: ::Ci::Pipeline.repository_source.count,
27 28
            ci_runners: ::Ci::Runner.count,
            ci_triggers: ::Ci::Trigger.count,
29
            ci_pipeline_schedules: ::Ci::PipelineSchedule.count,
30 31
            auto_devops_enabled: ::ProjectAutoDevops.enabled.count,
            auto_devops_disabled: ::ProjectAutoDevops.disabled.count,
32 33
            deploy_keys: DeployKey.count,
            deployments: Deployment.count,
34 35
            environments: ::Environment.count,
            in_review_folder: ::Environment.in_review_folder.count,
36 37 38 39 40 41 42 43 44 45
            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,
46
            projects_imported_from_github: Project.where(import_type: 'github').count,
47 48 49 50
            protected_branches: ProtectedBranch.count,
            releases: Release.count,
            snippets: Snippet.count,
            todos: Todo.count,
R
Robert Speicher 已提交
51
            uploads: Upload.count,
52
            web_hooks: WebHook.count
53
          }.merge(services_usage)
54 55 56 57
        }
      end

      def license_usage_data
S
Sean McGivern 已提交
58 59
        usage_data = {
          uuid: current_application_settings.uuid,
S
Sean McGivern 已提交
60
          hostname: Gitlab.config.gitlab.host,
S
Sean McGivern 已提交
61 62 63 64 65 66
          version: Gitlab::VERSION,
          active_user_count: User.active.count,
          recorded_at: Time.now,
          mattermost_enabled: Gitlab.config.mattermost.enabled,
          edition: 'CE'
        }
67 68 69

        usage_data
      end
70 71 72 73 74 75 76 77 78 79 80 81

      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
82 83 84
    end
  end
end