summaryrefslogtreecommitdiff
path: root/app/finders/ci/daily_build_group_report_results_finder.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/finders/ci/daily_build_group_report_results_finder.rb')
-rw-r--r--app/finders/ci/daily_build_group_report_results_finder.rb103
1 files changed, 73 insertions, 30 deletions
diff --git a/app/finders/ci/daily_build_group_report_results_finder.rb b/app/finders/ci/daily_build_group_report_results_finder.rb
index ef97ccb4c0f..9e736c70dda 100644
--- a/app/finders/ci/daily_build_group_report_results_finder.rb
+++ b/app/finders/ci/daily_build_group_report_results_finder.rb
@@ -1,56 +1,99 @@
# frozen_string_literal: true
+# DailyBuildGroupReportResultsFinder
+#
+# Used to filter DailyBuildGroupReportResults by set of params
+#
+# Arguments:
+# current_user
+# params:
+# project: integer
+# group: integer
+# coverage: boolean
+# ref_path: string
+# start_date: string
+# end_date: string
+# sort: boolean
+# limit: integer
+
module Ci
class DailyBuildGroupReportResultsFinder
include Gitlab::Allowable
- def initialize(current_user:, project:, ref_path: nil, start_date:, end_date:, limit: nil)
+ MAX_ITEMS = 1_000
+ REPORT_WINDOW = 90.days
+ DATE_FORMAT_ALLOWED = '%Y-%m-%d'
+
+ attr_reader :params, :current_user
+
+ def initialize(params: {}, current_user: nil)
+ @params = params
@current_user = current_user
- @project = project
- @ref_path = ref_path
- @start_date = start_date
- @end_date = end_date
- @limit = limit
end
def execute
- return none unless query_allowed?
+ return Ci::DailyBuildGroupReportResult.none unless query_allowed?
- query
+ collection = Ci::DailyBuildGroupReportResult.by_projects(params[:project])
+ collection = filter_report_results(collection)
+ collection
end
- protected
+ private
- attr_reader :current_user, :project, :ref_path, :start_date, :end_date, :limit
+ def query_allowed?
+ can?(current_user, :read_build_report_results, params[:project])
+ end
- def query
- Ci::DailyBuildGroupReportResult.recent_results(
- query_params,
- limit: limit
- )
+ def filter_report_results(collection)
+ collection = by_coverage(collection)
+ collection = by_ref_path(collection)
+ collection = by_dates(collection)
+
+ collection = sort(collection)
+ collection = limit_by(collection)
+ collection
end
- def query_allowed?
- can?(current_user, :read_build_report_results, project)
+ def by_coverage(items)
+ params[:coverage].present? ? items.with_coverage : items
+ end
+
+ def by_ref_path(items)
+ params[:ref_path].present? ? items.by_ref_path(params[:ref_path]) : items.with_default_branch
end
- def query_params
- params = {
- project_id: project,
- date: start_date..end_date
- }
+ def by_dates(items)
+ params[:start_date].present? && params[:end_date].present? ? items.by_dates(start_date, end_date) : items
+ end
- if ref_path.present?
- params[:ref_path] = ref_path
- else
- params[:default_branch] = true
- end
+ def sort(items)
+ params[:sort].present? ? items.ordered_by_date_and_group_name : items
+ end
- params
+ # rubocop: disable CodeReuse/ActiveRecord
+ def limit_by(items)
+ items.limit(limit)
end
+ # rubocop: enable CodeReuse/ActiveRecord
+
+ def limit
+ return MAX_ITEMS unless params[:limit].present?
- def none
- Ci::DailyBuildGroupReportResult.none
+ [params[:limit].to_i, MAX_ITEMS].min
+ 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
+
+Ci::DailyBuildGroupReportResultsFinder.prepend_if_ee('::EE::Ci::DailyBuildGroupReportResultsFinder')