Commit dcb14460 authored by Niko Belokolodov's avatar Niko Belokolodov

Add LicenceManagementJobs instrumentation class

Add tests for LicenseManagementJobs metric.
Disable license_scanning_jobs tests.
Becaue the metric is temporary disabled we would need to
disable related tests as well.
parent e856d01e
......@@ -171,6 +171,7 @@ module Ci
scope :with_live_trace, -> { where('EXISTS (?)', Ci::BuildTraceChunk.where('ci_builds.id = ci_build_trace_chunks.build_id').select(1)) }
scope :with_stale_live_trace, -> { with_live_trace.finished_before(12.hours.ago) }
scope :finished_before, -> (date) { finished.where('finished_at < ?', date) }
scope :license_management_jobs, -> { where(name: %i(license_management license_scanning)) } # handle license rename https://gitlab.com/gitlab-org/gitlab/issues/8911
scope :with_secure_reports_from_config_options, -> (job_types) do
joins(:metadata).where("ci_builds_metadata.config_options -> 'artifacts' -> 'reports' ?| array[:job_types]", job_types: job_types)
......
......@@ -6,6 +6,7 @@ product_stage: secure
product_group: group::composition analysis
product_category: license_compliance
value_type: number
instrumentation_class: LicenseManagementJobsMetric
status: active
time_frame: all
data_source: database
......
......@@ -144,13 +144,12 @@ module EE
def security_products_usage
results = SECURE_PRODUCT_TYPES.each_with_object({}) do |(secure_type, attribs), response|
next if secure_type == :license_management
response[attribs[:name]] = count(::Ci::Build.where(name: secure_type)) # rubocop:disable CodeReuse/ActiveRecord
end
# handle license rename https://gitlab.com/gitlab-org/gitlab/issues/8911
license_scan_count = results.delete(:license_scanning_jobs)
results[:license_management_jobs] += license_scan_count > 0 ? license_scan_count : 0 if license_scan_count.is_a?(Integer)
results[:license_management_jobs] = add_metric("LicenseManagementJobsMetric")
results
end
......
# frozen_string_literal: true
module Gitlab
module Usage
module Metrics
module Instrumentations
class LicenseManagementJobsMetric < DatabaseMetric
operation :count
relation { ::Ci::Build.license_management_jobs }
end
end
end
end
end
......@@ -8,6 +8,7 @@ RSpec.describe 'Every metric definition' do
let(:usage_ping) { Gitlab::UsageData.uncached_data }
let(:ignored_usage_ping_key_patterns) do
%w(
counts.license_scanning_jobs
license_add_ons
testing_total_unique_counts
user_auth_by_provider
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Usage::Metrics::Instrumentations::LicenseManagementJobsMetric do
before do
create(:ci_build, name: :license_management)
end
it_behaves_like 'a correct instrumented metric value', { time_frame: 'none', data_source: 'ruby' } do
let(:expected_value) { 1 }
end
end
......@@ -215,6 +215,26 @@ RSpec.describe Ci::Build do
end
end
describe '.license_management_jobs' do
subject { described_class.license_management_jobs }
let!(:management_build) { create(:ci_build, :success, name: :license_management) }
let!(:scanning_build) { create(:ci_build, :success, name: :license_scanning) }
let!(:another_build) { create(:ci_build, :success, name: :another_type) }
it 'returns license_scanning jobs' do
is_expected.to include(scanning_build)
end
it 'returns license_management jobs' do
is_expected.to include(management_build)
end
it 'doesnt return filtered out jobs' do
is_expected.not_to include(another_build)
end
end
describe '.finished_before' do
subject { described_class.finished_before(date) }
......
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