Commit fdf7f6a1 authored by Mark Chao's avatar Mark Chao

Merge branch...

Merge branch '33895-expand-supported-number-projects-on-environments-dashboard-by-adding-pagination' into 'master'

Add backend pagination to the environments dashboard

See merge request gitlab-org/gitlab!39847
parents e81de178 a481e240
......@@ -76,10 +76,13 @@ class OperationsController < ApplicationController
end
def serialize_as_json(projects)
DashboardOperationsSerializer.new(current_user: current_user).represent(projects).as_json
DashboardOperationsSerializer.new(current_user: current_user).represent(projects)
end
def serialize_as_json_for_environments(projects)
DashboardEnvironmentsSerializer.new(current_user: current_user).represent(projects).as_json
DashboardEnvironmentsSerializer
.new(current_user: current_user)
.with_pagination(request, response)
.represent(projects)
end
end
# frozen_string_literal: true
class DashboardEnvironmentsSerializer < BaseSerializer
include WithPagination
entity DashboardEnvironmentsProjectEntity
end
......@@ -3,8 +3,6 @@
module Dashboard
module Environments
class ListService
MAX_NUM_PROJECTS = 7
def initialize(user)
@user = user
end
......@@ -21,7 +19,7 @@ module Dashboard
def load_projects(user)
projects = ::Dashboard::Projects::ListService
.new(user, feature: :operations_dashboard)
.execute(user.ops_dashboard_projects, limit: MAX_NUM_PROJECTS)
.execute(user.ops_dashboard_projects)
ActiveRecord::Associations::Preloader.new.preload(projects, [
:route,
......
......@@ -3,17 +3,18 @@
module Dashboard
module Projects
class ListService
PRESELECT_PROJECTS_LIMIT = 150
def initialize(user, feature:)
@user = user
@feature = feature
end
def execute(project_ids, include_unavailable: false, limit: nil)
return [] unless License.feature_available?(feature)
def execute(project_ids, include_unavailable: false)
return Project.none unless License.feature_available?(feature)
project_ids = available_project_ids(project_ids) unless include_unavailable
projects = find_projects(project_ids)
projects = available_projects(projects) unless include_unavailable
projects = limit ? projects.first(limit) : projects
projects
end
......@@ -22,8 +23,10 @@ module Dashboard
attr_reader :user, :feature
def available_projects(projects)
projects.select { |project| project.feature_available?(feature) }
# see https://gitlab.com/gitlab-org/gitlab/-/merge_requests/39847
def available_project_ids(project_ids)
projects = Project.with_namespace.id_in(project_ids.first(PRESELECT_PROJECTS_LIMIT))
projects.select { |project| project.feature_available?(feature) }.map(&:id)
end
def find_projects(project_ids)
......
---
title: Add backend pagination to the environments dashboard
merge_request: 39847
author:
type: change
......@@ -400,19 +400,53 @@ RSpec.describe OperationsController do
expect(last_deployment_json['id']).to eq(deployment.id)
end
it 'returns a maximum of seven projects' do
projects = Array.new(8).map do
project = create(:project)
project.add_developer(user)
project
context 'with environments pagination' do
shared_examples_for 'environments pagination' do |params, projects_count|
specify do
get :environments_list, params: params
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('dashboard/operations/environments_list', dir: 'ee')
expect(json_response['projects'].count).to eq(projects_count)
expect(response).to include_pagination_headers
end
end
user.update!(ops_dashboard_projects: projects)
get :environments_list
context 'pagination behaviour' do
before do
projects = create_list(:project, 8) do |project|
project.add_developer(user)
end
user.update!(ops_dashboard_projects: projects)
end
context 'with `per_page`' do
it_behaves_like 'environments pagination', { per_page: 7 }, 7
end
context 'with `page=1`' do
it_behaves_like 'environments pagination', { per_page: 7, page: 1 }, 7
end
context 'with `page=2`' do
it_behaves_like 'environments pagination', { per_page: 7, page: 2 }, 1
end
end
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('dashboard/operations/environments_list', dir: 'ee')
expect(json_response['projects'].count).to eq(7)
context 'N+1 queries' do
subject { get :environments_list }
it 'avoids N+1 database queries' do
control_count = ActiveRecord::QueryRecorder.new { subject }.count
projects = create_list(:project, 8) do |project|
project.add_developer(user)
end
user.update!(ops_dashboard_projects: projects)
expect { subject }.not_to exceed_query_limit(control_count)
end
end
end
it 'does not return a project for which the operations dashboard feature is unavailable' do
......
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