Commit 0483019e authored by Sean McGivern's avatar Sean McGivern Committed by Rémy Coutable

Port 'Add more usage data to EE ping' to CE

CE port of https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/735
parent ebd5e9b4
(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);
...@@ -365,6 +365,9 @@ const ShortcutsBlob = require('./shortcuts_blob'); ...@@ -365,6 +365,9 @@ const ShortcutsBlob = require('./shortcuts_blob');
case 'admin': case 'admin':
new Admin(); new Admin();
switch (path[1]) { switch (path[1]) {
case 'application_settings':
new gl.ApplicationSettings();
break;
case 'groups': case 'groups':
new UsersSelect(); new UsersSelect();
break; break;
......
...@@ -17,6 +17,13 @@ class Admin::ApplicationSettingsController < Admin::ApplicationController ...@@ -17,6 +17,13 @@ class Admin::ApplicationSettingsController < Admin::ApplicationController
end end
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 def reset_runners_token
@application_setting.reset_runners_registration_token! @application_setting.reset_runners_registration_token!
flash[:notice] = 'New runners registration token has been generated!' flash[:notice] = 'New runners registration token has been generated!'
......
...@@ -492,9 +492,11 @@ ...@@ -492,9 +492,11 @@
= f.label :usage_ping_enabled do = f.label :usage_ping_enabled do
= f.check_box :usage_ping_enabled = f.check_box :usage_ping_enabled
Usage ping enabled Usage ping enabled
.help-block .container
Every week GitLab will report license usage back to GitLab, Inc. .help-block
Disable this option if you do not want this to occur. 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 %fieldset
%legend Email %legend Email
......
...@@ -15,7 +15,7 @@ class GitlabUsagePingWorker ...@@ -15,7 +15,7 @@ class GitlabUsagePingWorker
begin begin
HTTParty.post(url, HTTParty.post(url,
body: data.to_json, body: Gitlab::UsageData.to_json,
headers: { 'Content-type' => 'application/json' } headers: { 'Content-type' => 'application/json' }
) )
rescue HTTParty::Error => e rescue HTTParty::Error => e
...@@ -27,13 +27,6 @@ class GitlabUsagePingWorker ...@@ -27,13 +27,6 @@ class GitlabUsagePingWorker
Gitlab::ExclusiveLease.new('gitlab_usage_ping_worker:ping', timeout: LEASE_TIMEOUT).try_obtain Gitlab::ExclusiveLease.new('gitlab_usage_ping_worker:ping', timeout: LEASE_TIMEOUT).try_obtain
end end
def data
usage_data = { version: Gitlab::VERSION,
active_user_count: User.active.acount }
usage_data
end
def url def url
'https://version.gitlab.com/usage_data' 'https://version.gitlab.com/usage_data'
end end
......
...@@ -91,6 +91,7 @@ namespace :admin do ...@@ -91,6 +91,7 @@ namespace :admin do
resource :application_settings, only: [:show, :update] do resource :application_settings, only: [:show, :update] do
resources :services, only: [:index, :edit, :update] resources :services, only: [:index, :edit, :update]
get :usage_data
put :reset_runners_token put :reset_runners_token
put :reset_health_check_token put :reset_health_check_token
put :clear_repository_check_states put :clear_repository_check_states
......
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
...@@ -3,12 +3,49 @@ require 'spec_helper' ...@@ -3,12 +3,49 @@ require 'spec_helper'
describe Admin::ApplicationSettingsController do describe Admin::ApplicationSettingsController do
include StubENV include StubENV
let(:group) { create(:group) }
let(:project) { create(:project, namespace: group) }
let(:admin) { create(:admin) } let(:admin) { create(:admin) }
let(:user) { create(:user)}
before do before do
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false') stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
end 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 describe 'PUT #update' do
before do before do
sign_in(admin) sign_in(admin)
......
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
...@@ -3,13 +3,6 @@ require 'spec_helper' ...@@ -3,13 +3,6 @@ require 'spec_helper'
describe GitlabUsagePingWorker do describe GitlabUsagePingWorker do
subject { GitlabUsagePingWorker.new } 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 it "sends POST request" do
stub_application_setting(usage_ping_enabled: true) stub_application_setting(usage_ping_enabled: true)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment