Improve sidebar rendering performance [SCI-2478]

This commit is contained in:
Oleksii Kriuchykhin 2018-06-01 13:49:02 +02:00
parent c6cf5f1aab
commit 2ddc0fb561
14 changed files with 96 additions and 119 deletions

View file

@ -11,7 +11,7 @@ class ExperimentsController < ApplicationController
before_action :set_project,
only: %i(new create samples_index samples module_archive
clone_modal move_modal delete_samples)
before_action :load_projects_by_teams, only: %i(canvas samples module_archive)
before_action :load_projects_tree, only: %i(canvas samples module_archive)
before_action :check_view_permissions,
only: %i(canvas module_archive)
before_action :check_manage_permissions, only: :edit
@ -348,9 +348,8 @@ class ExperimentsController < ApplicationController
params.require(:experiment).permit(:name, :description, :archived)
end
def load_projects_by_teams
@projects_by_teams = current_user.projects_by_teams(current_team.id,
nil, false)
def load_projects_tree
@projects_tree = current_user.projects_tree(current_team, nil)
end
def check_view_permissions

View file

@ -21,8 +21,8 @@ class MyModulesController < ApplicationController
unassign_repository_records_modal
assign_repository_records_modal
repository_index)
before_action :load_projects_by_teams, only: %i(protocols results activities
samples repository archive)
before_action :load_projects_tree, only: %i(protocols results activities
samples repository archive)
before_action :check_manage_permissions_archive, only: %i(update destroy)
before_action :check_manage_permissions, only: %i(description due_date)
before_action :check_view_permissions, only:
@ -659,9 +659,8 @@ class MyModulesController < ApplicationController
render_403 unless can_read_team?(@repository.team)
end
def load_projects_by_teams
@projects_by_teams = current_user.projects_by_teams(current_team.id,
nil, false)
def load_projects_tree
@projects_tree = current_user.projects_tree(current_team, nil)
end
def check_manage_permissions

View file

