Optimize repository datatable cell serializer lookup, remove N+1 queries [SCI-11053] (#7851)

This commit is contained in:
Alex Kriuchykhin 2024-09-13 13:32:01 +02:00 committed by GitHub
parent 288e8d26af
commit 7f15eecbd8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 31 additions and 10 deletions

View file

@ -34,6 +34,8 @@ class MyModuleRepositoriesController < ApplicationController
repository_rows = datatable_service.repository_rows
.preload(:repository_columns,
:created_by,
:archived_by,
:last_modified_by,
repository_cells: { value: @repository.cell_preload_includes })
rows_view = 'repository_rows/index'
end

View file

@ -31,6 +31,7 @@ class RepositoryRowsController < ApplicationController
.preload(:repository_columns,
:created_by,
:archived_by,
:last_modified_by,
repository_cells: { value: @repository.cell_preload_includes })
.page(page)
.per(per_page)

View file

@ -6,6 +6,8 @@ module RepositoryDatatableHelper
def prepare_row_columns(repository_rows, repository, columns_mappings, team, options = {})
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?
repository_rows = reminders_enabled ? with_reminders_status(repository_rows, repository) : repository_rows
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)
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
stock_cell = record.repository_cells.find { |cell| cell.value_type == 'RepositoryStockValue' }
@ -98,6 +95,9 @@ module RepositoryDatatableHelper
}
}
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
row
@ -274,7 +274,27 @@ module RepositoryDatatableHelper
end
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(
cell.value,
scope: {
@ -338,8 +358,4 @@ module RepositoryDatatableHelper
def display_stock_warnings?(repository)
!repository.is_a?(RepositorySnapshot)
end
def repository_row_connections_enabled
Repository.repository_row_connections_enabled?
end
end

View file

@ -63,6 +63,8 @@ class Extends
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
REPOSITORY_EXTRA_SEARCH_ATTR = {
RepositoryTextValue: {