Commit e9567195 authored by Thiago Figueiró's avatar Thiago Figueiró Committed by Luke Duncalfe
parent 7a8c79ca
...@@ -225,30 +225,6 @@ module EE ...@@ -225,30 +225,6 @@ module EE
model_name.singular model_name.singular
end end
def counts_by_day_and_severity(start_date, end_date)
return [] unless ::Feature.enabled?(:vulnerability_history, default_enabled: true)
num_days_of_history = end_date - start_date + 1
# this clause guards against query timeouts
raise TooManyDaysError, "Cannot fetch counts for more than #{MAX_DAYS_OF_HISTORY} days" if num_days_of_history > MAX_DAYS_OF_HISTORY
quoted_start_date = connection.quote(start_date)
quoted_end_date = connection.quote(end_date)
select(
'DATE(calendar.entry) AS day, severity, COUNT(*)'
).from(
"generate_series(DATE #{quoted_start_date}, DATE #{quoted_end_date}, INTERVAL '1 day') as calendar(entry)"
).joins(
'INNER JOIN vulnerabilities ON vulnerabilities.created_at <= calendar.entry'
).where(
'(vulnerabilities.dismissed_at IS NULL OR vulnerabilities.dismissed_at > calendar.entry) AND (vulnerabilities.resolved_at IS NULL OR vulnerabilities.resolved_at > calendar.entry)'
).group(
:day, :severity
)
end
def report_type_order def report_type_order
report_types report_types
.sort .sort
......
---
name: vulnerability_history
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/27052
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/213625
milestone: '13.0'
type: development
group: group::threat insights
default_enabled: true
...@@ -447,63 +447,6 @@ RSpec.describe Vulnerability do ...@@ -447,63 +447,6 @@ RSpec.describe Vulnerability do
end end
end end
describe '.counts_by_day_and_severity' do
let(:current_date) { Time.zone.parse('2019-10-31') }
let(:from_date) { Date.parse('2019-10-22') }
let(:to_date) { Date.parse('2019-10-28') }
let!(:vulnerability_1) { create(:vulnerability, created_at: 5.days.ago, dismissed_at: Date.current, severity: :critical) }
let!(:vulnerability_2) { create(:vulnerability, created_at: 5.days.ago, dismissed_at: 1.day.ago, severity: :high) }
let!(:vulnerability_3) { create(:vulnerability, created_at: 4.days.ago, resolved_at: 2.days.ago, severity: :critical) }
subject(:counts_by_day_and_severity) { ::Vulnerability.counts_by_day_and_severity(from_date, to_date) }
around do |example|
travel_to(current_date) { example.run }
end
context 'when the vulnerability_history feature flag is disabled' do
before do
stub_feature_flags(vulnerability_history: false)
end
it 'returns an empty array' do
expect(counts_by_day_and_severity).to be_empty
end
end
context 'when the vulnerability_history feature flag is enabled' do
before do
stub_feature_flags(vulnerability_history: true)
end
context 'when there are less than 10 days between the from and to dates' do
it 'returns the count of unresolved, undismissed vulnerabilities for each severity for each day from the start date to the end date' do
expect(counts_by_day_and_severity.order(:day, :severity).to_json).to eq([
{ 'day' => '2019-10-26', 'severity' => 'high', 'count' => 1, 'id' => nil },
{ 'day' => '2019-10-26', 'severity' => 'critical', 'count' => 1, 'id' => nil },
{ 'day' => '2019-10-27', 'severity' => 'high', 'count' => 1, 'id' => nil },
{ 'day' => '2019-10-27', 'severity' => 'critical', 'count' => 2, 'id' => nil },
{ 'day' => '2019-10-28', 'severity' => 'high', 'count' => 1, 'id' => nil },
{ 'day' => '2019-10-28', 'severity' => 'critical', 'count' => 2, 'id' => nil }
].to_json)
end
end
context 'when theere are more than 10 days between the from and to dates' do
let(:from_date) { 10.days.ago.to_date }
let(:to_date) { Date.current }
it 'raises a TooManyDaysError' do
expect { counts_by_day_and_severity }.to raise_error(
::Vulnerability::TooManyDaysError,
'Cannot fetch counts for more than 10 days'
)
end
end
end
end
describe '.active_state_values' do describe '.active_state_values' do
let(:expected_values) { ::Vulnerability.states.values_at('detected', 'confirmed') } let(:expected_values) { ::Vulnerability.states.values_at('detected', 'confirmed') }
......
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