@ -9,7 +9,7 @@ class ProjectsController < ApplicationController
notifications reports
samples experiment_archive
delete_samples samples_index)
before_action :load_projects_by_teams, only: %i(index show samples archive
before_action :load_projects_tree, only: %i(index show samples archive
experiment_archive)
before_action :load_archive_vars, only: :archive
before_action :check_view_permissions, only: %i(show reports notifications
@ -313,26 +313,24 @@ class ProjectsController < ApplicationController
end
end
def load_projects_by_teams
def load_projects_tree
if current_user.teams.any?
@current_team_id = current_team.id if current_team
@current_team = current_team if current_team
@current_team_id ||= current_user.teams.first.id
@current_team ||= current_user.teams.first
@current_sort = params[:sort].to_s
@projects_by_teams = current_user.projects_by_teams(@current_team_id,
@current_sort,
false)
@projects_tree = current_user.projects_tree(@current_team, @current_sort)
else
@projects_by_teams = []
@projects_tree = []
end
end
def load_archive_vars
if current_user.teams.any?
@archived_projects_by_teams =
current_user.projects_by_teams(@current_team_id, @current_sort, true)
current_user.projects_by_teams(@current_team.id, @current_sort, true)
else
@projects_by_teams = []
@archived_projects_by_teams = []
end
end

View file

@ -19,6 +19,8 @@ class Experiment < ApplicationRecord
optional: true
has_many :my_modules, inverse_of: :experiment, dependent: :destroy
has_many :active_my_modules, -> { where(archived: false) },
class_name: 'MyModule'
has_many :my_module_groups, inverse_of: :experiment, dependent: :destroy
has_many :report_elements, inverse_of: :experiment, dependent: :destroy
has_many :activities, inverse_of: :experiment

View file

@ -32,6 +32,8 @@ class Project < ApplicationRecord
has_many :user_projects, inverse_of: :project
has_many :users, through: :user_projects
has_many :experiments, inverse_of: :project
has_many :active_experiments, -> { where(archived: false) },
class_name: 'Experiment'
has_many :project_comments, foreign_key: :associated_id, dependent: :destroy
has_many :activities, inverse_of: :project
has_many :tags, inverse_of: :project
@ -156,7 +158,7 @@ class Project < ApplicationRecord
return (self.user_projects.select { |up| up.user == user }).first.role
end
def active_experiments(sort_by = :new)
def sorted_active_experiments(sort_by = :new)
sort = case sort_by
when 'old' then { created_at: :asc }
when 'atoz' then { name: :asc }

View file

@ -338,6 +338,30 @@ class User < ApplicationRecord
result || []
end
def projects_tree(team, sort_by = nil)
result = team.projects.includes(active_experiments: :active_my_modules)
unless is_admin_of_team?(team)
# Only admins see all projects of the team
result = result.includes(:user_projects).where(
'visibility = 1 OR user_projects.user_id=:user_id)', user_id: id
)
end
sort =
case sort_by
when 'old'
{ created_at: :asc }
when 'atoz'
{ name: :asc }
when 'ztoa'
{ name: :desc }
else
{ created_at: :desc }
end
result.where(archived: false).order(sort)
end
# Finds all activities of user that is assigned to project. If user
# is not an owner of the project, user must be also assigned to
# module.

View file

@ -19,7 +19,7 @@
<ul class="dropdown-menu" aria-labelledby="sortMenu">
<% ["new", "old", "atoz", "ztoa"].each do |sort| %>
<% if @current_sort != sort %>
<li><a href="?<%= {team: @current_team_id, sort: sort}.reject{|k,v| v.to_s == "0"}.to_query %>"><%= t('projects.index.sort_' + sort) %></a></li>
<li><a href="?<%= {team: @current_team.id, sort: sort}.reject{|k,v| v.to_s == "0"}.to_query %>"><%= t('projects.index.sort_' + sort) %></a></li>
<% else %>
<li class="disabled"><a href="#"><%= t('projects.index.sort_' + sort) %></a></li>
<% end %>

View file

@ -96,10 +96,8 @@
</form>
</div>
<% @projects_by_teams.each do |team, projects| %>
<%= render partial: "projects/index/team_projects",
locals: {team: team, projects: projects} %>
<% end %>
<%= render partial: "projects/index/team_projects",
locals: { projects: @projects_tree } %>
</div>
<% end %>

View file

@ -1,5 +1,5 @@
<div class="row">
<% projects.each_index do |i| project = projects[i] %>
<% projects.each_with_index do |project, i| %>
<div class="col-lg-3 col-md-4 col-sm-6 col-xs-12">
<%= render partial: "projects/index/project", locals: {project: project} %>
</div>

View file

@ -41,7 +41,7 @@
</div>
</div>
<div class="row">
<% @project.active_experiments(@current_sort).each_with_index do |experiment, index| %>
<% @project.sorted_active_experiments(@current_sort).each_with_index do |experiment, index| %>
<%= render partial: 'projects/show/experiment',
locals: { experiment: experiment } %>

View file

@ -1,9 +1,7 @@
<% if project.active_experiments.present? then %>
<% if project.active_experiments.present? %>
<ul>
<% project.active_experiments.each do |experiment| %>
<% if (experiment_page? ||
sample_groups_page_experiment? ||
sample_types_page_expermient?) && experiment == @experiment %>
<% if experiment_page? && experiment == @experiment %>
<li class="active" data-parent="candidate">
<span class="tree-link line-wrap first-indent">
<i class="no-arrow"></i>
@ -15,15 +13,11 @@
<li data-parent="candidate">
<span class="tree-link line-wrap first-indent">
<i class="no-arrow"></i>
<% if can_read_experiment?(experiment) %>
<%= link_to experiment.name,
experiment_action_to_link_to(experiment),
title: experiment.name,
class: 'overview_exp_label'
%>
<% else %>
<%= experiment.name %>
<% end %>
<%= link_to experiment.name,
experiment_action_to_link_to(experiment),
title: experiment.name,
class: 'overview_exp_label'
%>
</span>
<%= render partial: 'shared/sidebar/my_modules', locals: { experiment: experiment } %>
</li>

View file

@ -1,53 +1,18 @@
<% if experiment.active_modules.present? then %>
<% if experiment.active_my_modules.present? %>
<ul>
<% experiment.active_module_groups.each do |my_module_group| %>
<li data-module-group="<%= my_module_group.id %>">
<% if my_module_group.my_modules.present? then %>
<ul>
<span class="my-module-group-element">
<% my_module_group.my_modules.sort_by{|m| m.workflow_order}.each do |my_module| %>
<li class="leaf <%= "active" if currently_active? my_module %>" data-module-id="<%= my_module.id %>">
<span class="tree-link second-indent">
<% if currently_active? my_module %>
<%= my_module.name %>
<% else %>
<% if can_read_experiment?(my_module.experiment) %>
<%= link_to my_module.name, module_action_to_link_to(my_module), class: "module-link", data: { no_turbolink: true } %>
<% else %>
<%= my_module.name %>
<% end %>
<% end %>
<% if is_canvas? %>
<a href="#" class="canvas-center-on"><span class="glyphicon glyphicon-map-marker"></span></a>
<% end %>
</span>
</li>
<% end %>
</span>
</ul>
<% end %>
</li>
<% end %>
<% modules_without_group = experiment.modules_without_group %>
<% if modules_without_group.present? then %>
<li>
<ul>
<% modules_without_group.each do |my_module| %>
<li class="leaf <%= "active" if currently_active? my_module %>"
data-module-id="<%= my_module.id %>">
<span class="tree-link second-indent">
<% if currently_active? my_module %>
<%= my_module.name %>
<% else %>
<%= link_to my_module.name, module_action_to_link_to(my_module), data: { no_turbolink: true } %>
<% end %>
<% if is_canvas? %>
<a href="" class="canvas-center-on"><span class="glyphicon glyphicon-map-marker"></span></a>
<% end %>
</span>
</li>
<% experiment.active_my_modules.each do |my_module| %>
<li class="leaf <%= "active" if currently_active? my_module %>"
data-module-id="<%= my_module.id %>">
<span class="tree-link second-indent">
<% if currently_active? my_module %>
<%= my_module.name %>
<% else %>
<%= link_to my_module.name, module_action_to_link_to(my_module), data: { no_turbolink: true } %>
<% end %>
</ul>
<% if is_canvas? %>
<a href="" class="canvas-center-on"><span class="glyphicon glyphicon-map-marker"></span></a>
<% end %>
</span>
</li>
<% end %>
</ul>

View file

@ -1,35 +1,31 @@
<ul>
<% if @projects_by_teams.present? then %>
<% @projects_by_teams.each do |team, projects| %>
<% projects.each do |project| %>
<% if (project_page? ||
sample_types_page_project? ||
sample_groups_page_project?) && project == @project %>
<li class="active" data-parent="candidate">
<span class="tree-link line-wrap no-indent">
<i class="no-arrow"></i>
<span data-project-id="<%= @project.id %>"
title="<%= @project.name %>"><%= @project.name %></span>
</span>
<%= render partial: 'shared/sidebar/experiments',
locals: { project: project } %>
</li>
<% else %>
<li data-parent="candidate">
<span class="tree-link line-wrap no-indent">
<i class="no-arrow"></i>
<%= link_to project.name,
project_action_to_link_to(project),
title: project.name,
data: { project_id: project.id } %>
</span>
<%= render partial: 'shared/sidebar/experiments',
locals: { project: project } %>
</li>
<% end %>
<% if @projects_tree.present? then %>
<% @projects_tree.each do |project| %>
<% if (project_page? ||
sample_types_page_project? ||
sample_groups_page_project?) && project == @project %>
<li class="active" data-parent="candidate">
<span class="tree-link line-wrap no-indent">
<i class="no-arrow"></i>
<span data-project-id="<%= @project.id %>"
title="<%= @project.name %>"><%= @project.name %></span>
</span>
<%= render partial: 'shared/sidebar/experiments',
locals: { project: project } %>
</li>
<% else %>
<li data-parent="candidate">
<span class="tree-link line-wrap no-indent">
<i class="no-arrow"></i>
<%= link_to project.name,
project_action_to_link_to(project),
title: project.name,
data: { project_id: project.id } %>
</span>
<%= render partial: 'shared/sidebar/experiments',
locals: { project: project } %>
</li>
<% end %>
<% end %>
<% end %>
</ul>

View file

@ -75,7 +75,7 @@ Rails.application.configure do
config.assets.raise_runtime_errors = true
# Only log info and higher on development
config.log_level = :info
config.log_level = :debug
# Only allow Better Errors to work on trusted ip, use ifconfig to see which
# one you use and put it into application.yml!