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

CE port of https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/735
This commit is contained in:
Sean McGivern 2017-04-05 13:29:48 +01:00 committed by Rémy Coutable
parent ebd5e9b454
commit 0483019e98
10 changed files with 192 additions and 18 deletions

View file

@ -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);

View file

@ -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;

View file

@ -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!'

View file

@ -492,9 +492,11 @@
= f.label :usage_ping_enabled do
= f.check_box :usage_ping_enabled
Usage ping enabled
.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.
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

View file

@ -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

View file

@ -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

57
lib/gitlab/usage_data.rb Normal file
View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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)