usage_data.rb 7.2 KB
Newer Older
Stan Hu's avatar
Stan Hu committed
1 2 3
module Gitlab
  class UsageData
    class << self
4
      def data(force_refresh: false)
5
        Rails.cache.fetch('usage_data', force: force_refresh, expires_in: 2.weeks) { uncached_data }
Stan Hu's avatar
Stan Hu committed
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)
Stan Hu's avatar
Stan Hu committed
13 14
      end

15 16
      def to_json(force_refresh: false)
        data(force_refresh: force_refresh).to_json
Stan Hu's avatar
Stan Hu committed
17 18
      end

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

        license = ::License.current

33 34
        usage_data[:edition] = license_edition(license)

35 36
        if license
          usage_data[:license_md5] = license.md5
37
          usage_data[:license_id] = license.license_id
38 39 40 41 42 43 44
          usage_data[:historical_max_users] = ::HistoricalData.max_historical_user_count
          usage_data[:licensee] = license.licensee
          usage_data[:license_user_count] = license.restricted_user_count
          usage_data[:license_starts_at] = license.starts_at
          usage_data[:license_expires_at] = license.expires_at
          usage_data[:license_plan] = license.plan
          usage_data[:license_add_ons] = license.add_ons
45
          usage_data[:license_trial] = license.trial?
46 47 48 49 50
        end

        usage_data
      end

51
      # rubocop:disable Metrics/AbcSize
Stan Hu's avatar
Stan Hu committed
52 53 54 55 56
      def system_usage_data
        {
          counts: {
            boards: Board.count,
            ci_builds: ::Ci::Build.count,
57 58
            ci_internal_pipelines: ::Ci::Pipeline.internal.count,
            ci_external_pipelines: ::Ci::Pipeline.external.count,
59 60
            ci_pipeline_config_auto_devops: ::Ci::Pipeline.auto_devops_source.count,
            ci_pipeline_config_repository: ::Ci::Pipeline.repository_source.count,
Stan Hu's avatar
Stan Hu committed
61 62
            ci_runners: ::Ci::Runner.count,
            ci_triggers: ::Ci::Trigger.count,
63
            ci_pipeline_schedules: ::Ci::PipelineSchedule.count,
64 65
            auto_devops_enabled: ::ProjectAutoDevops.enabled.count,
            auto_devops_disabled: ::ProjectAutoDevops.disabled.count,
Stan Hu's avatar
Stan Hu committed
66 67
            deploy_keys: DeployKey.count,
            deployments: Deployment.count,
68
            environments: ::Environment.count,
69
            epics: ::Epic.count,
70
            geo_nodes: GeoNode.count,
71 72 73
            clusters: ::Clusters::Cluster.count,
            clusters_enabled: ::Clusters::Cluster.enabled.count,
            clusters_disabled: ::Clusters::Cluster.disabled.count,
74
            clusters_platforms_gke: ::Clusters::Cluster.gcp_installed.enabled.count,
75 76 77 78 79
            clusters_platforms_user: ::Clusters::Cluster.user_provided.enabled.count,
            clusters_applications_helm: ::Clusters::Applications::Helm.installed.count,
            clusters_applications_ingress: ::Clusters::Applications::Ingress.installed.count,
            clusters_applications_prometheus: ::Clusters::Applications::Prometheus.installed.count,
            clusters_applications_runner: ::Clusters::Applications::Runner.installed.count,
80
            in_review_folder: ::Environment.in_review_folder.count,
Stan Hu's avatar
Stan Hu committed
81 82 83 84 85 86 87 88 89 90 91 92 93
            groups: Group.count,
            issues: Issue.count,
            keys: Key.count,
            labels: Label.count,
            ldap_group_links: LdapGroupLink.count,
            ldap_keys: LDAPKey.count,
            ldap_users: User.ldap.count,
            lfs_objects: LfsObject.count,
            merge_requests: MergeRequest.count,
            milestones: Milestone.count,
            notes: Note.count,
            pages_domains: PagesDomain.count,
            projects: Project.count,
94
            projects_imported_from_github: Project.where(import_type: 'github').count,
95
            projects_reporting_ci_cd_back_to_github: GithubService.without_defaults.active.count,
96
            projects_mirrored_with_pipelines_enabled: projects_mirrored_with_pipelines_enabled,
97
            protected_branches: ProtectedBranch.count,
Stan Hu's avatar
Stan Hu committed
98 99 100 101
            releases: Release.count,
            remote_mirrors: RemoteMirror.count,
            snippets: Snippet.count,
            todos: Todo.count,
102
            uploads: Upload.count,
Stan Hu's avatar
Stan Hu committed
103
            web_hooks: WebHook.count
104
          }.merge(service_desk_counts).merge(services_usage)
105 106 107 108
        }
      end

      def service_desk_counts
109
        return {} unless ::License.feature_available?(:service_desk)
110 111 112 113 114 115 116 117

        projects_with_service_desk = Project.where(service_desk_enabled: true)

        {
          service_desk_enabled_projects: projects_with_service_desk.count,
          service_desk_issues: Issue.where(project: projects_with_service_desk,
                                           author: User.support_bot,
                                           confidential: true).count
Stan Hu's avatar
Stan Hu committed
118 119 120
        }
      end

121
      def cycle_analytics_usage_data
122
        Gitlab::CycleAnalytics::UsageData.new.to_json
123 124
      end

125 126 127
      def features_usage_data
        features_usage_data_ce.merge(features_usage_data_ee)
      end
Z.J. van de Weg's avatar
Z.J. van de Weg committed
128

129 130
      def features_usage_data_ce
        {
131
          signup: Gitlab::CurrentSettings.allow_signup?,
132
          ldap: Gitlab.config.ldap.enabled,
133
          gravatar: Gitlab::CurrentSettings.gravatar_enabled?,
134 135 136 137 138 139
          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
        }
      end
Stan Hu's avatar
Stan Hu committed
140

141 142
      def features_usage_data_ee
        {
143
          elasticsearch: Gitlab::CurrentSettings.elasticsearch_search?,
144 145 146
          geo: Gitlab::Geo.enabled?
        }
      end
Stan Hu's avatar
Stan Hu committed
147

148 149 150 151 152 153
      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 }
        }
Stan Hu's avatar
Stan Hu committed
154
      end
Sean McGivern's avatar
Sean McGivern committed
155

156 157 158 159
      def license_edition(license)
        return 'EE Free' unless license

        case license.plan
160 161
        when 'ultimate'
          'EEU'
Sean McGivern's avatar
Sean McGivern committed
162 163 164 165
        when 'premium'
          'EEP'
        when 'starter'
          'EES'
166
        else # Older licenses
Sean McGivern's avatar
Sean McGivern committed
167 168
          'EE'
        end
169 170
      end

171 172 173 174 175 176 177 178 179 180
      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  }
Sean McGivern's avatar
Sean McGivern committed
181
      end
182 183 184 185 186 187 188 189 190 191

      def projects_mirrored_with_pipelines_enabled
        Project.joins(:project_feature).where(
          mirror: true,
          mirror_trigger_builds: true,
          project_features: {
            builds_access_level: ProjectFeature::ENABLED
          }
        ).count
      end
Stan Hu's avatar
Stan Hu committed
192 193 194
    end
  end
end