summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlivier Gonzalez <ogonzalez@gitlab.com>2018-11-06 10:33:33 -0500
committerVictor Zagorodny <vzagorodny@gitlab.com>2019-06-12 20:16:27 +0300
commit6dbc62cadb20dc923c1e3e886e0b652404c3fc05 (patch)
tree081569cb4ffaa570a3aca61467141b00346abb36
parent1dab293938905ac1f181ab69e7cc735b2814ebc5 (diff)
downloadgitlab-ce-6dbc62cadb20dc923c1e3e886e0b652404c3fc05.tar.gz
Add usage ping for reportsce-usage_ping_for_reports
Count number of reports generated (per type)
-rw-r--r--changelogs/unreleased/usage_ping_for_reports.yml5
-rw-r--r--db/migrate/20190612171003_add_index_on_job_artifacts_file_type.rb16
-rw-r--r--db/schema.rb2
-rw-r--r--lib/gitlab/usage_data.rb14
-rw-r--r--spec/factories/ci/job_artifacts.rb30
-rw-r--r--spec/lib/gitlab/usage_data_spec.rb40
-rw-r--r--spec/models/ci/job_artifact_spec.rb25
7 files changed, 129 insertions, 3 deletions
diff --git a/changelogs/unreleased/usage_ping_for_reports.yml b/changelogs/unreleased/usage_ping_for_reports.yml
new file mode 100644
index 00000000000..20e4aa550e7
--- /dev/null
+++ b/changelogs/unreleased/usage_ping_for_reports.yml
@@ -0,0 +1,5 @@
+---
+title: Add usage ping for reports.
+merge_request: 22750
+author:
+type: added
diff --git a/db/migrate/20190612171003_add_index_on_job_artifacts_file_type.rb b/db/migrate/20190612171003_add_index_on_job_artifacts_file_type.rb
new file mode 100644
index 00000000000..f5f82cda5ba
--- /dev/null
+++ b/db/migrate/20190612171003_add_index_on_job_artifacts_file_type.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+class AddIndexOnJobArtifactsFileType < ActiveRecord::Migration
+ include Gitlab::Database::MigrationHelpers
+
+ DOWNTIME = false
+
+ disable_ddl_transaction!
+
+ def up
+ add_concurrent_index :ci_job_artifacts, :file_type
+ end
+
+ def down
+ remove_concurrent_index :ci_job_artifacts, :file_type
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index c57a73c66da..a7de356eefb 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20190611161641) do
+ActiveRecord::Schema.define(version: 20190612171003) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
diff --git a/lib/gitlab/usage_data.rb b/lib/gitlab/usage_data.rb
index 9aa2e972adf..21c96174a7e 100644
--- a/lib/gitlab/usage_data.rb
+++ b/lib/gitlab/usage_data.rb
@@ -97,6 +97,7 @@ module Gitlab
}
.merge(services_usage)
.merge(approximate_counts)
+ .merge(reports_usage)
}.tap do |data|
if Feature.enabled?(:group_overview_security_dashboard)
data[:counts][:user_preferences] = user_preferences_usage
@@ -143,6 +144,13 @@ module Gitlab
end
# rubocop: disable CodeReuse/ActiveRecord
+ def reports_usage
+ results = count(::Ci::JobArtifact.with_reports.group(:file_type), fallback: Hash.new(-1))
+ ::Ci::JobArtifact.report_file_types.each_with_object({}) do |(type, id), response|
+ response[:"#{type}_reports"] = results[id] || 0
+ end
+ end
+
def services_usage
types = {
SlackService: :projects_slack_notifications_active,
@@ -175,7 +183,11 @@ module Gitlab
{} # augmented in EE
end
- def count(relation, fallback: -1)
+ def count(relation, fallback: -1, count_params: nil)
+ if count_params
+ return relation.count(count_params)
+ end
+
relation.count
rescue ActiveRecord::StatementInvalid
fallback
diff --git a/spec/factories/ci/job_artifacts.rb b/spec/factories/ci/job_artifacts.rb
index 542fa9775cd..69fb28daa0e 100644
--- a/spec/factories/ci/job_artifacts.rb
+++ b/spec/factories/ci/job_artifacts.rb
@@ -138,5 +138,35 @@ FactoryBot.define do
artifact.file_sha256 = Digest::SHA256.file(artifact.file.path).hexdigest
end
end
+
+ trait :sast do
+ file_type :sast
+ file_format :raw
+ end
+
+ trait :dependency_scanning do
+ file_type :dependency_scanning
+ file_format :raw
+ end
+
+ trait :container_scanning do
+ file_type :container_scanning
+ file_format :raw
+ end
+
+ trait :dast do
+ file_type :dast
+ file_format :raw
+ end
+
+ trait :license_management do
+ file_type :license_management
+ file_format :raw
+ end
+
+ trait :performance do
+ file_type :performance
+ file_format :raw
+ end
end
end
diff --git a/spec/lib/gitlab/usage_data_spec.rb b/spec/lib/gitlab/usage_data_spec.rb
index e44463dd767..a9c5e50ed78 100644
--- a/spec/lib/gitlab/usage_data_spec.rb
+++ b/spec/lib/gitlab/usage_data_spec.rb
@@ -127,6 +127,14 @@ describe Gitlab::UsageData do
todos
uploads
web_hooks
+ junit_reports
+ codequality_reports
+ sast_reports
+ dependency_scanning_reports
+ container_scanning_reports
+ dast_reports
+ license_management_reports
+ performance_reports
user_preferences
)
@@ -179,6 +187,38 @@ describe Gitlab::UsageData do
expect { subject }.not_to raise_error
end
+
+ context 'with reports' do
+ before do
+ create(:ci_empty_pipeline, project: projects[0]) do |pipeline|
+ create(:ci_build, pipeline: pipeline) do |build|
+ create(:ci_job_artifact, :junit, job: build)
+ create(:ci_job_artifact, :codequality, job: build)
+ create(:ci_job_artifact, :sast, job: build)
+ create(:ci_job_artifact, :dependency_scanning, job: build)
+ create(:ci_job_artifact, :container_scanning, job: build)
+ create(:ci_job_artifact, :dast, job: build)
+ create(:ci_job_artifact, :license_management, job: build)
+ end
+
+ create(:ci_build, pipeline: pipeline) do |build|
+ create(:ci_job_artifact, :junit, job: build)
+ end
+ end
+ end
+
+ it 'gathers reports usage data correctly' do
+ count_data = subject[:counts]
+ expect(count_data[:junit_reports]).to eq(2)
+ expect(count_data[:codequality_reports]).to eq(1)
+ expect(count_data[:sast_reports]).to eq(1)
+ expect(count_data[:dependency_scanning_reports]).to eq(1)
+ expect(count_data[:container_scanning_reports]).to eq(1)
+ expect(count_data[:dast_reports]).to eq(1)
+ expect(count_data[:license_management_reports]).to eq(1)
+ expect(count_data[:performance_reports]).to eq(0)
+ end
+ end
end
describe '#features_usage_data_ce' do
diff --git a/spec/models/ci/job_artifact_spec.rb b/spec/models/ci/job_artifact_spec.rb
index 1ba66565e03..5e79a601b0f 100644
--- a/spec/models/ci/job_artifact_spec.rb
+++ b/spec/models/ci/job_artifact_spec.rb
@@ -1,4 +1,4 @@
-# frozen_string_literal: true
+ # frozen_string_literal: true
require 'spec_helper'
@@ -176,6 +176,29 @@ describe Ci::JobArtifact do
end
end
+ describe '.report_file_types' do
+ EXPECTED_REPORT_TYPES =
+ %w[
+ junit
+ codequality
+ sast
+ dependency_scanning
+ container_scanning
+ dast
+ license_management
+ performance
+ ].freeze
+
+ subject { described_class.report_file_types }
+
+ it 'returns file types that correspond to reports' do
+ # If this test fails chances are you've added a new
+ # artifact file_type and must decide whether or not
+ # it's a report.
+ expect(subject.keys).to match_array(EXPECTED_REPORT_TYPES)
+ end
+ end
+
describe '#file' do
subject { artifact.file }