summaryrefslogtreecommitdiff
path: root/app/finders/jobs_with_artifacts_finder.rb
diff options
context:
space:
mode:
authorshampton <shampton@gitlab.com>2019-08-13 14:21:05 -0700
committershampton <shampton@gitlab.com>2019-08-13 14:21:05 -0700
commit7fc69369802614b28de9a46b9b7e10c7c6ea7710 (patch)
tree7d772ff1384d6faf5760e09868d15e5bf0b94e3d /app/finders/jobs_with_artifacts_finder.rb
parentdf35d772c655587eecbe7b3e387c8b8bc287b23c (diff)
downloadgitlab-ce-7fc69369802614b28de9a46b9b7e10c7c6ea7710.tar.gz
Moving community fork to CE branchartifacts-management-contribution
Moving community contribution fork branch to CE so that I can work on it. Original MR: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/18707
Diffstat (limited to 'app/finders/jobs_with_artifacts_finder.rb')
-rw-r--r--app/finders/jobs_with_artifacts_finder.rb64
1 files changed, 64 insertions, 0 deletions
diff --git a/app/finders/jobs_with_artifacts_finder.rb b/app/finders/jobs_with_artifacts_finder.rb
new file mode 100644
index 00000000000..bc291864452
--- /dev/null
+++ b/app/finders/jobs_with_artifacts_finder.rb
@@ -0,0 +1,64 @@
+# frozen_string_literal: true
+
+class JobsWithArtifactsFinder
+ NUMBER_OF_JOBS_PER_PAGE = 30
+
+ def initialize(project:, params:)
+ @project = project
+ @params = params
+ end
+
+ def execute
+ jobs = jobs_with_size
+ jobs = filter_by_name(jobs)
+ jobs = filter_by_deleted_branches(jobs)
+ jobs = sorted(jobs)
+ jobs = paginated(jobs)
+ jobs
+ end
+
+ def total_size
+ job_ids = @project.builds.select(:id)
+
+ @project.builds.where(id: job_ids).sum(:artifacts_size) +
+ @project.job_artifacts.where(job_id: job_ids).sum(:size)
+ end
+
+ def sort_key
+ @params[:sort].presence || 'created_asc'
+ end
+
+ private
+
+ def filter_by_name(jobs)
+ return jobs if @params[:search].blank?
+
+ jobs.search(@params[:search])
+ end
+
+ def filter_by_deleted_branches(jobs)
+ deleted_branches = @params[:'deleted_branches_deleted-branches']
+
+ return jobs if deleted_branches.blank?
+
+ deleted_branches = ActiveModel::Type::Boolean.new.cast(deleted_branches)
+
+ if deleted_branches
+ jobs.where.not(ref: @project.repository.ref_names)
+ else
+ jobs.where(ref: @project.repository.ref_names)
+ end
+ end
+
+ def sorted(jobs)
+ jobs.order_by(sort_key)
+ end
+
+ def paginated(jobs)
+ jobs.page(@params[:page]).per(NUMBER_OF_JOBS_PER_PAGE).without_count
+ end
+
+ def jobs_with_size
+ @project.builds.with_sum_artifacts_size
+ end
+end