Merge pull request #6110 from betagouv/instructeur_procedure_show_another_try

amélioration des temps de réponse instructeur procedure show
This commit is contained in:
LeSim 2021-04-16 14:29:53 +02:00 committed by GitHub
commit 2bb3dca9e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -16,6 +16,12 @@ class ProcedurePresentation < ApplicationRecord
'self' => ['id', 'state'] 'self' => ['id', 'state']
} }
TABLE = 'table'
COLUMN = 'column'
SLASH = '/'
TYPE_DE_CHAMP = 'type_de_champ'
TYPE_DE_CHAMP_PRIVATE = 'type_de_champ_private'
belongs_to :assign_to, optional: false belongs_to :assign_to, optional: false
delegate :procedure, to: :assign_to delegate :procedure, to: :assign_to
@ -65,12 +71,14 @@ class ProcedurePresentation < ApplicationRecord
fields.concat procedure.types_de_champ fields.concat procedure.types_de_champ
.where.not(type_champ: explanatory_types_de_champ) .where.not(type_champ: explanatory_types_de_champ)
.order(:id) .order(:id)
.map { |type_de_champ| field_hash(type_de_champ.libelle, 'type_de_champ', type_de_champ.stable_id.to_s) } .pluck(:libelle, :stable_id)
.map { |(libelle, stable_id)| field_hash(libelle, TYPE_DE_CHAMP, stable_id.to_s) }
fields.concat procedure.types_de_champ_private fields.concat procedure.types_de_champ_private
.where.not(type_champ: explanatory_types_de_champ) .where.not(type_champ: explanatory_types_de_champ)
.order(:id) .order(:id)
.map { |type_de_champ| field_hash(type_de_champ.libelle, 'type_de_champ_private', type_de_champ.stable_id.to_s) } .pluck(:libelle, :stable_id)
.map { |(libelle, stable_id)| field_hash(libelle, TYPE_DE_CHAMP_PRIVATE, stable_id.to_s) }
fields fields
end end
@ -83,11 +91,11 @@ class ProcedurePresentation < ApplicationRecord
end end
def displayed_fields_values(dossier) def displayed_fields_values(dossier)
displayed_fields.map { |field| get_value(dossier, field['table'], field['column']) } displayed_fields.map { |field| get_value(dossier, field[TABLE], field[COLUMN]) }
end end
def sorted_ids(dossiers, instructeur) def sorted_ids(dossiers, instructeur)
table, column, order = sort.values_at('table', 'column', 'order') table, column, order = sort.values_at(TABLE, COLUMN, 'order')
case table case table
when 'notifications' when 'notifications'
@ -99,12 +107,12 @@ class ProcedurePresentation < ApplicationRecord
(dossiers.order('dossiers.updated_at asc').ids - dossiers_id_with_notification) + (dossiers.order('dossiers.updated_at asc').ids - dossiers_id_with_notification) +
dossiers_id_with_notification dossiers_id_with_notification
end end
when 'type_de_champ' when TYPE_DE_CHAMP
dossiers dossiers
.with_type_de_champ(column) .with_type_de_champ(column)
.order("champs.value #{order}") .order("champs.value #{order}")
.pluck(:id) .pluck(:id)
when 'type_de_champ_private' when TYPE_DE_CHAMP_PRIVATE
dossiers dossiers
.with_type_de_champ_private(column) .with_type_de_champ_private(column)
.order("champs.value #{order}") .order("champs.value #{order}")
@ -125,7 +133,7 @@ class ProcedurePresentation < ApplicationRecord
end end
def filtered_ids(dossiers, statut) def filtered_ids(dossiers, statut)
filters[statut].group_by { |filter| filter.values_at('table', 'column') } .map do |(table, column), filters| filters[statut].group_by { |filter| filter.values_at(TABLE, COLUMN) } .map do |(table, column), filters|
values = filters.pluck('value') values = filters.pluck('value')
case table case table
when 'self' when 'self'
@ -133,10 +141,10 @@ class ProcedurePresentation < ApplicationRecord
.map { |v| Time.zone.parse(v).beginning_of_day rescue nil } .map { |v| Time.zone.parse(v).beginning_of_day rescue nil }
.compact .compact
dossiers.filter_by_datetimes(column, dates) dossiers.filter_by_datetimes(column, dates)
when 'type_de_champ' when TYPE_DE_CHAMP
dossiers.with_type_de_champ(column) dossiers.with_type_de_champ(column)
.filter_ilike(:champs, :value, values) .filter_ilike(:champs, :value, values)
when 'type_de_champ_private' when TYPE_DE_CHAMP_PRIVATE
dossiers.with_type_de_champ_private(column) dossiers.with_type_de_champ_private(column)
.filter_ilike(:champs_private, :value, values) .filter_ilike(:champs_private, :value, values)
when 'etablissement' when 'etablissement'
@ -172,14 +180,14 @@ class ProcedurePresentation < ApplicationRecord
def eager_load_displayed_fields(dossiers) def eager_load_displayed_fields(dossiers)
relations_to_include = displayed_fields relations_to_include = displayed_fields
.pluck('table') .pluck(TABLE)
.reject { |table| table == 'self' } .reject { |table| table == 'self' }
.map do |table| .map do |table|
case table case table
when 'type_de_champ' when TYPE_DE_CHAMP
:champs { champs: :type_de_champ }
when 'type_de_champ_private' when TYPE_DE_CHAMP_PRIVATE
:champs_private { champs_private: :type_de_champ }
else else
table table
end end
@ -190,9 +198,9 @@ class ProcedurePresentation < ApplicationRecord
end end
def human_value_for_filter(filter) def human_value_for_filter(filter)
case filter['table'] case filter[TABLE]
when 'type_de_champ', 'type_de_champ_private' when TYPE_DE_CHAMP, TYPE_DE_CHAMP_PRIVATE
find_type_de_champ(filter['column']).dynamic_type.filter_to_human(filter['value']) find_type_de_champ(filter[COLUMN]).dynamic_type.filter_to_human(filter['value'])
else else
filter['value'] filter['value']
end end
@ -200,19 +208,19 @@ class ProcedurePresentation < ApplicationRecord
def add_filter(statut, field, value) def add_filter(statut, field, value)
if value.present? if value.present?
table, column = field.split('/') table, column = field.split(SLASH)
label = find_field(table, column)['label'] label = find_field(table, column)['label']
case table case table
when 'type_de_champ', 'type_de_champ_private' when TYPE_DE_CHAMP, TYPE_DE_CHAMP_PRIVATE
value = find_type_de_champ(column).dynamic_type.human_to_filter(value) value = find_type_de_champ(column).dynamic_type.human_to_filter(value)
end end
updated_filters = filters.dup updated_filters = filters.dup
updated_filters[statut] << { updated_filters[statut] << {
'label' => label, 'label' => label,
'table' => table, TABLE => table,
'column' => column, COLUMN => column,
'value' => value 'value' => value
} }
@ -221,11 +229,11 @@ class ProcedurePresentation < ApplicationRecord
end end
def remove_filter(statut, field, value) def remove_filter(statut, field, value)
table, column = field.split('/') table, column = field.split(SLASH)
updated_filters = filters.dup updated_filters = filters.dup
updated_filters[statut] = filters[statut].reject do |filter| updated_filters[statut] = filters[statut].reject do |filter|
filter.values_at('table', 'column', 'value') == [table, column, value] filter.values_at(TABLE, COLUMN, 'value') == [table, column, value]
end end
update!(filters: updated_filters) update!(filters: updated_filters)
@ -236,7 +244,7 @@ class ProcedurePresentation < ApplicationRecord
values = [] values = []
end end
fields = values.map { |value| find_field(*value.split('/')) } fields = values.map { |value| find_field(*value.split(SLASH)) }
update!(displayed_fields: fields) update!(displayed_fields: fields)
@ -246,15 +254,15 @@ class ProcedurePresentation < ApplicationRecord
end end
def update_sort(table, column) def update_sort(table, column)
order = if sort.values_at('table', 'column') == [table, column] order = if sort.values_at(TABLE, COLUMN) == [table, column]
sort['order'] == 'asc' ? 'desc' : 'asc' sort['order'] == 'asc' ? 'desc' : 'asc'
else else
'asc' 'asc'
end end
update!(sort: { update!(sort: {
'table' => table, TABLE => table,
'column' => column, COLUMN => column,
'order' => order 'order' => order
}) })
end end
@ -262,11 +270,11 @@ class ProcedurePresentation < ApplicationRecord
private private
def field_id(field) def field_id(field)
field.values_at('table', 'column').join('/') field.values_at(TABLE, COLUMN).join(SLASH)
end end
def find_field(table, column) def find_field(table, column)
fields.find { |field| field.values_at('table', 'column') == [table, column] } fields.find { |field| field.values_at(TABLE, COLUMN) == [table, column] }
end end
def find_type_de_champ(column) def find_type_de_champ(column)
@ -300,7 +308,7 @@ class ProcedurePresentation < ApplicationRecord
end end
def check_allowed_field(kind, field, extra_columns = {}) def check_allowed_field(kind, field, extra_columns = {})
table, column = field.values_at('table', 'column') table, column = field.values_at(TABLE, COLUMN)
if !valid_column?(table, column, extra_columns) if !valid_column?(table, column, extra_columns)
errors.add(kind, "#{table}.#{column} nest pas une colonne permise") errors.add(kind, "#{table}.#{column} nest pas une colonne permise")
end end
@ -314,9 +322,9 @@ class ProcedurePresentation < ApplicationRecord
dossier.send(table)&.send(column) dossier.send(table)&.send(column)
when 'followers_instructeurs' when 'followers_instructeurs'
dossier.send(table)&.map { |g| g.send(column) }&.join(', ') dossier.send(table)&.map { |g| g.send(column) }&.join(', ')
when 'type_de_champ' when TYPE_DE_CHAMP
dossier.champs.find { |c| c.stable_id == column.to_i }.to_s dossier.champs.find { |c| c.stable_id == column.to_i }.to_s
when 'type_de_champ_private' when TYPE_DE_CHAMP_PRIVATE
dossier.champs_private.find { |c| c.stable_id == column.to_i }.to_s dossier.champs_private.find { |c| c.stable_id == column.to_i }.to_s
when 'groupe_instructeur' when 'groupe_instructeur'
dossier.groupe_instructeur.label dossier.groupe_instructeur.label
@ -326,8 +334,8 @@ class ProcedurePresentation < ApplicationRecord
def field_hash(label, table, column) def field_hash(label, table, column)
{ {
'label' => label, 'label' => label,
'table' => table, TABLE => table,
'column' => column COLUMN => column
} }
end end
@ -338,8 +346,8 @@ class ProcedurePresentation < ApplicationRecord
def valid_columns_for_table(table) def valid_columns_for_table(table)
@column_whitelist ||= fields @column_whitelist ||= fields
.group_by { |field| field['table'] } .group_by { |field| field[TABLE] }
.transform_values { |fields| Set.new(fields.pluck('column')) } .transform_values { |fields| Set.new(fields.pluck(COLUMN)) }
@column_whitelist[table] || [] @column_whitelist[table] || []
end end