Skip to content

Commit

Permalink
Improve activities loading speed [SCI-11178]
Browse files Browse the repository at this point in the history
  • Loading branch information
andrej-scinote authored and artoscinote committed Feb 4, 2025
1 parent 4dd2810 commit 97e9129
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
5 changes: 2 additions & 3 deletions app/models/experiment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ def self.search(
)
teams = options[:teams] || current_team || user.teams.select(:id)

new_query = distinct.with_granted_permissions(user, ExperimentPermissions::READ)
.where(user_assignments: { team: teams })
new_query = distinct.viewable_by_user(user, teams)
.where_attributes_like_boolean(SEARCHABLE_ATTRIBUTES, query, options)

new_query = new_query.joins(:project).active.where(projects: { archived: false }) unless include_archived
Expand All @@ -80,7 +79,7 @@ def self.search(

def self.viewable_by_user(user, teams)
with_granted_permissions(user, ExperimentPermissions::READ)
.where(project: Project.viewable_by_user(user, teams))
.where(user_assignments: { team: teams })
end

def self.with_children_viewable_by_user(user)
Expand Down
5 changes: 2 additions & 3 deletions app/models/my_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ def self.search(
teams = options[:teams] || current_team || user.teams.select(:id)

new_query = distinct.left_joins(:task_comments, my_module_tags: :tag, user_my_modules: :user)
.with_granted_permissions(user, MyModulePermissions::READ)
.where(user_assignments: { team: teams })
.viewable_by_user(user, teams)
.where_attributes_like_boolean(SEARCHABLE_ATTRIBUTES, query, options)

unless include_archived
Expand All @@ -130,7 +129,7 @@ def self.search(

def self.viewable_by_user(user, teams)
with_granted_permissions(user, MyModulePermissions::READ)
.where(experiment: Experiment.viewable_by_user(user, teams))
.where(user_assignments: { team: teams })
end

def self.filter_by_teams(teams = [])
Expand Down
3 changes: 1 addition & 2 deletions app/models/protocol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,7 @@ def self.viewable_by_user(user, teams, options = {})

def self.viewable_by_user_my_module_protocols(user, teams)
distinct.joins(:my_module)
.where(my_modules: MyModule.with_granted_permissions(user, MyModulePermissions::READ)
.where(user_assignments: { team: teams }))
.where(my_modules: MyModule.viewable_by_user(user, teams))
end

def self.filter_by_teams(teams = [])
Expand Down
10 changes: 5 additions & 5 deletions app/services/activities_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
class ActivitiesService
def self.load_activities(user, teams, filters = {})
# Create condition for view permissions checking first
visible_teams = user.teams.where(id: teams)
visible_projects = Project.viewable_by_user(user, visible_teams)
teams = user.teams.where(id: teams).pluck(:id)
visible_projects = Project.viewable_by_user(user, teams)
visible_my_modules = MyModule.viewable_by_user(user, teams)

# Temporary solution until handling of deleted subjects is fully implemented
visible_repository_teams = visible_teams.with_user_permission(user, RepositoryPermissions::READ)
visible_by_teams = Activity.where(project: nil, team_id: visible_teams.select(:id))
visible_repository_teams = user.teams.where(id: teams).with_user_permission(user, RepositoryPermissions::READ)
visible_by_teams = Activity.where(project: nil, team_id: teams)
.where.not(subject_type: %w(RepositoryBase RepositoryRow Protocol))
.order(created_at: :desc)
visible_by_repositories = Activity.where(subject_type: %w(RepositoryBase RepositoryRow), team_id: visible_repository_teams.select(:id))
Expand All @@ -19,7 +19,7 @@ def self.load_activities(user, teams, filters = {})
.order(created_at: :desc)

visible_by_experiments = Activity.where(subject_type: 'Experiment')
.where(subject_id: Experiment.viewable_by_user(user, visible_teams))
.where(subject_id: Experiment.viewable_by_user(user, teams))
.order(created_at: :desc)

visible_by_my_modules = Activity.where("subject_id IN (?) AND subject_type = 'MyModule' OR " \
Expand Down

0 comments on commit 97e9129

Please sign in to comment.