mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-02-22 23:03:00 +08:00
Merge pull request #2391 from okriuchykhin/ok_SCI_4330
Improve speed of global activities fetching query [SCI-4330]
This commit is contained in:
commit
446280a2e4
8 changed files with 58 additions and 33 deletions
|
@ -5,20 +5,24 @@ class ActivitiesService
|
|||
# Create condition for view permissions checking first
|
||||
visible_teams = user.teams.where(id: teams)
|
||||
visible_projects = Project.viewable_by_user(user, visible_teams)
|
||||
query = Activity.where(project: visible_projects)
|
||||
.or(Activity.where(project: nil, team: visible_teams))
|
||||
visible_by_teams = Activity.where(project: nil, team_id: visible_teams.pluck(:id))
|
||||
.order(created_at: :desc)
|
||||
visible_by_projects = Activity.where(project_id: visible_projects.pluck(:id))
|
||||
.order(created_at: :desc)
|
||||
|
||||
query = Activity.from("((#{visible_by_teams.to_sql}) UNION ALL (#{visible_by_projects.to_sql})) AS activities")
|
||||
|
||||
if filters[:subjects].present?
|
||||
subjects_with_children = load_subjects_children(filters[:subjects])
|
||||
if subjects_with_children[:Project]
|
||||
query = query.where('project_id IN (?)', subjects_with_children[:Project])
|
||||
subjects_with_children.except!(:Project)
|
||||
if subjects_with_children[:project]
|
||||
query = query.where('project_id IN (?)', subjects_with_children[:project])
|
||||
subjects_with_children.except!(:project)
|
||||
end
|
||||
where_condition = subjects_with_children.map { '(subject_type = ? AND subject_id IN(?))' }.join(' OR ')
|
||||
where_arguments = subjects_with_children.flatten
|
||||
if subjects_with_children[:MyModule]
|
||||
if subjects_with_children[:my_module]
|
||||
where_condition = where_condition.concat(' OR (my_module_id IN(?))')
|
||||
where_arguments << subjects_with_children[:MyModule]
|
||||
where_arguments << subjects_with_children[:my_module]
|
||||
end
|
||||
query = query.where(where_condition, *where_arguments)
|
||||
end
|
||||
|
@ -45,12 +49,11 @@ class ActivitiesService
|
|||
end
|
||||
|
||||
def self.load_subjects_children(subjects = {})
|
||||
subject_types = Extends::ACTIVITY_SUBJECT_CHILDREN
|
||||
subject_types.each do |subject_name, children|
|
||||
Extends::ACTIVITY_SUBJECT_CHILDREN.each do |subject_name, children|
|
||||
next unless children && subjects[subject_name]
|
||||
|
||||
children.each do |child|
|
||||
parent_model = subject_name.to_s.constantize
|
||||
parent_model = subject_name.to_s.camelize.constantize
|
||||
child_model = parent_model.reflect_on_association(child).class_name.to_sym
|
||||
next if subjects[child_model]
|
||||
|
||||
|
@ -64,7 +67,7 @@ class ActivitiesService
|
|||
end
|
||||
|
||||
def self.my_module_activities(my_module)
|
||||
subjects_with_children = load_subjects_children(MyModule: [my_module.id])
|
||||
subjects_with_children = load_subjects_children(my_module: [my_module.id])
|
||||
query = Activity.where(project: my_module.experiment.project)
|
||||
query.where(
|
||||
subjects_with_children.map { '(subject_type = ? AND subject_id IN(?))' }.join(' OR '),
|
||||
|
|
|
@ -13,12 +13,12 @@
|
|||
<div class="navigational-breadcrumbs">
|
||||
<% unless activity.old_activity? || !activity.subject_type.present?%>
|
||||
<%= render partial: "global_activities/references/#{activity.subject_type.underscore}.html.erb",
|
||||
locals: {
|
||||
team: activity.team,
|
||||
subject: activity.subject,
|
||||
breadcrumbs: activity.breadcrumbs,
|
||||
values: activity.values,
|
||||
type_of: activity.type_of
|
||||
locals: {
|
||||
team: activity.team,
|
||||
subject: activity.subject,
|
||||
breadcrumbs: activity.breadcrumbs,
|
||||
values: activity.values,
|
||||
type_of: activity.type_of
|
||||
} %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
|
|
@ -5,8 +5,6 @@
|
|||
<%= render partial: "top_pane" %>
|
||||
</div>
|
||||
<div class="ga-bottom">
|
||||
|
||||
|
||||
<div class="ga-main">
|
||||
<div class="activities-container perfect-scrollbar">
|
||||
<div class="no-activities-message text-center <%= 'hidden' if @grouped_activities.keys.any? %>">
|
||||
|
@ -26,13 +24,11 @@
|
|||
<div id="ga-more-activities-placeholder" class="hidden"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="ga-side">
|
||||
<div class="filters-container perfect-scrollbar">
|
||||
<%= render partial: "side_filters" %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
|
|
@ -121,15 +121,15 @@ class Extends
|
|||
).freeze
|
||||
|
||||
ACTIVITY_SUBJECT_CHILDREN = {
|
||||
Repository: [:repository_rows],
|
||||
RepositoryRow: nil,
|
||||
Report: nil,
|
||||
Project: nil,
|
||||
Experiment: [:my_modules],
|
||||
MyModule: [:results,:protocols],
|
||||
Result: nil,
|
||||
Protocol: [:steps],
|
||||
Step: nil
|
||||
repository: [:repository_rows],
|
||||
repository_row: nil,
|
||||
report: nil,
|
||||
project: nil,
|
||||
experiment: [:my_modules],
|
||||
my_module: [:results, :protocols],
|
||||
result: nil,
|
||||
protocol: [:steps],
|
||||
step: nil
|
||||
}
|
||||
|
||||
ACTIVITY_MESSAGE_ITEMS_TYPES =
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class FixAndUpdateIndicesOnActivities < ActiveRecord::Migration[6.0]
|
||||
def up
|
||||
execute(
|
||||
"UPDATE activities " \
|
||||
"SET values = jsonb_set(values, '{\"message_items\", \"asset_name\", \"value_for\"}', '\"file_name\"') " \
|
||||
"WHERE values @> '{\"message_items\": {\"asset_name\": {\"value_for\": \"file_file_name\"}}}';"
|
||||
)
|
||||
add_index :activities, %i(created_at team_id), order: { created_at: :desc, team_id: :asc },
|
||||
where: 'project_id IS NULL',
|
||||
name: 'index_activities_on_created_at_and_team_id_and_no_project_id'
|
||||
end
|
||||
|
||||
def down
|
||||
remove_index :activities, %i(created_at team_id)
|
||||
end
|
||||
end
|
|
@ -3987,6 +3987,13 @@ CREATE UNIQUE INDEX index_active_storage_blobs_on_key ON public.active_storage_b
|
|||
CREATE INDEX index_activities_on_created_at ON public.activities USING btree (created_at);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_activities_on_created_at_and_team_id_and_no_project_id; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE INDEX index_activities_on_created_at_and_team_id_and_no_project_id ON public.activities USING btree (created_at DESC, team_id) WHERE (project_id IS NULL);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_activities_on_experiment_id; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
@ -7159,6 +7166,7 @@ INSERT INTO "schema_migrations" (version) VALUES
|
|||
('20191206105058'),
|
||||
('20191210103004'),
|
||||
('20191218072619'),
|
||||
('20200113143828');
|
||||
('20200113143828'),
|
||||
('20200204100934');
|
||||
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ version: '2'
|
|||
services:
|
||||
db:
|
||||
container_name: scinote_db_production
|
||||
image: postgres:9.6
|
||||
image: postgres:11
|
||||
volumes:
|
||||
- scinote_production_postgres:/var/lib/postgresql/data
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ version: '2'
|
|||
services:
|
||||
db:
|
||||
container_name: scinote_db_development
|
||||
image: postgres:9.6
|
||||
image: postgres:11
|
||||
volumes:
|
||||
- scinote_development_postgres:/var/lib/postgresql/data
|
||||
ports:
|
||||
|
|
Loading…
Reference in a new issue