Commit 9e8cbba5 authored by Maxime Orefice's avatar Maxime Orefice Committed by Vitali Tatarintev

Refactor coverage finder dates

This commit moves our business logic to fetch daily coverage data
by dates in our new finder. We will now use default dates in case
our params being sent by the frontend are nil.
parent dbe5012f
# frozen_string_literal: true # frozen_string_literal: true
class Projects::Ci::DailyBuildGroupReportResultsController < Projects::ApplicationController class Projects::Ci::DailyBuildGroupReportResultsController < Projects::ApplicationController
include Gitlab::Utils::StrongMemoize
REPORT_WINDOW = 90.days
before_action :authorize_read_build_report_results! before_action :authorize_read_build_report_results!
before_action :validate_param_type! before_action :validate_param_type!
...@@ -49,28 +45,13 @@ class Projects::Ci::DailyBuildGroupReportResultsController < Projects::Applicati ...@@ -49,28 +45,13 @@ class Projects::Ci::DailyBuildGroupReportResultsController < Projects::Applicati
{ {
project: project, project: project,
coverage: true, coverage: true,
start_date: start_date, start_date: params[:start_date],
end_date: end_date, end_date: params[:end_date],
ref_path: params[:ref_path], ref_path: params[:ref_path],
sort: true sort: true
} }
end end
def start_date
strong_memoize(:start_date) do
start_date = Date.parse(params.require(:start_date))
# The start_date cannot be older than `end_date - 90 days`
[start_date, end_date - REPORT_WINDOW].max
end
end
def end_date
strong_memoize(:end_date) do
Date.parse(params.require(:end_date))
end
end
def allowed_param_types def allowed_param_types
Ci::DailyBuildGroupReportResult::PARAM_TYPES Ci::DailyBuildGroupReportResult::PARAM_TYPES
end end
......
...@@ -62,7 +62,7 @@ class Projects::GraphsController < Projects::ApplicationController ...@@ -62,7 +62,7 @@ class Projects::GraphsController < Projects::ApplicationController
return unless can?(current_user, :read_build_report_results, project) return unless can?(current_user, :read_build_report_results, project)
date_today = Date.current date_today = Date.current
report_window = Projects::Ci::DailyBuildGroupReportResultsController::REPORT_WINDOW report_window = ::Ci::DailyBuildGroupReportResultsFinder::REPORT_WINDOW
@daily_coverage_options = { @daily_coverage_options = {
base_params: { base_params: {
......
...@@ -11,8 +11,8 @@ ...@@ -11,8 +11,8 @@
# group: integer # group: integer
# coverage: boolean # coverage: boolean
# ref_path: string # ref_path: string
# start_date: date # start_date: string
# end_date: date # end_date: string
# sort: boolean # sort: boolean
# limit: integer # limit: integer
...@@ -21,6 +21,8 @@ module Ci ...@@ -21,6 +21,8 @@ module Ci
include Gitlab::Allowable include Gitlab::Allowable
MAX_ITEMS = 1_000 MAX_ITEMS = 1_000
REPORT_WINDOW = 90.days
DATE_FORMAT_ALLOWED = '%Y-%m-%d'
attr_reader :params, :current_user attr_reader :params, :current_user
...@@ -62,7 +64,7 @@ module Ci ...@@ -62,7 +64,7 @@ module Ci
end end
def by_dates(items) def by_dates(items)
params[:start_date].present? && params[:end_date].present? ? items.by_dates(params[:start_date], params[:end_date]) : items params[:start_date].present? && params[:end_date].present? ? items.by_dates(start_date, end_date) : items
end end
def sort(items) def sort(items)
...@@ -80,6 +82,17 @@ module Ci ...@@ -80,6 +82,17 @@ module Ci
[params[:limit].to_i, MAX_ITEMS].min [params[:limit].to_i, MAX_ITEMS].min
end end
def start_date
start_date = Date.strptime(params[:start_date], DATE_FORMAT_ALLOWED) rescue REPORT_WINDOW.ago.to_date
# The start_date cannot be older than `end_date - 90 days`
[start_date, end_date - REPORT_WINDOW].max
end
def end_date
Date.strptime(params[:end_date], DATE_FORMAT_ALLOWED) rescue Date.current
end
end end
end end
......
...@@ -42,8 +42,8 @@ class Groups::Analytics::CoverageReportsController < Groups::Analytics::Applicat ...@@ -42,8 +42,8 @@ class Groups::Analytics::CoverageReportsController < Groups::Analytics::Applicat
{ {
group: @group, group: @group,
coverage: true, coverage: true,
start_date: Date.parse(params.require(:start_date)), start_date: params[:start_date],
end_date: Date.parse(params.require(:end_date)), end_date: params[:end_date],
ref_path: params[:ref_path], ref_path: params[:ref_path],
sort: true sort: true
} }
......
...@@ -79,6 +79,25 @@ RSpec.describe Ci::DailyBuildGroupReportResultsFinder do ...@@ -79,6 +79,25 @@ RSpec.describe Ci::DailyBuildGroupReportResultsFinder do
]) ])
end end
end end
context 'when provided dates are nil' do
let(:start_date) { nil }
let(:end_date) { nil }
let(:rspec_coverage_4) { create_daily_coverage('rspec', 98.0, 91.days.ago.to_date.to_s) }
it 'returns all coverages from the last 90 days' do
expect(coverages).to match_array(
[
karma_coverage_3,
rspec_coverage_3,
karma_coverage_2,
rspec_coverage_2,
karma_coverage_1,
rspec_coverage_1
]
)
end
end
end end
end end
end end
......
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