Commit 31b15e3d authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Simplify CI projects query

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 0731a7af
...@@ -16,12 +16,15 @@ module Ci ...@@ -16,12 +16,15 @@ module Ci
end end
def index def index
@projects = Ci::Project.all
if current_user if current_user
@projects = ProjectListBuilder.new.execute(current_user, params[:search]) @projects = @projects.where(gitlab_id: current_user.authorized_projects.pluck(:id))
@projects = @projects.page(params[:page]).per(40)
@total_count = @projects.size
end end
@projects = @projects.includes(:last_commit).order('ci_commits.created_at DESC')
@projects = @projects.page(params[:page]).per(40)
respond_to do |format| respond_to do |format|
format.json do format.json do
pager_json("ci/projects/index", @total_count) pager_json("ci/projects/index", @total_count)
......
...@@ -41,6 +41,7 @@ module Ci ...@@ -41,6 +41,7 @@ module Ci
has_many :events, dependent: :destroy, class_name: 'Ci::Event' has_many :events, dependent: :destroy, class_name: 'Ci::Event'
has_many :variables, dependent: :destroy, class_name: 'Ci::Variable' has_many :variables, dependent: :destroy, class_name: 'Ci::Variable'
has_many :triggers, dependent: :destroy, class_name: 'Ci::Trigger' has_many :triggers, dependent: :destroy, class_name: 'Ci::Trigger'
has_one :last_commit, -> { order 'ci_commits.created_at DESC' }, class_name: 'Ci::Commit'
# Project services # Project services
has_many :services, dependent: :destroy, class_name: 'Ci::Service' has_many :services, dependent: :destroy, class_name: 'Ci::Service'
......
- if project.gitlab_ci_project - last_commit = project.last_commit
- ci_project = project.gitlab_ci_project %tr
- last_commit = ci_project.last_commit %td
%tr = link_to [:ci, project] do
%td = project.name
= link_to [:ci, ci_project] do %td
= ci_project.name - if last_commit
%td = ci_status_with_icon(last_commit.status)
- if last_commit = commit_link(last_commit)
= ci_status_with_icon(last_commit.status) &middot;
= commit_link(last_commit) - if project.last_commit_date
&middot; = time_ago_in_words project.last_commit_date
- if ci_project.last_commit_date ago
= time_ago_in_words ci_project.last_commit_date - else
ago No builds yet
- else %td
No builds yet - if project.public
%td %i.fa.fa-globe
- if ci_project.public Public
%i.fa.fa-globe - else
Public %i.fa.fa-lock
- else Private
%i.fa.fa-lock %td
Private = project.commits.count
%td
= ci_project.commits.count
module Ci
class ProjectListBuilder
def execute(current_user, search = nil)
projects = current_user.authorized_projects
projects = projects.search(search) if search
projects.
joins("LEFT JOIN ci_projects ON projects.id = ci_projects.gitlab_id
LEFT JOIN #{last_commit_subquery} AS last_commit ON #{Ci::Project.table_name}.id = last_commit.project_id").
reorder("ci_projects.id is NULL ASC,
CASE WHEN last_commit.committed_at IS NULL THEN 1 ELSE 0 END,
last_commit.committed_at DESC")
end
private
def last_commit_subquery
"(SELECT project_id, MAX(committed_at) committed_at FROM #{Ci::Commit.table_name} GROUP BY project_id)"
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