| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
 | require 'csv'
namespace :import 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
  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]
    importer = SimpleImporter.create configuration_name: args[:configuration_name], filepath: args[:filepath]
    if args[:referential_id].present?
      referential = Referential.find args[:referential_id]
      importer.configure do |config|
        config.add_value :referential, referential
        config.context = {referential: referential, output_dir: args[: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
  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]
    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]
    end
  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]
    referential = Referential.find args[:referential_id]
    referential.switch
    stop_area_referential = referential.stop_area_referential
    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]
    end
  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]
    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]
    end
  end
end
 |