Skip to content

Commit

Permalink
Merged in CHOUETTE-3161-refactor-gtfs-neptune-to-create-referential-w…
Browse files Browse the repository at this point in the history
…ith-builder (pull request #1544)

CHOUETTE-3161 Refactor GTFS/Neptune import to create referential with builder

Approved-by: Alban Peignier
  • Loading branch information
Hai Hieu Vu authored and albanpeignier committed May 15, 2024
2 parents 85ef3d5 + 8b5e19b commit a4390d9
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 80 deletions.
91 changes: 61 additions & 30 deletions app/models/concerns/local_import_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,27 @@ def import_type
def import
Chouette::Benchmark.measure "import_#{import_type}", id: id do
update status: 'running', started_at: Time.now
@progress = 0

ActiveRecord::Base.cache do
import_without_status
end

processor.after([referential])

@progress = nil
@status ||= 'successful'
referential&.active!
update status: @status, ended_at: Time.now
end
rescue InvalidReferential
update status: 'failed', ended_at: Time.now
if overlapping_referential_ids.present?
create_message criticity: :error, message_key: 'referential_creation_overlapping_existing_referential_block'
end
rescue StandardError => e
update status: 'failed', ended_at: Time.now
Chouette::Safe.capture "#{self.class.name} ##{id} failed", e

if (referential && overlapped_referential_ids = referential.overlapped_referential_ids).present?
overlapped = Referential.find overlapped_referential_ids.last
create_message(
criticity: :error,
message_attributes: {
referential_name: referential.name,
overlapped_name: overlapped.name,
overlapped_url: Rails.application.routes.url_helpers.workbench_referential_path(workbench, overlapped)
},
message_key: 'referential_creation_overlapping_existing_referential'
)
else
create_message criticity: :error, message_key: :full_text, message_attributes: { text: e.message }
end
create_message criticity: :error, message_key: :full_text, message_attributes: { text: e.message }
referential&.failed!
ensure
main_resource&.save
Expand All @@ -77,27 +67,68 @@ def import_resources(*resources)
end
end

def create_referential
Chouette::Benchmark.measure 'create_referential' do
self.referential ||= Referential.new(
name: referential_name,
organisation_id: workbench.organisation_id,
workbench_id: workbench.id,
metadatas: [referential_metadata],
def referential_builder
@referential_builder ||= ReferentialBuilder.new(workbench, name: referential_name, metadata: referential_metadata)
end

# Create a Referential with given name and medata
class ReferentialBuilder
def initialize(workbench, name:, metadata:)
@workbench = workbench
@name = name
@metadata = metadata
end
attr_reader :workbench, :name, :metadata

delegate :organisation, to: :workbench

def create(&block)
if valid?
Rails.logger.debug "Create imported Referential: #{referential.inspect}"
block.call referential
else
Rails.logger.debug "Can't created imported Referential: #{referential.inspect}"
end
end

def referential
@referential ||= workbench.referentials.create(
name: name,
organisation: organisation,
metadatas: [metadata],
ready: false
)
end

def valid?
@valid ||= referential.valid?
end

def overlapping_referential_ids
@overlapping_referential_ids ||= referential.overlapped_referential_ids
end
end

# DEPRECATED Use #within_referential
def create_referential
Chouette::Benchmark.measure 'create_referential' do
self.referential ||= referential_builder.referential

begin
self.referential.save!
rescue ActiveRecord::RecordInvalid
# No double capture for Chouette::Safe
Rails.logger.error "Unable to create referential: #{self.referential.errors.messages}"
raise
end
main_resource.update referential: referential if main_resource

unless referential_builder.valid?
self.overlapping_referential_ids = referential_builder.overlapping_referential_ids
raise InvalidReferential
end

referential
end
end

# DEPRECATED Use #within_referential
class InvalidReferential < StandardError
end

def referential_name
name.presence || File.basename(local_file.to_s)
end
Expand Down
52 changes: 6 additions & 46 deletions app/models/import/netex_generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,12 @@ def within_referential(&block)

return if referential_builder.valid?

# Create a global error message
messages.create criticity: :error, message_key: 'referential_creation_overlapping_existing_referential'
# Save overlapping referentials for user display
# self.overlapping_referential_ids = referential_builder.overlapping_referential_ids
end

def referential_builder
@referential_builder ||= ReferentialBuilder.new(workbench, name: name, metadata: referential_metadata)
# create_message has a strange behavior in this context
messages.build(
criticity: :error,
message_key: 'referential_creation_overlapping_existing_referential_block'
)
self.overlapping_referential_ids = referential_builder.overlapping_referential_ids
end

def referential_metadata
Expand All @@ -102,44 +100,6 @@ def referential_metadata
ReferentialMetadata.new line_ids: imported_line_ids, periodes: [netex_source.validity_period]
end

# Create a Referential with given name and medata
class ReferentialBuilder
def initialize(workbench, name:, metadata:)
@workbench = workbench
@name = name
@metadata = metadata
end
attr_reader :workbench, :name, :metadata

delegate :organisation, to: :workbench

def create(&block)
if valid?
Rails.logger.debug "Create imported Referential: #{referential.inspect}"
block.call referential
else
Rails.logger.debug "Can't created imported Referential: #{referential.inspect}"
end
end

def referential
@referential ||= workbench.referentials.create(
name: name,
organisation: organisation,
metadatas: [metadata],
ready: false
)
end

def valid?
@valid ||= referential.valid?
end

def overlapping_referential_ids
@overlapping_referential_ids ||= referential.overlapped_referential_ids
end
end

# TODO: why the resource statuses are not checked automaticaly ??
# See CHOUETTE-2747
def update_import_status
Expand Down
2 changes: 1 addition & 1 deletion config/locales/import_messages.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ en:
multiple_companies_in_file: "The file %{source_filename} contains multiple companies, imported lines have therefore not be associated."
referential_creation_missing_lines_in_files: "Le référentiel %{referential_name} has not been created because the file contains no offer data"
referential_creation_missing_lines: "The referential %{referential_name} has not been created because no line has been found in the organization"
# referential_creation_overlapping_existing_referential: "You are trying to import a file that overlaps DataSets in edition (see Overlapping section for details)."
referential_creation_overlapping_existing_referential_block: "You are trying to import a file that overlaps DataSets in edition (see Overlapping section for details)."
referential_creation_overlapping_existing_referential: "The referential %{referential_name} has not been created because another referential already exists for the same periods and lines: <a href='%{overlapped_url}'>%{overlapped_name}</a>"
referential_creation: "The referential %{referential_name} has not been created because another referential with the same lines and periods already exists"
trip_with_inconsistent_stop_times: The trip %{trip_id} has inconsistent stop times
Expand Down
2 changes: 1 addition & 1 deletion config/locales/import_messages.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fr:
multiple_companies_in_file: "Le fichier %{source_filename} contient plusieurs transporteurs, les lignes importées n'ont donc pas étées associées."
referential_creation_missing_lines_in_files: "Le référentiel %{referential_name} n'a pas pu être créé car aucune donnée d'offre n'est présente dans le fichier"
referential_creation_missing_lines: "Le référentiel %{referential_name} n'a pas pu être créé car aucune ligne n'appartient à l'organisation"
# referential_creation_overlapping_existing_referential: "Vous essayez d’importer un fichier qui chevauche des Jeux de Données en édition (consulter la section Chevauchement)."
referential_creation_overlapping_existing_referential_block: "Vous essayez d’importer un fichier qui chevauche des Jeux de Données en édition (consulter la section Chevauchement)."
referential_creation_overlapping_existing_referential: "Le référentiel %{referential_name} n'a pas pu être créé car un référentiel existe déjà sur les mêmes périodes et lignes: <a href='%{overlapped_url}'>%{overlapped_name}</a>"
referential_creation: "Le référentiel %{referential_name} n'a pas pu être créé."
trip_with_inconsistent_stop_times: La course %{trip_id} a des horaires incohérents
Expand Down
2 changes: 1 addition & 1 deletion config/locales/imports.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ en:
parent: Parent
status: State
overlapping: Overlapping
overlapping_tips: you can archive or delete these DataSets before restarting a new import
overlapping_tips: You can archive or delete these DataSets before restarting a new import
netex:
table_title: "Status of analyzed files"
table_explanation: "When calendriers.xml and/or commun.xml are not imported, then all lines file are not processed."
Expand Down
2 changes: 1 addition & 1 deletion config/locales/imports.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fr:
status: État
referential: Jeu de données
overlapping: Chevauchement
overlapping_tips: vous pouvez archiver ou supprimer ces Jeux de Données avant de relancer le même import
overlapping_tips: Vous pouvez archiver ou supprimer ces Jeux de Données avant de relancer le même import
netex:
table_title: "État des fichiers analysés"
table_explanation: "Dans le cas ou le(s) fichiers calendriers.xml et/ou commun.xml sont dans un état non importé, alors tous les fichiers lignes sont automatiquement dans un état non traité."
Expand Down

0 comments on commit a4390d9

Please sign in to comment.