aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/chouette/vehicle_journey.rb4
-rw-r--r--app/models/simple_exporter.rb3
-rw-r--r--app/models/simple_interface.rb6
-rw-r--r--app/models/simple_json_exporter.rb53
-rw-r--r--lib/tasks/exports.rake30
5 files changed, 61 insertions, 35 deletions
diff --git a/app/models/chouette/vehicle_journey.rb b/app/models/chouette/vehicle_journey.rb
index 49a2b387e..194dc85ff 100644
--- a/app/models/chouette/vehicle_journey.rb
+++ b/app/models/chouette/vehicle_journey.rb
@@ -156,11 +156,11 @@ module Chouette
end
def sales_start
- purchase_windows.map{|p| p.date_ranges.first}.min
+ purchase_windows.map{|p| p.date_ranges.map &:first}.flatten.min
end
def sales_end
- purchase_windows.map{|p| p.date_ranges.last}.max
+ purchase_windows.map{|p| p.date_ranges.map &:last}.flatten.max
end
def calculate_vehicle_journey_at_stop_day_offset
diff --git a/app/models/simple_exporter.rb b/app/models/simple_exporter.rb
index adc48533f..721e56dda 100644
--- a/app/models/simple_exporter.rb
+++ b/app/models/simple_exporter.rb
@@ -99,6 +99,7 @@ class SimpleExporter < SimpleInterface
def handle_item item
number_of_lines = @number_of_lines
+ @current_item = item
map_item_to_rows(item).each_with_index do |item, i|
@number_of_lines = number_of_lines + i
@current_row = item.attributes
@@ -112,7 +113,7 @@ class SimpleExporter < SimpleInterface
end
push_in_journal({event: :success, kind: :log})
@statuses += @new_status
- print_state if @current_line % 20 == 0
+ print_state if @current_line % 20 == 0 || i > 0
@current_line += 1
@csv << row
end
diff --git a/app/models/simple_interface.rb b/app/models/simple_interface.rb
index 07fabd832..5a1c3dca8 100644
--- a/app/models/simple_interface.rb
+++ b/app/models/simple_interface.rb
@@ -33,7 +33,7 @@ class SimpleInterface < ActiveRecord::Base
@errors = []
@messages = []
@padding = 1
- @current_line = 0
+ @current_line = -1
@padding = [1, Math.log([@number_of_lines, 1].max, 10).ceil()].max
end
@@ -280,6 +280,10 @@ class SimpleInterface < ActiveRecord::Base
!!@options[:required]
end
+ def omit_nil?
+ !!@options[:omit_nil]
+ end
+
def scope
@options[:scope] || []
end
diff --git a/app/models/simple_json_exporter.rb b/app/models/simple_json_exporter.rb
index 706307de1..44ecfcba2 100644
--- a/app/models/simple_json_exporter.rb
+++ b/app/models/simple_json_exporter.rb
@@ -68,7 +68,9 @@ class SimpleJsonExporter < SimpleExporter
def resolve_node item, node
vals = []
- [item.send(node.attribute)].flatten.each do |node_item|
+ scoped_item = node.scope.inject(item){|tmp, scope| tmp.send(scope)}
+
+ [scoped_item.send(node.attribute)].flatten.each do |node_item|
item_val = {}
apply_configuration node_item, node.configuration, item_val
vals.push item_val
@@ -79,7 +81,7 @@ class SimpleJsonExporter < SimpleExporter
def apply_configuration item, configuration, output
configuration.columns.each do |col|
val = resolve_value item, col
- output[col.name] = val
+ output[col.name] = val unless val.nil? && col.omit_nil?
end
configuration.nodes.each do |node|
@@ -89,20 +91,25 @@ class SimpleJsonExporter < SimpleExporter
end
def handle_item item
- serialized_item = {}
- @current_row = item.attributes
- @current_row = @current_row.slice(*configuration.logged_attributes) if configuration.logged_attributes.present?
- @new_status = nil
+ number_of_lines = @number_of_lines
+ @current_item = item
+ map_item_to_rows(item).each_with_index do |item, i|
+ @number_of_lines = number_of_lines + i
+ serialized_item = {}
+ @current_row = item.attributes
+ @current_row = @current_row.slice(*configuration.logged_attributes) if configuration.logged_attributes.present?
+ @new_status = nil
- apply_configuration item, self.configuration, serialized_item
+ apply_configuration item, self.configuration, serialized_item
- @new_status ||= colorize("✓", :green)
+ @new_status ||= colorize("✓", :green)
- push_in_journal({event: :success, kind: :log})
- @statuses += @new_status
- print_state if @current_line % 20 == 0
- @current_line += 1
- append_item serialized_item
+ push_in_journal({event: :success, kind: :log})
+ @statuses += @new_status
+ print_state if @current_line % 20 == 0 || i > 0
+ @current_line += 1
+ append_item serialized_item
+ end
end
def append_item serialized_item
@@ -116,13 +123,23 @@ class SimpleJsonExporter < SimpleExporter
alias_method :add_field, :add_column
def initialize import_name, opts={}
- @nodes = []
super import_name, opts
+ @collection = opts[:collection]
+ @nodes = opts[:nodes] || []
+ @root = opts[:root]
+ end
+
+ def options
+ super.update({
+ nodes: @nodes,
+ root: @root,
+ })
end
def add_node name, opts={}
@nodes ||= []
- node = Node.new({name: name.to_s}.update(opts))
+ @scope ||= []
+ node = Node.new({name: name.to_s, scope: @scope.dup}.update(opts))
yield node.configuration
@nodes.push node
end
@@ -154,11 +171,15 @@ class SimpleJsonExporter < SimpleExporter
end
def attribute
- name
+ @options[:attribute] || name
end
def multiple
!!@options[:multiple]
end
+
+ def scope
+ @options[:scope] || []
+ end
end
end
diff --git a/lib/tasks/exports.rake b/lib/tasks/exports.rake
index 8688611e8..547388b35 100644
--- a/lib/tasks/exports.rake
+++ b/lib/tasks/exports.rake
@@ -43,7 +43,7 @@ namespace :export do
desc "export a complete offer from the given referential in the given X next days"
task :full_offer, [:referential_id, :configuration_name, :timelapse, :output_dir, :logs_output_dir] => :environment do |t, args|
referential = Referential.find args[:referential_id]
- args.with_defaults(output_dir: "#{referential.name.parameterize}_#{Time.now.strftime "%y%m%d%H%M"}", logs_output_dir: "./log/exporters/", timelapse: 90)
+ args.with_defaults(output_dir: "#{referential.name.parameterize}/#{Time.now.strftime "%y%m%d%H%M"}", logs_output_dir: "./log/exporters/", timelapse: 90)
referential.switch
@@ -51,44 +51,44 @@ namespace :export do
if journeys.count == 0
puts "No maching journeys were found".red
else
- exporter = SimpleExporter.create configuration_name: "#{args[:configuration_name]}_journeys", filepath: "#{args[:output_dir]}/#{args[:configuration_name]}_journeys.csv"
+ exporter = SimpleJsonExporter.create configuration_name: "#{args[:configuration_name]}_companies", filepath: "#{args[:output_dir]}/#{args[:configuration_name]}_companies.json"
+ ids = journeys.pluck :company_id
+ ids += journeys.joins(route: :line).pluck :"lines.company_id"
+
exporter.configure do |config|
- config.collection = journeys
+ config.collection = Chouette::Company.where(id: ids.uniq).order('name')
end
SimpleInterfacesHelper.run_interface_controlling_interruption exporter, :export, args
- return unless exporter.status == :success
+ break if exporter.status == :error
- exporter = SimpleExporter.create configuration_name: "#{args[:configuration_name]}_schedules", filepath: "#{args[:output_dir]}/#{args[:configuration_name]}_schedules.csv"
+ exporter = SimpleJsonExporter.create configuration_name: "#{args[:configuration_name]}_schedules", filepath: "#{args[:output_dir]}/#{args[:configuration_name]}_schedules.json"
exporter.configure do |config|
config.collection = journeys
end
SimpleInterfacesHelper.run_interface_controlling_interruption exporter, :export, args
- return unless exporter.status == :success
+ break if exporter.status == :error
- exporter = SimpleExporter.create configuration_name: "#{args[:configuration_name]}_routes", filepath: "#{args[:output_dir]}/#{args[:configuration_name]}_routes.csv"
+ exporter = SimpleJsonExporter.create configuration_name: "#{args[:configuration_name]}_routes", filepath: "#{args[:output_dir]}/#{args[:configuration_name]}_routes.json"
exporter.configure do |config|
config.collection = Chouette::JourneyPattern.where(id: journeys.pluck(:journey_pattern_id).uniq)
end
SimpleInterfacesHelper.run_interface_controlling_interruption exporter, :export, args
- return unless exporter.status == :success
+ break if exporter.status == :error
- exporter = SimpleExporter.create configuration_name: "#{args[:configuration_name]}_stops", filepath: "#{args[:output_dir]}/#{args[:configuration_name]}_stops.csv"
+ exporter = SimpleJsonExporter.create configuration_name: "#{args[:configuration_name]}_stops", filepath: "#{args[:output_dir]}/#{args[:configuration_name]}_stops.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
SimpleInterfacesHelper.run_interface_controlling_interruption exporter, :export, args
- return unless exporter.status == :success
-
- exporter = SimpleExporter.create configuration_name: "#{args[:configuration_name]}_companies", filepath: "#{args[:output_dir]}/#{args[:configuration_name]}_companies.csv"
- ids = journeys.pluck :company_id
- ids += journeys.joins(route: :line).pluck :"lines.company_id"
+ break if exporter.status == :error
+ exporter = SimpleJsonExporter.create configuration_name: "#{args[:configuration_name]}_journeys", filepath: "#{args[:output_dir]}/#{args[:configuration_name]}_journeys.json"
exporter.configure do |config|
- config.collection = Chouette::Company.where(id: ids.uniq).order('name')
+ config.collection = journeys
end
SimpleInterfacesHelper.run_interface_controlling_interruption exporter, :export, args