aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZog2018-04-04 11:06:27 +0200
committerJohan Van Ryseghem2018-05-28 10:34:19 +0200
commitac79e03aced3969df8626f15165a0f6e6f783295 (patch)
tree63f6cfd0bf4212e184e35075484ce8ac9e45f48a
parent72f9cc6d810fd2a2226e9b1f0c74b8fbfbb2811c (diff)
downloadchouette-core-ac79e03aced3969df8626f15165a0f6e6f783295.tar.bz2
Refs #6216; Uniformize output folders
And fix logs
-rw-r--r--app/models/simple_importer.rb16
-rw-r--r--app/models/simple_interface.rb7
-rw-r--r--app/models/simple_interfaces_group.rb4
-rw-r--r--lib/tasks/exports.rake14
4 files changed, 24 insertions, 17 deletions
diff --git a/app/models/simple_importer.rb b/app/models/simple_importer.rb
index 4cfe90cff..d47ff6a92 100644
--- a/app/models/simple_importer.rb
+++ b/app/models/simple_importer.rb
@@ -42,8 +42,10 @@ class SimpleImporter < SimpleInterface
end
def dump_csv_from_context
- dir = context[:logs_output_dir] || "log/importers"
- filepath = File.join dir, "#{self.configuration_name}_#{Time.now.strftime "%y%m%d%H%M"}.csv"
+ dir = @output_dir
+ FileUtils.mkdir_p dir
+
+ filepath = File.join dir, "#{self.configuration_name}_#{Time.now.strftime "%y%m%d%H%M"}_dump.csv"
# for some reason, context[:csv].to_csv does not work
CSV.open(filepath, 'w') do |csv|
header = true
@@ -131,14 +133,16 @@ class SimpleImporter < SimpleInterface
@current_attribute = col[:attribute]
val = col[:value]
if val.nil? || val.is_a?(Proc)
- if row.has_key? col.name
- if val.is_a?(Proc)
+ if val.is_a?(Proc)
+ if row.has_key? col.name
val = instance_exec(row[col.name], &val)
else
- val = row[col.name]
+ val = instance_exec(&val)
end
+ elsif row.has_key? col.name
+ val = row[col.name]
else
- push_in_journal({event: :column_not_found, message: "Column not found: #{col.name}", kind: :warning})
+ push_in_journal({event: :column_not_found, message: "Column not found: #{col.name || col.attribute}", kind: :warning})
self.status ||= :success_with_warnings
end
end
diff --git a/app/models/simple_interface.rb b/app/models/simple_interface.rb
index 7b04a07df..d5feafb07 100644
--- a/app/models/simple_interface.rb
+++ b/app/models/simple_interface.rb
@@ -97,13 +97,14 @@ class SimpleInterface < ApplicationModel
def write_output_to_csv
cols = %i(line kind event message error)
- if self.journal.size > 0 && self.journal.first[:row].present?
+ journal = self.journal && self.journal.map(&:symbolize_keys)
+ if journal && journal.size > 0 && journal.first[:row].present?
log "Writing output log"
FileUtils.mkdir_p @output_dir
- keys = self.journal.first[:row].map(&:first)
+ keys = journal.first[:row].map(&:first)
CSV.open(output_filepath, "w") do |csv|
csv << cols + keys
- self.journal.each do |j|
+ journal.each do |j|
csv << cols.map{|c| j[c]} + j[:row].map(&:last)
end
end
diff --git a/app/models/simple_interfaces_group.rb b/app/models/simple_interfaces_group.rb
index 808be6570..53e28c36a 100644
--- a/app/models/simple_interfaces_group.rb
+++ b/app/models/simple_interfaces_group.rb
@@ -30,7 +30,9 @@ class SimpleInterfacesGroup
name = "### #{self.name} ###"
centered_name = " " * ([width - name.size, 0].max / 2) + name
- banner = [centered_name, ""]
+ banner = [centered_name]
+ banner << "Output to: #{shared_options[:output_dir]}" if shared_options[:output_dir]
+ banner << ""
banner << @interfaces.each_with_index.map do |interface, i|
if interface[:interface].status.present?
SimpleInterface.colorize interface[:name], SimpleInterface.status_color(interface[:interface].status)
diff --git a/lib/tasks/exports.rake b/lib/tasks/exports.rake
index 845d581d3..33fbf81ed 100644
--- a/lib/tasks/exports.rake
+++ b/lib/tasks/exports.rake
@@ -52,9 +52,9 @@ namespace :export do
puts "No maching journeys were found".red
else
exports_group = SimpleInterfacesGroup.new "Export Complet \"#{referential.name}\" du #{Time.now.to_date} au #{args[:timelapse].to_i.days.from_now.to_date}"
- exports_group.shared_options = {verbose: true}
+ exports_group.shared_options = {verbose: true, output_dir: args[:output_dir]}
- exporter = SimpleJsonExporter.create configuration_name: "#{args[:configuration_name]}_companies", filepath: "#{args[:output_dir]}/#{args[:configuration_name]}_companies.json"
+ exporter = SimpleJsonExporter.create configuration_name: "#{args[:configuration_name]}_companies", filepath: "#{args[:output_dir]}/service_type.json"
ids = journeys.pluck :company_id
ids += journeys.joins(route: :line).pluck :"lines.company_id"
@@ -64,28 +64,28 @@ namespace :export do
exports_group.add_interface exporter, "Services Types", :export
- exporter = SimpleJsonExporter.create configuration_name: "#{args[:configuration_name]}_schedules", filepath: "#{args[:output_dir]}/#{args[:configuration_name]}_schedules.json"
+ exporter = SimpleJsonExporter.create configuration_name: "#{args[:configuration_name]}_schedules", filepath: "#{args[:output_dir]}/schedule.json"
exporter.configure do |config|
- config.collection = journeys
+ config.collection = journeys.where("custom_field_values->>'capacity' IS NOT NULL")
end
exports_group.add_interface exporter, "Schedules", :export
- exporter = SimpleJsonExporter.create configuration_name: "#{args[:configuration_name]}_routes", filepath: "#{args[:output_dir]}/#{args[:configuration_name]}_routes.json"
+ exporter = SimpleJsonExporter.create configuration_name: "#{args[:configuration_name]}_routes", filepath: "#{args[:output_dir]}/route.json"
exporter.configure do |config|
config.collection = Chouette::JourneyPattern.where(id: journeys.pluck(:journey_pattern_id).uniq)
end
exports_group.add_interface exporter, "Routes", :export
- exporter = SimpleJsonExporter.create configuration_name: "#{args[:configuration_name]}_stops", filepath: "#{args[:output_dir]}/#{args[:configuration_name]}_stops.json"
+ exporter = SimpleJsonExporter.create configuration_name: "#{args[:configuration_name]}_stops", filepath: "#{args[:output_dir]}/station.json"
exporter.configure do |config|
config.collection = Chouette::StopArea.where(id: journeys.joins(:stop_points).pluck(:"stop_points.stop_area_id").uniq).order('parent_id ASC NULLS FIRST')
end
exports_group.add_interface exporter, "Stops", :export
- exporter = SimpleJsonExporter.create configuration_name: "#{args[:configuration_name]}_journeys", filepath: "#{args[:output_dir]}/#{args[:configuration_name]}_journeys.json"
+ exporter = SimpleJsonExporter.create configuration_name: "#{args[:configuration_name]}_journeys", filepath: "#{args[:output_dir]}/service.json"
exporter.configure do |config|
config.collection = journeys
end