Skip to content
代码片段 群组 项目
未验证 提交 3649b482 编辑于 作者: Javiera Tapia's avatar Javiera Tapia 提交者: GitLab
浏览文件

Merge branch 'michold-fix-vuln-metric' into 'master'

Fix ResolvedVulnerabilities metric sql

See merge request https://gitlab.com/gitlab-org/gitlab/-/merge_requests/160087



Merged-by: default avatarJaviera Tapia <jtapia@gitlab.com>
Approved-by: default avatarNiko Belokolodov <nbelokolodov@gitlab.com>
Approved-by: default avatarLucas Charles <me@lucascharles.me>
Approved-by: default avatarJaviera Tapia <jtapia@gitlab.com>
Co-authored-by: default avatarmichold <mwielich@gitlab.com>
No related branches found
No related tags found
无相关合并请求
......@@ -9,6 +9,11 @@ class CountDistinctReportTypesWithResolvedVulnerabilitiesPerProjectMetric < Data
timestamp_column('vulnerability_state_transitions.created_at')
# Override sql so that it doesn't use the Vulnerabilities::Read table name
def to_sql
relation.select("COUNT(*)").to_sql
end
# We must override value since we are not able to batch this query due to usage of the count subquery
def value
relation.count
......
......@@ -13,6 +13,16 @@
let_it_be(:project_with_two_resolutions) { create(:project, group: group_with_resolutions) }
let_it_be(:project_with_one_resolution) { create(:project, group: child_group_with_resolutions) }
let_it_be(:project_with_no_resolutions) { create(:project, group: group_without_resolutions) }
let(:expected_value) { 3 }
let(:expected_query) do
"SELECT COUNT(*) FROM (" \
"SELECT DISTINCT \"vulnerability_reads\".\"project_id\", \"vulnerability_reads\".\"report_type\" FROM " \
"\"vulnerability_reads\" INNER JOIN vulnerability_state_transitions\n " \
"ON vulnerability_state_transitions.vulnerability_id = vulnerability_reads.vulnerability_id " \
"WHERE \"vulnerability_state_transitions\".\"to_state\" = 3 AND " \
"\"vulnerability_state_transitions\".\"created_at\" BETWEEN '#{start}' AND '#{finish}' " \
"GROUP BY \"vulnerability_reads\".\"project_id\", \"vulnerability_reads\".\"report_type\") subquery"
end
before do
create(:vulnerability, :with_read, :resolved, :sast,
......@@ -31,16 +41,7 @@
project: project_with_no_resolutions)
end
it_behaves_like 'a correct instrumented metric value and query', { time_frame: '28d', data_source: 'database' } do
let(:expected_value) { 3 }
let(:expected_query) do
"SELECT COUNT(\"vulnerability_reads\".\"vulnerability_id\") FROM (" \
"SELECT DISTINCT \"vulnerability_reads\".\"project_id\", \"vulnerability_reads\".\"report_type\" FROM " \
"\"vulnerability_reads\" INNER JOIN vulnerability_state_transitions\n " \
"ON vulnerability_state_transitions.vulnerability_id = vulnerability_reads.vulnerability_id " \
"WHERE \"vulnerability_state_transitions\".\"to_state\" = 3 AND " \
"\"vulnerability_state_transitions\".\"created_at\" BETWEEN '#{start}' AND '#{finish}' " \
"GROUP BY \"vulnerability_reads\".\"project_id\", \"vulnerability_reads\".\"report_type\") subquery"
end
end
it_behaves_like 'a correct instrumented database query execution value',
{ time_frame: '28d', data_source: 'database' }
it_behaves_like 'a correct instrumented metric value and query', { time_frame: '28d', data_source: 'database' }
end
......@@ -41,6 +41,25 @@
end
end
RSpec.shared_examples 'a correct instrumented database query execution value' do |params|
let(:time_frame) { params[:time_frame] }
let(:options) { params[:options] }
let(:metric) { described_class.new(time_frame: time_frame, options: options) }
around do |example|
freeze_time { example.run }
end
before do
allow(metric.relation).to receive(:transaction_open?).and_return(false)
end
it 'returns correct value' do
query_result = metric.relation.connection.execute(metric.instrumentation).to_a.first.each_value.first
expect(query_result).to eq(expected_value)
end
end
RSpec.shared_examples 'a correct instrumented metric value and query' do |params|
it_behaves_like 'a correct instrumented metric value', params
it_behaves_like 'a correct instrumented metric query', params
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册