mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-09-20 14:45:56 +08:00
Optimize repository datatable cell serializer lookup, remove N+1 queries [SCI-11053] (#7851)
This commit is contained in:
parent
288e8d26af
commit
7f15eecbd8
|
@ -34,6 +34,8 @@ class MyModuleRepositoriesController < ApplicationController
|
||||||
repository_rows = datatable_service.repository_rows
|
repository_rows = datatable_service.repository_rows
|
||||||
.preload(:repository_columns,
|
.preload(:repository_columns,
|
||||||
:created_by,
|
:created_by,
|
||||||
|
:archived_by,
|
||||||
|
:last_modified_by,
|
||||||
repository_cells: { value: @repository.cell_preload_includes })
|
repository_cells: { value: @repository.cell_preload_includes })
|
||||||
rows_view = 'repository_rows/index'
|
rows_view = 'repository_rows/index'
|
||||||
end
|
end
|
||||||
|
|
|
@ -31,6 +31,7 @@ class RepositoryRowsController < ApplicationController
|
||||||
.preload(:repository_columns,
|
.preload(:repository_columns,
|
||||||
:created_by,
|
:created_by,
|
||||||
:archived_by,
|
:archived_by,
|
||||||
|
:last_modified_by,
|
||||||
repository_cells: { value: @repository.cell_preload_includes })
|
repository_cells: { value: @repository.cell_preload_includes })
|
||||||
.page(page)
|
.page(page)
|
||||||
.per(per_page)
|
.per(per_page)
|
||||||
|
|
|
@ -6,6 +6,8 @@ module RepositoryDatatableHelper
|
||||||
|
|
||||||
def prepare_row_columns(repository_rows, repository, columns_mappings, team, options = {})
|
def prepare_row_columns(repository_rows, repository, columns_mappings, team, options = {})
|
||||||
has_stock_management = repository.has_stock_management?
|
has_stock_management = repository.has_stock_management?
|
||||||
|
stock_management_column_exists = repository.repository_columns.stock_type.exists?
|
||||||
|
repository_row_connections_enabled = Repository.repository_row_connections_enabled?
|
||||||
reminders_enabled = Repository.reminders_enabled?
|
reminders_enabled = Repository.reminders_enabled?
|
||||||
repository_rows = reminders_enabled ? with_reminders_status(repository_rows, repository) : repository_rows
|
repository_rows = reminders_enabled ? with_reminders_status(repository_rows, repository) : repository_rows
|
||||||
stock_managable = has_stock_management && !options[:disable_stock_management] &&
|
stock_managable = has_stock_management && !options[:disable_stock_management] &&
|
||||||
|
@ -53,11 +55,6 @@ module RepositoryDatatableHelper
|
||||||
serialize_repository_cell_value(cell, team, repository, reminders_enabled: reminders_enabled)
|
serialize_repository_cell_value(cell, team, repository, reminders_enabled: reminders_enabled)
|
||||||
end
|
end
|
||||||
|
|
||||||
if repository.repository_columns.stock_type.exists?
|
|
||||||
stock_cell = record.repository_cells.find { |cell| cell.value_type == 'RepositoryStockValue' }
|
|
||||||
row['stock'] = serialize_repository_cell_value(record.repository_stock_cell, team, repository) if stock_cell.present?
|
|
||||||
end
|
|
||||||
|
|
||||||
if has_stock_management
|
if has_stock_management
|
||||||
stock_cell = record.repository_cells.find { |cell| cell.value_type == 'RepositoryStockValue' }
|
stock_cell = record.repository_cells.find { |cell| cell.value_type == 'RepositoryStockValue' }
|
||||||
|
|
||||||
|
@ -98,6 +95,9 @@ module RepositoryDatatableHelper
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
elsif stock_management_column_exists
|
||||||
|
stock_cell = record.repository_cells.find { |cell| cell.value_type == 'RepositoryStockValue' }
|
||||||
|
row['stock'] = serialize_repository_cell_value(record.repository_stock_cell, team, repository) if stock_cell.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
row
|
row
|
||||||
|
@ -274,7 +274,27 @@ module RepositoryDatatableHelper
|
||||||
end
|
end
|
||||||
|
|
||||||
def serialize_repository_cell_value(cell, team, repository, options = {})
|
def serialize_repository_cell_value(cell, team, repository, options = {})
|
||||||
serializer_class = "RepositoryDatatable::#{cell.repository_column.data_type}Serializer".constantize
|
# case/when is used because it is much faster then .constantize
|
||||||
|
serializer_class =
|
||||||
|
case cell.repository_column.data_type
|
||||||
|
when 'RepositoryTextValue' then RepositoryDatatable::RepositoryTextValueSerializer
|
||||||
|
when 'RepositoryNumberValue' then RepositoryDatatable::RepositoryNumberValueSerializer
|
||||||
|
when 'RepositoryListValue' then RepositoryDatatable::RepositoryListValueSerializer
|
||||||
|
when 'RepositoryChecklistValue' then RepositoryDatatable::RepositoryChecklistValueSerializer
|
||||||
|
when 'RepositoryStatusValue' then RepositoryDatatable::RepositoryStatusValueSerializer
|
||||||
|
when 'RepositoryTimeValue' then RepositoryDatatable::RepositoryTimeValueSerializer
|
||||||
|
when 'RepositoryDateValue' then RepositoryDatatable::RepositoryDateValueSerializer
|
||||||
|
when 'RepositoryDateTimeValue' then RepositoryDatatable::RepositoryDateTimeValueSerializer
|
||||||
|
when 'RepositoryDateRangeValue' then RepositoryDatatable::RepositoryDateRangeValueSerializer
|
||||||
|
when 'RepositoryTimeRangeValue' then RepositoryDatatable::RepositoryTimeRangeValueSerializer
|
||||||
|
when 'RepositoryDateTimeRangeValue' then RepositoryDatatable::RepositoryDateTimeRangeValueSerializer
|
||||||
|
when 'RepositoryAssetValue' then RepositoryDatatable::RepositoryAssetValueSerializer
|
||||||
|
when 'RepositoryStockValue' then RepositoryDatatable::RepositoryStockValueSerializer
|
||||||
|
when 'RepositoryStockConsumptionValue' then RepositoryDatatable::RepositoryStockConsumptionValueSerializer
|
||||||
|
else
|
||||||
|
Extends::REPOSITORY_EXTRA_VALUE_SERIALIZERS[cell.value_type]
|
||||||
|
end
|
||||||
|
|
||||||
serializer_class.new(
|
serializer_class.new(
|
||||||
cell.value,
|
cell.value,
|
||||||
scope: {
|
scope: {
|
||||||
|
@ -338,8 +358,4 @@ module RepositoryDatatableHelper
|
||||||
def display_stock_warnings?(repository)
|
def display_stock_warnings?(repository)
|
||||||
!repository.is_a?(RepositorySnapshot)
|
!repository.is_a?(RepositorySnapshot)
|
||||||
end
|
end
|
||||||
|
|
||||||
def repository_row_connections_enabled
|
|
||||||
Repository.repository_row_connections_enabled?
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -63,6 +63,8 @@ class Extends
|
||||||
|
|
||||||
REPOSITORY_IMPORT_COLUMN_PRELOADS = %i(repository_list_items repository_status_items repository_checklist_items)
|
REPOSITORY_IMPORT_COLUMN_PRELOADS = %i(repository_list_items repository_status_items repository_checklist_items)
|
||||||
|
|
||||||
|
REPOSITORY_EXTRA_VALUE_SERIALIZERS = {}
|
||||||
|
|
||||||
# Extra attributes used for search in repositories, 'filed_name' => include_hash
|
# Extra attributes used for search in repositories, 'filed_name' => include_hash
|
||||||
REPOSITORY_EXTRA_SEARCH_ATTR = {
|
REPOSITORY_EXTRA_SEARCH_ATTR = {
|
||||||
RepositoryTextValue: {
|
RepositoryTextValue: {
|
||||||
|
|
Loading…
Reference in a new issue