diff options
| author | Zog | 2018-03-02 11:17:08 +0100 |
|---|---|---|
| committer | Zog | 2018-03-02 11:17:08 +0100 |
| commit | c1ac12174b9aff7535a84af9f76d1cda95b750f4 (patch) | |
| tree | 1207cd5a338cb9d7868bb893a50ef19978515b54 /lib/tasks | |
| parent | 6e694fb2d88fe29049ecf5bb3d0e89e0585bf6c5 (diff) | |
| download | chouette-core-c1ac12174b9aff7535a84af9f76d1cda95b750f4.tar.bz2 | |
Refs #6068; Refactor import/export tasks
Diffstat (limited to 'lib/tasks')
| -rw-r--r-- | lib/tasks/exports.rake | 47 | ||||
| -rw-r--r-- | lib/tasks/helpers/simple_interfaces.rb | 27 | ||||
| -rw-r--r-- | lib/tasks/imports.rake | 88 |
3 files changed, 97 insertions, 65 deletions
diff --git a/lib/tasks/exports.rake b/lib/tasks/exports.rake new file mode 100644 index 000000000..036d96b11 --- /dev/null +++ b/lib/tasks/exports.rake @@ -0,0 +1,47 @@ +require 'csv' +require 'tasks/helpers/simple_interfaces' + +namespace :export do + desc "Notify parent imports when children finish" + task notify_parent: :environment do + ParentNotifier.new(Import).notify_when_finished + end + + desc "Mark old unfinished Netex imports as 'aborted'" + task netex_abort_old: :environment do + NetexImport.abort_old + end + + desc "export companies in the give LineReferential using the given exporter" + task :companies, [:referential_id, :configuration_name, :filepath, :logs_output_dir] => :environment do |t, args| + args.with_defaults(filepath: "./companies.csv", logs_output_dir: "./log/exporters/") + FileUtils.mkdir_p args[:logs_output_dir] + + referential = LineReferential.find args[:referential_id] + exporter = SimpleExporter.create configuration_name: args[:configuration_name], filepath: args[:filepath] + exporter.configure do |config| + config.collection = referential.companies.order(:name) + end + + SimpleInterfacesHelper.run_interface_controlling_interruption exporter, :export, args + end + + desc "export lines in the give LineReferential using the given exporter" + task :lines, [:referential_id, :configuration_name, :filepath, :logs_output_dir] => :environment do |t, args| + args.with_defaults(filepath: "./companies.csv", logs_output_dir: "./log/exporters/") + FileUtils.mkdir_p args[:logs_output_dir] + + referential = LineReferential.find args[:referential_id] + exporter = SimpleExporter.create configuration_name: args[:configuration_name], filepath: args[:filepath] + exporter.configure do |config| + config.collection = referential.lines.order(:name) + end + + SimpleInterfacesHelper.run_interface_controlling_interruption exporter, :export, args + end + + desc "export a complete offer from the gicen referential in the given X next days" + task :full_offer, [:referential_id, :timelapse, :configuration_name, :output_dir, :logs_output_dir] => :environment do |t, args| + args.with_defaults(filepath: "./companies.csv", logs_output_dir: "./log/exporters/") + end +end diff --git a/lib/tasks/helpers/simple_interfaces.rb b/lib/tasks/helpers/simple_interfaces.rb new file mode 100644 index 000000000..68e02e818 --- /dev/null +++ b/lib/tasks/helpers/simple_interfaces.rb @@ -0,0 +1,27 @@ +module SimpleInterfacesHelper + def self.interface_output_to_csv interface, output_dir + filepath = File.join output_dir, + "#{interface.configuration_name}_#{Time.now.strftime "%y%m%d%H%M"}_out.csv" + cols = %w(line kind event message error) + if interface.reload.journal.size > 0 + keys = interface.journal.first["row"].map(&:first) + CSV.open(filepath, "w") do |csv| + csv << cols + keys + interface.journal.each do |j| + csv << cols.map{|c| j[c]} + j["row"].map(&:last) + end + end + puts "Task Output written in #{filepath}" + end + end + + def self.run_interface_controlling_interruption interface, method, args + begin + interface.send(method, verbose: true) + rescue Interrupt + raise + ensure + puts "\n\e[33m***\e[0m Done, status: " + (interface.status == "success" ? "\e[32m" : "\e[31m" ) + (interface.status || "") + "\e[0m" + interface_output_to_csv interface, args[:logs_output_dir] + end + end +end diff --git a/lib/tasks/imports.rake b/lib/tasks/imports.rake index f01d3f34f..cd9217e5a 100644 --- a/lib/tasks/imports.rake +++ b/lib/tasks/imports.rake @@ -1,4 +1,5 @@ require 'csv' +require 'tasks/helpers/simple_interfaces' namespace :import do desc "Notify parent imports when children finish" @@ -11,25 +12,10 @@ namespace :import do NetexImport.abort_old end - def importer_output_to_csv importer, output_dir - filepath = File.join output_dir, + "#{importer.configuration_name}_#{Time.now.strftime "%y%m%d%H%M"}_out.csv" - cols = %w(line kind event message error) - if importer.reload.journal.size > 0 - keys = importer.journal.first["row"].map(&:first) - CSV.open(filepath, "w") do |csv| - csv << cols + keys - importer.journal.each do |j| - csv << cols.map{|c| j[c]} + j["row"].map(&:last) - end - end - puts "Import Output written in #{filepath}" - end - end - desc "import the given file with the corresponding importer" - task :import, [:configuration_name, :filepath, :referential_id, :output_dir] => :environment do |t, args| - args.with_defaults(output_dir: "./log/importers/") - FileUtils.mkdir_p args[:output_dir] + task :import, [:configuration_name, :filepath, :referential_id, :logs_output_dir] => :environment do |t, args| + args.with_defaults(logs_output_dir: "./log/importers/") + FileUtils.mkdir_p args[:logs_output_dir] importer = SimpleImporter.create configuration_name: args[:configuration_name], filepath: args[:filepath] @@ -37,46 +23,32 @@ namespace :import do referential = Referential.find args[:referential_id] importer.configure do |config| config.add_value :referential, referential - config.context = {referential: referential, output_dir: args[:output_dir]} + config.context = {referential: referential, logs_output_dir: args[:logs_output_dir]} end end - puts "\e[33m***\e[0m Start importing" - begin - importer.import(verbose: true) - rescue Interrupt - raise - ensure - puts "\n\e[33m***\e[0m Import done, status: " + (importer.status == "success" ? "\e[32m" : "\e[31m" ) + (importer.status || "") + "\e[0m" - importer_output_to_csv importer, args[:output_dir] - end + + SimpleInterfacesHelper.run_interface_controlling_interruption importer, :import, args end desc "import the given file with the corresponding importer in the given StopAreaReferential" task :import_in_stop_area_referential, [:referential_id, :configuration_name, :filepath] => :environment do |t, args| - args.with_defaults(output_dir: "./log/importers/") - FileUtils.mkdir_p args[:output_dir] + args.with_defaults(logs_output_dir: "./log/importers/") + FileUtils.mkdir_p args[:logs_output_dir] referential = StopAreaReferential.find args[:referential_id] importer = SimpleImporter.create configuration_name: args[:configuration_name], filepath: args[:filepath] importer.configure do |config| config.add_value :stop_area_referential, referential - config.context = {stop_area_referential: referential, output_dir: args[:output_dir]} - end - puts "\e[33m***\e[0m Start importing" - begin - importer.import(verbose: true) - rescue Interrupt - raise - ensure - puts "\n\e[33m***\e[0m Import done, status: " + (importer.status == "success" ? "\e[32m" : "\e[31m" ) + (importer.status || "") + "\e[0m" - importer_output_to_csv importer, args[:output_dir] + config.context = {stop_area_referential: referential, logs_output_dir: args[:logs_output_dir]} end + + SimpleInterfacesHelper.run_interface_controlling_interruption importer, :import, args end desc "import the given routes files" task :import_routes, [:referential_id, :configuration_name, :mapping_filepath, :filepath] => :environment do |t, args| - args.with_defaults(output_dir: "./log/importers/") - FileUtils.mkdir_p args[:output_dir] + args.with_defaults(logs_output_dir: "./log/importers/") + FileUtils.mkdir_p args[:logs_output_dir] referential = Referential.find args[:referential_id] referential.switch @@ -84,38 +56,24 @@ namespace :import do importer = SimpleImporter.create configuration_name: args[:configuration_name], filepath: args[:filepath] importer.configure do |config| config.add_value :stop_area_referential, referential - config.context = {stop_area_referential: stop_area_referential, mapping_filepath: args[:mapping_filepath], output_dir: args[:output_dir]} - end - puts "\e[33m***\e[0m Start importing" - begin - importer.import(verbose: true) - rescue Interrupt - raise - ensure - puts "\n\e[33m***\e[0m Import done, status: " + (importer.status == "success" ? "\e[32m" : "\e[31m" ) + (importer.status || "") + "\e[0m" - importer_output_to_csv importer, args[:output_dir] + config.context = {stop_area_referential: stop_area_referential, mapping_filepath: args[:mapping_filepath], logs_output_dir: args[:logs_output_dir]} end + + SimpleInterfacesHelper.run_interface_controlling_interruption importer, :import, args end desc "import the given file with the corresponding importer in the given LineReferential" - task :import_in_line_referential, [:referential_id, :configuration_name, :filepath, :output_dir] => :environment do |t, args| - args.with_defaults(output_dir: "./log/importers/") - FileUtils.mkdir_p args[:output_dir] + task :import_in_line_referential, [:referential_id, :configuration_name, :filepath, :logs_output_dir] => :environment do |t, args| + args.with_defaults(logs_output_dir: "./log/importers/") + FileUtils.mkdir_p args[:logs_output_dir] referential = LineReferential.find args[:referential_id] importer = SimpleImporter.create configuration_name: args[:configuration_name], filepath: args[:filepath] importer.configure do |config| config.add_value :line_referential, referential - config.context = {line_referential: referential, output_dir: args[:output_dir]} - end - puts "\e[33m***\e[0m Start importing" - begin - importer.import(verbose: true) - rescue Interrupt - raise - ensure - puts "\n\e[33m***\e[0m Import done, status: " + (importer.status == "success" ? "\e[32m" : "\e[31m" ) + (importer.status || "") + "\e[0m" - importer_output_to_csv importer, args[:output_dir] + config.context = {line_referential: referential, logs_output_dir: args[:logs_output_dir]} end + + SimpleInterfacesHelper.run_interface_controlling_interruption importer, :import, args end end |
