From 0483019e9800dc1b4ef4493890f815f047b7c04e Mon Sep 17 00:00:00 2001 From: Sean McGivern Date: Wed, 5 Apr 2017 13:29:48 +0100 Subject: [PATCH] Port 'Add more usage data to EE ping' to CE CE port of https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/735 --- .../javascripts/application_settings.js.es6 | 16 +++++ app/assets/javascripts/dispatcher.js | 3 + .../admin/application_settings_controller.rb | 7 ++ .../application_settings/_form.html.haml | 8 ++- app/workers/gitlab_usage_ping_worker.rb | 9 +-- config/routes/admin.rb | 1 + lib/gitlab/usage_data.rb | 57 ++++++++++++++++ .../application_settings_controller_spec.rb | 37 +++++++++++ spec/lib/gitlab/usage_data_spec.rb | 65 +++++++++++++++++++ spec/workers/gitlab_usage_ping_worker_spec.rb | 7 -- 10 files changed, 192 insertions(+), 18 deletions(-) create mode 100644 app/assets/javascripts/application_settings.js.es6 create mode 100644 lib/gitlab/usage_data.rb create mode 100644 spec/lib/gitlab/usage_data_spec.rb diff --git a/app/assets/javascripts/application_settings.js.es6 b/app/assets/javascripts/application_settings.js.es6 new file mode 100644 index 00000000000..ce7d5129d8d --- /dev/null +++ b/app/assets/javascripts/application_settings.js.es6 @@ -0,0 +1,16 @@ +(global => { + global.gl = global.gl || {}; + + gl.ApplicationSettings = function() { + var usage_data_url = $('.usage-data').data('endpoint'); + + $.ajax({ + type: "GET", + url: usage_data_url, + dataType: "html", + success: function (html) { + $(".usage-data").html(html); + } + }); + }; +})(window); diff --git a/app/assets/javascripts/dispatcher.js b/app/assets/javascripts/dispatcher.js index f277e1dddc7..9d8f965dee0 100644 --- a/app/assets/javascripts/dispatcher.js +++ b/app/assets/javascripts/dispatcher.js @@ -365,6 +365,9 @@ const ShortcutsBlob = require('./shortcuts_blob'); case 'admin': new Admin(); switch (path[1]) { + case 'application_settings': + new gl.ApplicationSettings(); + break; case 'groups': new UsersSelect(); break; diff --git a/app/controllers/admin/application_settings_controller.rb b/app/controllers/admin/application_settings_controller.rb index 82b01be5a11..73b03b41594 100644 --- a/app/controllers/admin/application_settings_controller.rb +++ b/app/controllers/admin/application_settings_controller.rb @@ -17,6 +17,13 @@ class Admin::ApplicationSettingsController < Admin::ApplicationController end end + def usage_data + respond_to do |format| + format.html { render html: Gitlab::Highlight.highlight('payload.json', Gitlab::UsageData.to_json) } + format.json { render json: Gitlab::UsageData.to_json } + end + end + def reset_runners_token @application_setting.reset_runners_registration_token! flash[:notice] = 'New runners registration token has been generated!' diff --git a/app/views/admin/application_settings/_form.html.haml b/app/views/admin/application_settings/_form.html.haml index d8b6ce13ca4..f671af477ad 100644 --- a/app/views/admin/application_settings/_form.html.haml +++ b/app/views/admin/application_settings/_form.html.haml @@ -492,9 +492,11 @@ = f.label :usage_ping_enabled do = f.check_box :usage_ping_enabled Usage ping enabled - .help-block - Every week GitLab will report license usage back to GitLab, Inc. - Disable this option if you do not want this to occur. + .container + .help-block + Every week GitLab will report license usage back to GitLab, Inc. + Disable this option if you do not want this to occur. This is the JSON payload that will be sent: + %pre.usage-data.js-syntax-highlight.code.highlight{ "data-endpoint" => usage_data_admin_application_settings_path(format: :html) } %fieldset %legend Email diff --git a/app/workers/gitlab_usage_ping_worker.rb b/app/workers/gitlab_usage_ping_worker.rb index 2e039b7f3c5..866f5d03d8b 100644 --- a/app/workers/gitlab_usage_ping_worker.rb +++ b/app/workers/gitlab_usage_ping_worker.rb @@ -15,7 +15,7 @@ class GitlabUsagePingWorker begin HTTParty.post(url, - body: data.to_json, + body: Gitlab::UsageData.to_json, headers: { 'Content-type' => 'application/json' } ) rescue HTTParty::Error => e @@ -27,13 +27,6 @@ class GitlabUsagePingWorker Gitlab::ExclusiveLease.new('gitlab_usage_ping_worker:ping', timeout: LEASE_TIMEOUT).try_obtain end - def data - usage_data = { version: Gitlab::VERSION, - active_user_count: User.active.acount } - - usage_data - end - def url 'https://version.gitlab.com/usage_data' end diff --git a/config/routes/admin.rb b/config/routes/admin.rb index 486ce3c5c87..3c1c2ce2582 100644 --- a/config/routes/admin.rb +++ b/config/routes/admin.rb @@ -91,6 +91,7 @@ namespace :admin do resource :application_settings, only: [:show, :update] do resources :services, only: [:index, :edit, :update] + get :usage_data put :reset_runners_token put :reset_health_check_token put :clear_repository_check_states diff --git a/lib/gitlab/usage_data.rb b/lib/gitlab/usage_data.rb new file mode 100644 index 00000000000..234d8680545 --- /dev/null +++ b/lib/gitlab/usage_data.rb @@ -0,0 +1,57 @@ +module Gitlab + class UsageData + class << self + def data + Rails.cache.fetch('usage_data', expires_in: 1.hour) { uncached_data } + end + + def uncached_data + license_usage_data.merge(system_usage_data) + end + + def to_json + data.to_json + end + + def system_usage_data + { + counts: { + boards: Board.count, + ci_builds: ::Ci::Build.count, + ci_pipelines: ::Ci::Pipeline.count, + ci_runners: ::Ci::Runner.count, + ci_triggers: ::Ci::Trigger.count, + deploy_keys: DeployKey.count, + deployments: Deployment.count, + environments: Environment.count, + 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, + pushes: Event.code_push.count, + pages_domains: PagesDomain.count, + projects: Project.count, + protected_branches: ProtectedBranch.count, + releases: Release.count, + services: Service.where(active: true).count, + snippets: Snippet.count, + todos: Todo.count, + web_hooks: WebHook.count + } + } + end + + def license_usage_data + usage_data = { version: Gitlab::VERSION, + active_user_count: User.active.count, + recorded_at: Time.now } + + usage_data + end + end + end +end diff --git a/spec/controllers/admin/application_settings_controller_spec.rb b/spec/controllers/admin/application_settings_controller_spec.rb index 5dd8f66343f..2565622f8df 100644 --- a/spec/controllers/admin/application_settings_controller_spec.rb +++ b/spec/controllers/admin/application_settings_controller_spec.rb @@ -3,12 +3,49 @@ require 'spec_helper' describe Admin::ApplicationSettingsController do include StubENV + let(:group) { create(:group) } + let(:project) { create(:project, namespace: group) } let(:admin) { create(:admin) } + let(:user) { create(:user)} before do stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false') end + describe 'GET #usage_data with no access' do + before do + sign_in(user) + end + + it 'returns 404' do + get :usage_data, format: :html + + expect(response.status).to eq(404) + end + end + + describe 'GET #usage_data' do + before do + sign_in(admin) + end + + it 'returns HTML data' do + get :usage_data, format: :html + + expect(response.body).to start_with('