Commit f7140dcc authored by Robert Speicher's avatar Robert Speicher

Merge branch 'index-milestone-title-label' into 'master'

Improve performance when filtering on milestone on labels

Also some minor refactoring in the seeding.

See merge request !1587
parents 904c11ef 8b8fbd4e
...@@ -74,6 +74,7 @@ v 8.1.0 (unreleased) ...@@ -74,6 +74,7 @@ v 8.1.0 (unreleased)
- Only render 404 page from /public - Only render 404 page from /public
- Hide passwords from services API (Alex Lossent) - Hide passwords from services API (Alex Lossent)
- Fix: Images cannot show when projects' path was changed - Fix: Images cannot show when projects' path was changed
- Optimize query when filtering on issuables (Zeger-Jan van de Weg)
- Fix padding of outdated discussion item. - Fix padding of outdated discussion item.
v 8.0.4 v 8.0.4
......
...@@ -53,15 +53,36 @@ class IssuableFinder ...@@ -53,15 +53,36 @@ class IssuableFinder
end end
end end
def project?
params[:project_id].present?
end
def project def project
return @project if defined?(@project) return @project if defined?(@project)
@project = if project?
if params[:project_id].present? @project = Project.find(params[:project_id])
Project.find(params[:project_id])
else unless Ability.abilities.allowed?(current_user, :read_project, @project)
nil @project = nil
end end
else
@project = nil
end
@project
end
def projects
return @projects if defined?(@projects)
if project?
project
elsif current_user && params[:authorized_only].presence && !current_user_related?
current_user.authorized_projects
else
ProjectsFinder.new.execute(current_user)
end
end end
def search def search
...@@ -72,7 +93,7 @@ class IssuableFinder ...@@ -72,7 +93,7 @@ class IssuableFinder
params[:milestone_title].present? params[:milestone_title].present?
end end
def no_milestones? def filter_by_no_milestone?
milestones? && params[:milestone_title] == Milestone::None.title milestones? && params[:milestone_title] == Milestone::None.title
end end
...@@ -81,12 +102,22 @@ class IssuableFinder ...@@ -81,12 +102,22 @@ class IssuableFinder
@milestones = @milestones =
if milestones? if milestones?
Milestone.where(title: params[:milestone_title]) scope = Milestone.where(project_id: projects)
scope.where(title: params[:milestone_title])
else else
nil nil
end end
end end
def labels?
params[:label_name].present?
end
def filter_by_no_label?
labels? && params[:label_name] == Label::None.title
end
def assignee? def assignee?
params[:assignee_id].present? params[:assignee_id].present?
end end
...@@ -120,19 +151,7 @@ class IssuableFinder ...@@ -120,19 +151,7 @@ class IssuableFinder
private private
def init_collection def init_collection
table_name = klass.table_name klass.all
if project
if Ability.abilities.allowed?(current_user, :read_project, project)
project.send(table_name)
else
[]
end
elsif current_user && params[:authorized_only].presence && !current_user_related?
klass.of_projects(current_user.authorized_projects).references(:project)
else
klass.of_projects(ProjectsFinder.new.execute(current_user)).references(:project)
end
end end
def by_scope(items) def by_scope(items)
...@@ -170,7 +189,12 @@ class IssuableFinder ...@@ -170,7 +189,12 @@ class IssuableFinder
end end
def by_project(items) def by_project(items)
items = items.of_projects(project.id) if project items =
if projects
items.of_projects(projects).references(:project)
else
items.none
end
items items
end end
...@@ -185,18 +209,6 @@ class IssuableFinder ...@@ -185,18 +209,6 @@ class IssuableFinder
items.sort(params[:sort]) items.sort(params[:sort])
end end
def by_milestone(items)
if milestones?
if no_milestones?
items = items.where(milestone_id: [-1, nil])
else
items = items.where(milestone_id: milestones.try(:pluck, :id))
end
end
items
end
def by_assignee(items) def by_assignee(items)
if assignee? if assignee?
items = items.where(assignee_id: assignee.try(:id)) items = items.where(assignee_id: assignee.try(:id))
...@@ -213,20 +225,36 @@ class IssuableFinder ...@@ -213,20 +225,36 @@ class IssuableFinder
items items
end end
def by_label(items) def by_milestone(items)
if params[:label_name].present? if milestones?
if params[:label_name] == Label::None.title if filter_by_no_milestone?
item_ids = LabelLink.where(target_type: klass.name).pluck(:target_id) items = items.where(milestone_id: [-1, nil])
else
items = items.joins(:milestone).where(milestones: { title: params[:milestone_title] })
if projects
items = items.where(milestones: { project_id: projects })
end
end
end
items
end
items = items.where('id NOT IN (?)', item_ids) def by_label(items)
if labels?
if filter_by_no_label?
items = items.
joins("LEFT OUTER JOIN label_links ON label_links.target_type = '#{klass.name}' AND label_links.target_id = #{klass.table_name}.id").
where(label_links: { id: nil })
else else
label_names = params[:label_name].split(",") label_names = params[:label_name].split(",")
item_ids = LabelLink.joins(:label). items = items.joins(:labels).where(labels: { title: label_names })
where('labels.title in (?)', label_names).
where(target_type: klass.name).pluck(:target_id)
items = items.where(id: item_ids) if projects
items = items.where(labels: { project_id: projects })
end
end end
end end
......
Gitlab::Seeder.quiet do Gitlab::Seeder.quiet do
(2..20).each do |i| 20.times do |i|
begin begin
User.create!( User.create!(
username: FFaker::Internet.user_name, username: FFaker::Internet.user_name,
...@@ -15,7 +15,7 @@ Gitlab::Seeder.quiet do ...@@ -15,7 +15,7 @@ Gitlab::Seeder.quiet do
end end
end end
(1..5).each do |i| 5.times do |i|
begin begin
User.create!( User.create!(
username: "user#{i}", username: "user#{i}",
......
Gitlab::Seeder.quiet do Gitlab::Seeder.quiet do
Project.all.each do |project| Project.all.each do |project|
(1..5).each do |i| 5.times do |i|
milestone_params = { milestone_params = {
title: "v#{i}.0", title: "v#{i}.0",
description: FFaker::Lorem.sentence, description: FFaker::Lorem.sentence,
......
Gitlab::Seeder.quiet do Gitlab::Seeder.quiet do
Project.all.each do |project| Project.all.each do |project|
(1..10).each do |i| 10.times do
issue_params = { issue_params = {
title: FFaker::Lorem.sentence(6), title: FFaker::Lorem.sentence(6),
description: FFaker::Lorem.sentence, description: FFaker::Lorem.sentence,
......
...@@ -22,7 +22,7 @@ class Member < ActiveRecord::Base ...@@ -22,7 +22,7 @@ class Member < ActiveRecord::Base
end end
eos eos
(1..50).each do |i| 50.times do |i|
user = User.all.sample user = User.all.sample
PersonalSnippet.seed(:id, [{ PersonalSnippet.seed(:id, [{
......
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