summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean McGivern <sean@gitlab.com>2017-04-05 13:29:48 +0100
committerRémy Coutable <remy@rymai.me>2017-04-14 15:20:55 +0200
commit0483019e9800dc1b4ef4493890f815f047b7c04e (patch)
tree24430c787a2e69166ccd214cc1e15a489f32e9e9
parentebd5e9b4549ebc80155a5a8f139efdb40b6f8b12 (diff)
downloadgitlab-ce-0483019e9800dc1b4ef4493890f815f047b7c04e.tar.gz
Port 'Add more usage data to EE ping' to CE
CE port of https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/735
-rw-r--r--app/assets/javascripts/application_settings.js.es616
-rw-r--r--app/assets/javascripts/dispatcher.js3
-rw-r--r--app/controllers/admin/application_settings_controller.rb7
-rw-r--r--app/views/admin/application_settings/_form.html.haml8
-rw-r--r--app/workers/gitlab_usage_ping_worker.rb9
-rw-r--r--config/routes/admin.rb1
-rw-r--r--lib/gitlab/usage_data.rb57
-rw-r--r--spec/controllers/admin/application_settings_controller_spec.rb37
-rw-r--r--spec/lib/gitlab/usage_data_spec.rb65
-rw-r--r--spec/workers/gitlab_usage_ping_worker_spec.rb7
10 files changed, 192 insertions, 18 deletions
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('<span')
+ expect(response.status).to eq(200)
+ end
+
+ it 'returns JSON data' do
+ get :usage_data, format: :json
+
+ body = JSON.parse(response.body)
+ expect(body["version"]).to eq(Gitlab::VERSION)
+ expect(body).to include('counts')
+ expect(response.status).to eq(200)
+ end
+ end
+
describe 'PUT #update' do
before do
sign_in(admin)
diff --git a/spec/lib/gitlab/usage_data_spec.rb b/spec/lib/gitlab/usage_data_spec.rb
new file mode 100644
index 00000000000..8d2f086f0f5
--- /dev/null
+++ b/spec/lib/gitlab/usage_data_spec.rb
@@ -0,0 +1,65 @@
+require 'spec_helper'
+
+describe Gitlab::UsageData do
+ let!(:project) { create(:empty_project) }
+ let!(:project2) { create(:empty_project) }
+ let!(:board) { create(:board, project: project) }
+
+ describe '#data' do
+ subject { Gitlab::UsageData.data }
+
+ it "gathers usage data" do
+ expect(subject.keys).to match_array(%i(
+ active_user_count
+ counts
+ version
+ recorded_at
+ ))
+ end
+
+ it "gathers usage counts" do
+ count_data = subject[:counts]
+
+ expect(count_data[:boards]).to eq(1)
+ expect(count_data[:projects]).to eq(2)
+
+ expect(count_data.keys).to match_array(%i(
+ boards
+ ci_builds
+ ci_pipelines
+ ci_runners
+ ci_triggers
+ deploy_keys
+ deployments
+ environments
+ groups
+ issues
+ keys
+ labels
+ lfs_objects
+ merge_requests
+ milestones
+ notes
+ projects
+ pushes
+ pages_domains
+ protected_branches
+ releases
+ services
+ snippets
+ todos
+ web_hooks
+ ))
+ end
+ end
+
+ describe '#license_usage_data' do
+ subject { Gitlab::UsageData.license_usage_data }
+
+ it "gathers license data" do
+ expect(subject[:version]).to eq(Gitlab::VERSION)
+ expect(subject[:active_user_count]).to eq(User.active.count)
+ expect(subject[:recorded_at]).to be_a(Time)
+ end
+ end
+end
diff --git a/spec/workers/gitlab_usage_ping_worker_spec.rb b/spec/workers/gitlab_usage_ping_worker_spec.rb
index 8c5c027ac29..8821df3d81e 100644
--- a/spec/workers/gitlab_usage_ping_worker_spec.rb
+++ b/spec/workers/gitlab_usage_ping_worker_spec.rb
@@ -3,13 +3,6 @@ require 'spec_helper'
describe GitlabUsagePingWorker do
subject { GitlabUsagePingWorker.new }
- it "gathers license data" do
- data = subject.data
-
- expect(data[:version]).to eq(Gitlab::VERSION)
- expect(data[:active_user_count]).to eq(User.active.count)
- end
-
it "sends POST request" do
stub_application_setting(usage_ping_enabled: true)