gitlab-org--gitlab-foss/app/finders/projects_finder.rb

178 lines
4.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
# ProjectsFinder
#
# Used to filter Projects by set of params
#
# Arguments:
# current_user - which user use
# project_ids_relation: int[] - project ids to use
# params:
# trending: boolean
# owned: boolean
# non_public: boolean
# starred: boolean
# sort: string
# visibility_level: int
# tags: string[]
# personal: boolean
# search: string
# non_archived: boolean
# archived: 'only' or boolean
# min_access_level: integer
#
2016-03-20 16:03:53 -04:00
class ProjectsFinder < UnionFinder
2017-09-18 09:03:24 -04:00
include CustomAttributesFilter
attr_accessor :params
attr_reader :current_user, :project_ids_relation
def initialize(params: {}, current_user: nil, project_ids_relation: nil)
@params = params
@current_user = current_user
@project_ids_relation = project_ids_relation
end
def execute
2017-06-29 13:20:59 -04:00
user = params.delete(:user)
collection =
if user
PersonalProjectsFinder.new(user, finder_params).execute(current_user) # rubocop: disable CodeReuse/Finder
2017-06-29 13:20:59 -04:00
else
init_collection
end
collection = filter_projects(collection)
2017-06-13 10:44:55 -04:00
sort(collection)
end
private
def init_collection
2017-06-13 10:44:55 -04:00
if current_user
collection_with_user
else
collection_without_user
end
end
# EE would override this to add more filters
def filter_projects(collection)
collection = by_ids(collection)
collection = by_personal(collection)
collection = by_starred(collection)
collection = by_trending(collection)
collection = by_visibilty_level(collection)
collection = by_tags(collection)
collection = by_search(collection)
collection = by_archived(collection)
collection = by_custom_attributes(collection)
collection = by_deleted_status(collection)
collection
end
# rubocop: disable CodeReuse/ActiveRecord
2017-06-13 10:44:55 -04:00
def collection_with_user
if owned_projects?
current_user.owned_projects
elsif min_access_level?
current_user.authorized_projects.where('project_authorizations.access_level >= ?', params[:min_access_level])
else
2017-06-13 10:44:55 -04:00
if private_only?
current_user.authorized_projects
else
Project.public_or_visible_to_user(current_user)
2017-06-13 10:44:55 -04:00
end
end
2017-06-13 10:44:55 -04:00
end
# rubocop: enable CodeReuse/ActiveRecord
2017-06-13 10:44:55 -04:00
# Builds a collection for an anonymous user.
def collection_without_user
if private_only? || owned_projects? || min_access_level?
2017-06-13 10:44:55 -04:00
Project.none
else
Project.public_to_user
end
end
def owned_projects?
params[:owned].present?
end
2017-06-13 10:44:55 -04:00
def private_only?
params[:non_public].present?
end
def min_access_level?
params[:min_access_level].present?
end
# rubocop: disable CodeReuse/ActiveRecord
def by_ids(items)
UNION of SELECT/WHERE is faster than WHERE on UNION Instead of applying WHERE on a UNION, apply the WHERE on each of the seperate SELECT statements, and do UNION on that. Local tests with about 2_000_000 projects: - 1_500_000 private projects - 40_000 internal projects - 400_000 public projects For the API endpoint `/api/v4/projects?visibility=private` the slowest query was: ```sql SELECT "projects".* FROM "projects" WHERE ... ``` The original query took 1073.8ms. The query refactored to UNION of SELECT/WHERE took 2.3ms. The original query was: ```sql SELECT "projects".* FROM "projects" WHERE "projects"."pending_delete" = $1 AND (projects.id IN (SELECT "projects"."id" FROM "projects" INNER JOIN "project_authorizations" ON "projects"."id" = "project_authorizations"."project_id" WHERE "projects"."pending_delete" = 'f' AND "project_authorizations"."user_id" = 23 UNION SELECT "projects"."id" FROM "projects" WHERE "projects"."visibility_level" IN (20, 10))) AND "projects"."visibility_level" = $2 AND "projects"."archived" = $3 ORDER BY "projects"."created_at" DESC LIMIT 20 OFFSET 0 [["pending_delete", "f"], ["visibility_level", 0], ["archived", "f"]] ``` The refactored query: ```sql SELECT "projects".* FROM "projects" WHERE "projects"."pending_delete" = $1 AND (projects.id IN (SELECT "projects"."id" FROM "projects" INNER JOIN "project_authorizations" ON "projects"."id" = "project_authorizations"."project_id" WHERE "projects"."pending_delete" = 'f' AND "project_authorizations"."user_id" = 23 AND "projects"."visibility_level" = 0 AND "projects"."archived" = 'f' UNION SELECT "projects"."id" FROM "projects" WHERE "projects"."visibility_level" IN (20, 10) AND "projects"."visibility_level" = 0 AND "projects"."archived" = 'f')) ORDER BY "projects"."created_at" DESC LIMIT 20 OFFSET 0 [["pending_delete", "f"]] ```
2017-05-24 09:03:45 -04:00
project_ids_relation ? items.where(id: project_ids_relation) : items
end
# rubocop: enable CodeReuse/ActiveRecord
def union(items)
find_union(items, Project).with_route
end
def by_personal(items)
(params[:personal].present? && current_user) ? items.personal(current_user) : items
end
def by_starred(items)
(params[:starred].present? && current_user) ? items.starred_by(current_user) : items
end
def by_trending(items)
params[:trending].present? ? items.trending : items
end
# rubocop: disable CodeReuse/ActiveRecord
def by_visibilty_level(items)
params[:visibility_level].present? ? items.where(visibility_level: params[:visibility_level]) : items
end
# rubocop: enable CodeReuse/ActiveRecord
def by_tags(items)
params[:tag].present? ? items.tagged_with(params[:tag]) : items
end
def by_search(items)
params[:search] ||= params[:name]
params[:search].present? ? items.search(params[:search]) : items
end
def by_deleted_status(items)
params[:without_deleted].present? ? items.without_deleted : items
end
def sort(items)
params[:sort].present? ? items.sort_by_attribute(params[:sort]) : items.order_id_desc
end
def by_archived(projects)
if params[:non_archived]
projects.non_archived
elsif params.key?(:archived)
if params[:archived] == 'only'
projects.archived
elsif Gitlab::Utils.to_boolean(params[:archived])
projects
else
projects.non_archived
end
else
projects
end
end
def finder_params
return {} unless min_access_level?
{ min_access_level: params[:min_access_level] }
end
end