diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/cron.rb | 78 | ||||
| -rw-r--r-- | lib/stif/permission_translator.rb | 2 | ||||
| -rw-r--r-- | lib/tasks/ci.rake | 26 | ||||
| -rw-r--r-- | lib/tasks/codifligne.rake | 8 | ||||
| -rw-r--r-- | lib/tasks/compliance_check_sets.rb | 11 | ||||
| -rw-r--r-- | lib/tasks/exports.rake | 14 | ||||
| -rw-r--r-- | lib/tasks/imports.rake | 10 | ||||
| -rw-r--r-- | lib/tasks/organisations.rake | 6 | ||||
| -rw-r--r-- | lib/tasks/reflex.rake | 8 | ||||
| -rw-r--r-- | lib/tasks/users.rake | 6 | ||||
| -rw-r--r-- | lib/tom_tom.rb | 28 | ||||
| -rw-r--r-- | lib/tom_tom/errors.rb | 4 | ||||
| -rw-r--r-- | lib/tom_tom/errors/matrix_remote_error.rb | 5 | ||||
| -rw-r--r-- | lib/tom_tom/matrix.rb | 20 |
14 files changed, 153 insertions, 73 deletions
diff --git a/lib/cron.rb b/lib/cron.rb new file mode 100644 index 000000000..fe010ffd9 --- /dev/null +++ b/lib/cron.rb @@ -0,0 +1,78 @@ +module Cron + class << self + + def every_day_at_3AM + sync_reflex + end + + def every_day_at_4AM + sync_codifligne + end + + def every_hour + sync_organizations + sync_users + end + + def every_5_minutes + check_import_operations + check_ccset_operations + end + + private + + def sync_organizations + begin + Organisation.portail_sync + rescue => e + Rails.logger.error(e.inspect) + end + end + + def sync_users + begin + User.portail_sync + rescue => e + Rails.logger.error(e.inspect) + end + end + + def sync_reflex + begin + sync = StopAreaReferential.find_by(name: 'Reflex').stop_area_referential_syncs.build + raise "reflex:sync aborted - There is already an synchronisation in progress" unless sync.valid? + sync.save + rescue => e + Rails.logger.warn(e.message) + end + end + + def sync_codifligne + begin + sync = LineReferential.find_by(name: 'CodifLigne').line_referential_syncs.build + raise "Codifligne:sync aborted - There is already an synchronisation in progress" unless sync.valid? + sync.save + rescue => e + Rails.logger.warn(e.message) + end + end + + def check_ccset_operations + begin + ParentNotifier.new(ComplianceCheckSet).notify_when_finished + ComplianceCheckSet.abort_old + rescue => e + Rails.logger.error(e.inspect) + end + end + + def check_import_operations + begin + ParentNotifier.new(Import::Base).notify_when_finished + Import::Netex.abort_old + rescue => e + Rails.logger.error(e.inspect) + end + end + end +end
\ No newline at end of file diff --git a/lib/stif/permission_translator.rb b/lib/stif/permission_translator.rb index 09a7c610c..aefe50fe1 100644 --- a/lib/stif/permission_translator.rb +++ b/lib/stif/permission_translator.rb @@ -48,7 +48,7 @@ module Stif def translation_table { "boiv:read-offer" => %w{sessions.create}, - "boiv:edit-offer" => all_destructive_permissions + %w{sessions.create}, + "boiv:edit-offer" => all_destructive_permissions + %w{sessions.create workbenches.update}, } end diff --git a/lib/tasks/ci.rake b/lib/tasks/ci.rake index 0723e4ccb..7ef867b71 100644 --- a/lib/tasks/ci.rake +++ b/lib/tasks/ci.rake @@ -14,10 +14,19 @@ namespace :ci do desc "Prepare CI build" task :setup do - cp "config/database.yml", "config/database.yml.orig" - cp "config/database/ci.yml", "config/database.yml" - puts "Use #{database_name} database" + unless ENV["IGNORE_YARN_INSTALL"] + # FIXME remove this specific behavior + # Managed by Dockerfile.build + sh "yarn --frozen-lockfile install" + end + + unless ENV["KEEP_DATABASE_CONFIG"] + # FIXME remove this specific behavior + cp "config/database.yml", "config/database.yml.orig" + cp "config/database/ci.yml", "config/database.yml" + end + puts "Use #{database_name} database" if parallel_tests? sh "RAILS_ENV=test rake parallel:drop parallel:create parallel:migrate" else @@ -25,6 +34,15 @@ namespace :ci do end end + task :fix_webpacker do + # Redefine webpacker:yarn_install to avoid --production + # in CI process + Rake::Task["webpacker:yarn_install"].clear + Rake::Task.define_task "webpacker:yarn_install" do + puts "Don't run yarn" + end + end + def git_branch if ENV['GIT_BRANCH'] =~ %r{/(.*)$} $1 @@ -52,7 +70,7 @@ namespace :ci do end task :assets do - sh "RAILS_ENV=test bundle exec rake assets:precompile i18n:js:export" + sh "RAILS_ENV=test bundle exec rake ci:fix_webpacker assets:precompile i18n:js:export" end task :jest do diff --git a/lib/tasks/codifligne.rake b/lib/tasks/codifligne.rake deleted file mode 100644 index dd5e99370..000000000 --- a/lib/tasks/codifligne.rake +++ /dev/null @@ -1,8 +0,0 @@ -namespace :codifligne do - desc "Sync lines, companies, networks, and group of lines from codifligne" - task sync: :environment do - sync = LineReferential.find_by(name: 'CodifLigne').line_referential_syncs.build - raise "Codifligne:sync aborted - There is already an synchronisation in progress" unless sync.valid? - sync.save if sync.valid? - end -end diff --git a/lib/tasks/compliance_check_sets.rb b/lib/tasks/compliance_check_sets.rb deleted file mode 100644 index c53c7f9ed..000000000 --- a/lib/tasks/compliance_check_sets.rb +++ /dev/null @@ -1,11 +0,0 @@ -namespace :compliance_check_sets do - desc "Notify parent check sets when children finish" - task notify_parent: :environment do - ParentNotifier.new(ComplianceCheckSet).notify_when_finished - end - - desc "Mark old unfinished check sets as 'aborted'" - task abort_old: :environment do - ComplianceCheckSet.abort_old - end -end 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 diff --git a/lib/tasks/imports.rake b/lib/tasks/imports.rake index d393ab156..258c37bbd 100644 --- a/lib/tasks/imports.rake +++ b/lib/tasks/imports.rake @@ -2,16 +2,6 @@ require 'csv' require 'tasks/helpers/simple_interfaces' namespace :import do - desc "Notify parent imports when children finish" - task notify_parent: :environment do - ParentNotifier.new(Import::Base).notify_when_finished - end - - desc "Mark old unfinished Netex imports as 'aborted'" - task netex_abort_old: :environment do - Import::Netex.abort_old - end - desc "import the given file with the corresponding importer" task :import, [:configuration_name, :filepath, :referential_id, :logs_output_dir] => :environment do |t, args| args.with_defaults(logs_output_dir: "./log/importers/") diff --git a/lib/tasks/organisations.rake b/lib/tasks/organisations.rake deleted file mode 100644 index 1b21d7119..000000000 --- a/lib/tasks/organisations.rake +++ /dev/null @@ -1,6 +0,0 @@ -namespace :organisations do - desc "Sync organisations from stif portail" - task sync: :environment do - Organisation.portail_sync - end -end diff --git a/lib/tasks/reflex.rake b/lib/tasks/reflex.rake deleted file mode 100644 index 67496cee0..000000000 --- a/lib/tasks/reflex.rake +++ /dev/null @@ -1,8 +0,0 @@ -namespace :reflex do - desc "Sync data from Reflex api" - task sync: :environment do - sync = StopAreaReferential.find_by(name: 'Reflex').stop_area_referential_syncs.build - raise "reflex:sync aborted - There is already an synchronisation in progress" unless sync.valid? - sync.save if sync.valid? - end -end diff --git a/lib/tasks/users.rake b/lib/tasks/users.rake deleted file mode 100644 index c045639c1..000000000 --- a/lib/tasks/users.rake +++ /dev/null @@ -1,6 +0,0 @@ -namespace :users do - desc "Sync users from stif portail" - task sync: :environment do - User.portail_sync - end -end diff --git a/lib/tom_tom.rb b/lib/tom_tom.rb index fcebcc7ac..91f1a3800 100644 --- a/lib/tom_tom.rb +++ b/lib/tom_tom.rb @@ -1,26 +1,30 @@ module TomTom BASE_URL = 'https://api.tomtom.com' - @api_key = Rails.application.secrets.tomtom_api_key - @connection = Faraday.new( - url: BASE_URL, - params: { - key: @api_key - } - ) do |faraday| - faraday.use FaradayMiddleware::FollowRedirects, limit: 1 - faraday.adapter Faraday.default_adapter + @@api_key = Rails.application.secrets.tomtom_api_key + cattr_accessor :api_key + + def self.connection + @connection ||= Faraday.new( + url: BASE_URL, + params: { + key: api_key + } + ) do |faraday| + faraday.use FaradayMiddleware::FollowRedirects, limit: 1 + faraday.adapter Faraday.default_adapter + end end def self.enabled? - @api_key.present? + api_key.present? && /[a-zA-Z0-9]{32}/ === api_key end def self.batch(way_costs) - TomTom::Batch.new(@connection).batch(way_costs) + TomTom::Batch.new(connection).batch(way_costs) end def self.matrix(way_costs) - TomTom::Matrix.new(@connection).matrix(way_costs) + TomTom::Matrix.new(connection).matrix(way_costs) end end diff --git a/lib/tom_tom/errors.rb b/lib/tom_tom/errors.rb new file mode 100644 index 000000000..da3f2239c --- /dev/null +++ b/lib/tom_tom/errors.rb @@ -0,0 +1,4 @@ +module TomTom + module Errors + end +end diff --git a/lib/tom_tom/errors/matrix_remote_error.rb b/lib/tom_tom/errors/matrix_remote_error.rb new file mode 100644 index 000000000..b13767847 --- /dev/null +++ b/lib/tom_tom/errors/matrix_remote_error.rb @@ -0,0 +1,5 @@ +module TomTom + module Errors + class MatrixRemoteError < RuntimeError; end + end +end diff --git a/lib/tom_tom/matrix.rb b/lib/tom_tom/matrix.rb index c418cd516..e779abbf7 100644 --- a/lib/tom_tom/matrix.rb +++ b/lib/tom_tom/matrix.rb @@ -4,6 +4,10 @@ module TomTom @connection = connection end + # Exceptions: + # + # * This raises a `TomTom::Errors::MatrixRemoteError` when the API responds + # with an error. def matrix(way_costs) points_with_ids = points_from_way_costs(way_costs) points = points_as_params(points_with_ids) @@ -21,6 +25,8 @@ module TomTom req.body = build_request_body(points) end + check_for_error_response(response) + extract_costs_to_way_costs!( way_costs, points_with_ids, @@ -77,6 +83,20 @@ module TomTom }) end + def check_for_error_response(response) + if response.status != 200 + raise TomTom::Errors::MatrixRemoteError, + "status: #{response.status}, body: #{response.body}" + end + + json = JSON.parse(response.body) + + if json.has_key?('error') + raise TomTom::Errors::MatrixRemoteError, + "status: #{response.status}, message: #{json['error']['description']}" + end + end + def extract_costs_to_way_costs!(way_costs, points, matrix_json) way_costs = [] |
