aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXinhui2016-09-09 12:48:27 +0200
committerXinhui2016-09-09 12:48:49 +0200
commitc78802969f4d45028d3854198e311530813ae73b (patch)
tree720f119123b051fc609a534c54482c924c65760e
parenta40c2468c762f039061e71f2e2ba03358e5183ec (diff)
downloadchouette-core-c78802969f4d45028d3854198e311530813ae73b.tar.bz2
Model StopAreaReferentialSync
Refs #1609
-rw-r--r--app/models/stop_area_referential.rb1
-rw-r--r--app/models/stop_area_referential_sync.rb9
-rw-r--r--app/models/stop_area_sync_operation.rb3
-rw-r--r--config/locales/line_referentials.fr.yml9
-rw-r--r--config/locales/stop_area_referentials.en.yml5
-rw-r--r--config/locales/stop_area_referentials.fr.yml6
-rw-r--r--db/migrate/20160909092812_create_stop_area_referential_syncs.rb9
-rw-r--r--db/migrate/20160909093322_create_stop_area_sync_operations.rb12
-rw-r--r--db/schema.rb20
-rw-r--r--db/seeds.rb2
-rw-r--r--lib/stif/codif_line_synchronization.rb6
-rw-r--r--lib/stif/reflex_synchronization.rb59
-rw-r--r--spec/factories/stop_area_referential_syncs.rb15
-rw-r--r--spec/factories/stop_area_sync_operations.rb8
-rw-r--r--spec/models/stop_area_referential_sync_spec.rb24
-rw-r--r--spec/models/stop_area_sync_operation_spec.rb9
-rw-r--r--spec/tasks/reflex_rake_spec.rb9
17 files changed, 170 insertions, 36 deletions
diff --git a/app/models/stop_area_referential.rb b/app/models/stop_area_referential.rb
index 325385011..58e0c559c 100644
--- a/app/models/stop_area_referential.rb
+++ b/app/models/stop_area_referential.rb
@@ -3,6 +3,7 @@ class StopAreaReferential < ActiveRecord::Base
has_many :organisations, through: :stop_area_referential_memberships
has_many :stop_areas, class_name: 'Chouette::StopArea'
+ has_one :stop_area_referential_sync
def add_member(organisation, options = {})
attributes = options.merge organisation: organisation
diff --git a/app/models/stop_area_referential_sync.rb b/app/models/stop_area_referential_sync.rb
new file mode 100644
index 000000000..3a9b2d03b
--- /dev/null
+++ b/app/models/stop_area_referential_sync.rb
@@ -0,0 +1,9 @@
+class StopAreaReferentialSync < ActiveRecord::Base
+ belongs_to :stop_area_referential
+ has_many :stop_area_sync_operations, dependent: :destroy
+
+ def record_status status, message
+ stop_area_sync_operations << StopAreaSyncOperation.new(status: status, message: message)
+ stop_area_sync_operations.first.destroy while stop_area_sync_operations.count > 30
+ end
+end
diff --git a/app/models/stop_area_sync_operation.rb b/app/models/stop_area_sync_operation.rb
new file mode 100644
index 000000000..ca0fde4db
--- /dev/null
+++ b/app/models/stop_area_sync_operation.rb
@@ -0,0 +1,3 @@
+class StopAreaSyncOperation < ActiveRecord::Base
+ belongs_to :stop_area_referential_sync
+end
diff --git a/config/locales/line_referentials.fr.yml b/config/locales/line_referentials.fr.yml
index b18a53d5b..adb96cbe0 100644
--- a/config/locales/line_referentials.fr.yml
+++ b/config/locales/line_referentials.fr.yml
@@ -5,13 +5,14 @@ fr:
edit:
title: "Modifier le référentiel %{line_referential}"
synchronization:
- message:
- success: "Synchronisation réussie après %{time} secondes avec %{imported} éléments importés de Codifligne. %{deleted} éléments ont été supprimés."
- failure: "Synchronisation interrompue après %{time} secondes."
+ codifligne:
+ message:
+ success: "Synchronisation réussie après %{time} secondes avec %{imported} éléments importés de Codifligne. %{deleted} éléments ont été supprimés."
+ failure: "Synchronisation interrompue après %{time} secondes."
activerecord:
models:
line_referential:
one: "référentiel"
attributes:
line_referential:
- sync_interval: "Fréquence de synchronisation" \ No newline at end of file
+ sync_interval: "Fréquence de synchronisation"
diff --git a/config/locales/stop_area_referentials.en.yml b/config/locales/stop_area_referentials.en.yml
new file mode 100644
index 000000000..9ac0daede
--- /dev/null
+++ b/config/locales/stop_area_referentials.en.yml
@@ -0,0 +1,5 @@
+en:
+ synchronization:
+ reflex:
+ message: "Synchronization successful in %{time} seconds with %{imported} objects from Reflex. %{deleted} objects were deleted."
+ failure: "Synchronization interrupted after %{time} seconds."
diff --git a/config/locales/stop_area_referentials.fr.yml b/config/locales/stop_area_referentials.fr.yml
new file mode 100644
index 000000000..89254384f
--- /dev/null
+++ b/config/locales/stop_area_referentials.fr.yml
@@ -0,0 +1,6 @@
+fr:
+ synchronization:
+ reflex:
+ message:
+ success: "Synchronisation réussie après %{time} secondes avec %{imported} éléments importés de Reflex. %{deleted} éléments ont été supprimés."
+ failure: "Synchronisation interrompue après %{time} secondes."
diff --git a/db/migrate/20160909092812_create_stop_area_referential_syncs.rb b/db/migrate/20160909092812_create_stop_area_referential_syncs.rb
new file mode 100644
index 000000000..eb68f662b
--- /dev/null
+++ b/db/migrate/20160909092812_create_stop_area_referential_syncs.rb
@@ -0,0 +1,9 @@
+class CreateStopAreaReferentialSyncs < ActiveRecord::Migration
+ def change
+ create_table :stop_area_referential_syncs do |t|
+ t.references :stop_area_referential, index: true
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/migrate/20160909093322_create_stop_area_sync_operations.rb b/db/migrate/20160909093322_create_stop_area_sync_operations.rb
new file mode 100644
index 000000000..fef4f5e1f
--- /dev/null
+++ b/db/migrate/20160909093322_create_stop_area_sync_operations.rb
@@ -0,0 +1,12 @@
+class CreateStopAreaSyncOperations < ActiveRecord::Migration
+ def change
+ create_table :stop_area_sync_operations do |t|
+ t.string :status
+ t.references :stop_area_referential_sync
+ t.string :message
+
+ t.timestamps
+ end
+ add_index :stop_area_sync_operations, :stop_area_referential_sync_id, name: 'stop_area_referential_sync_id'
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index a5b18db68..0db23d55f 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20160905094930) do
+ActiveRecord::Schema.define(version: 20160909093322) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -449,12 +449,30 @@ ActiveRecord::Schema.define(version: 20160905094930) do
t.boolean "owner"
end
+ create_table "stop_area_referential_syncs", force: true do |t|
+ t.integer "stop_area_referential_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ add_index "stop_area_referential_syncs", ["stop_area_referential_id"], :name => "index_stop_area_referential_syncs_on_stop_area_referential_id"
+
create_table "stop_area_referentials", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
+ create_table "stop_area_sync_operations", force: true do |t|
+ t.string "status"
+ t.integer "stop_area_referential_sync_id"
+ t.string "message"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ add_index "stop_area_sync_operations", ["stop_area_referential_sync_id"], :name => "stop_area_referential_sync_id"
+
create_table "stop_areas", force: true do |t|
t.integer "parent_id", limit: 8
t.string "objectid", null: false
diff --git a/db/seeds.rb b/db/seeds.rb
index a975b07f8..14640e608 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -42,6 +42,7 @@ line_referential = LineReferential.find_or_create_by(name: "CodifLigne") do |ref
end
LineReferentialSync.find_or_create_by(line_referential: line_referential)
+StopAreaReferentialSync.find_or_create_by(stop_area_referential: stop_area_referential)
10.times do |n|
line_referential.lines.find_or_create_by name: "Test #{n}" do |l|
@@ -50,7 +51,6 @@ LineReferentialSync.find_or_create_by(line_referential: line_referential)
end
-
offer_workbench = OfferWorkbench.find_or_create_by(name: "Gestion de l'offre", organisation: operator)
[["parissudest201604", "Paris Sud-Est Avril 2016"],
diff --git a/lib/stif/codif_line_synchronization.rb b/lib/stif/codif_line_synchronization.rb
index 01ad29afc..9da0e30cc 100644
--- a/lib/stif/codif_line_synchronization.rb
+++ b/lib/stif/codif_line_synchronization.rb
@@ -61,12 +61,12 @@ module Stif
total_deleted = deleted_op + deleted_li + deleted_ne + deleted_gr
total_time = elapsed_time_since start_time
- LineReferential.first.line_referential_sync.record_status :ok, I18n.t('synchronization.message.success', time: total_time, imported: total_codifligne_elements, deleted: total_deleted)
+ LineReferential.first.line_referential_sync.record_status :ok, I18n.t('synchronization.codifligne.message.success', time: total_time, imported: total_codifligne_elements, deleted: total_deleted)
rescue Exception => e
total_time = elapsed_time_since start_time
Rails.logger.error "Codifligne:sync - Error: #{e}, ended after #{total_time} seconds"
- LineReferential.first.line_referential_sync.record_status :ko, I18n.t('synchronization.message.failure', time: total_time)
+ LineReferential.first.line_referential_sync.record_status :ko, I18n.t('synchronization.codifligne.message.failure', time: total_time)
end
end
@@ -174,4 +174,4 @@ module Stif
end
end
end
-end \ No newline at end of file
+end
diff --git a/lib/stif/reflex_synchronization.rb b/lib/stif/reflex_synchronization.rb
index 55fe3d404..eab86caf1 100644
--- a/lib/stif/reflex_synchronization.rb
+++ b/lib/stif/reflex_synchronization.rb
@@ -11,45 +11,52 @@ module Stif
end
def synchronize
- start = Time.now
- client = Reflex::API.new
- processed = []
+ tstart = Time.now
+ begin
+ client = Reflex::API.new
+ processed = []
- ['getOR', 'getOP'].each do |method|
- results = client.process method
- Rails.logger.info "Reflex:sync - Process #{method} done in #{Time.now - start} seconds"
- results.each do |type, entries|
- Rails.logger.info "Reflex:sync - #{entries.count} #{type} retrieved"
- end
+ ['getOR', 'getOP'].each do |method|
+ start = Time.now
+ results = client.process method
+ Rails.logger.info "Reflex:sync - Process #{method} done in #{Time.now - start} seconds"
+ results.each do |type, entries|
+ Rails.logger.info "Reflex:sync - #{entries.count} #{type} retrieved"
+ end
- # Create or update stop_area for every quay, stop_place
- stop_areas = results[:Quay].merge(results[:StopPlace])
- start = Time.now
- stop_areas.each do |id, entry|
- processed << self.create_or_update_stop_area(entry).objectid
- end
- Rails.logger.info "Reflex:sync - Create or update StopArea done in #{Time.now - start} seconds"
+ # Create or update stop_area for every quay, stop_place
+ stop_areas = results[:Quay].merge(results[:StopPlace])
+ start = Time.now
+ stop_areas.each do |id, entry|
+ processed << self.create_or_update_stop_area(entry).objectid
+ end
+ Rails.logger.info "Reflex:sync - Create or update StopArea done in #{Time.now - start} seconds"
- # Walk through every entry and set parent stop_area
- start = Time.now
- stop_areas.each do |id, entry|
- self.stop_area_set_parent entry
+ # Walk through every entry and set parent stop_area
+ start = Time.now
+ stop_areas.each do |id, entry|
+ self.stop_area_set_parent entry
+ end
+ Rails.logger.info "Reflex:sync - StopArea set parent done in #{Time.now - start} seconds"
end
- Rails.logger.info "Reflex:sync - StopArea set parent done in #{Time.now - start} seconds"
+
+ # Purge deleted stop_area
+ deleted = self.set_deleted_stop_area processed.uniq
+ self.defaut_referential.stop_area_referential_sync.record_status :ok, I18n.t('synchronization.reflex.message.success', time: Time.now - tstart, imported: processed.uniq.size, deleted: deleted.size)
+ rescue Exception => e
+ Rails.logger.error "Reflex:sync - Error: #{e}, ended after #{Time.now - tstart} seconds"
+ LineReferential.first.line_referential_sync.record_status :ko, I18n.t('synchronization.reflex.message.failure', time: Time.now - tstart)
end
- # Purge deleted stop_area
- self.set_deleted_stop_area processed.uniq
end
def set_deleted_stop_area processed
- Rails.logger.info "Reflex:sync - StopArea start deleted_stop_area"
start = Time.now
deleted = Chouette::StopArea.where(deleted_at: nil).pluck(:objectid).uniq - processed
deleted.each_slice(50) do |object_ids|
Chouette::StopArea.where(objectid: object_ids).update_all(deleted_at: Time.now)
end
- Rails.logger.info "Reflex:sync - StopArea #{deleted.size} stop_area deleted since last sync"
- Rails.logger.info "Reflex:sync - StopArea purge deleted in #{Time.now - start} seconds"
+ Rails.logger.info "Reflex:sync - StopArea #{deleted.size} stop_area deleted since last sync in #{Time.now - start} seconds"
+ deleted
end
def stop_area_set_parent entry
diff --git a/spec/factories/stop_area_referential_syncs.rb b/spec/factories/stop_area_referential_syncs.rb
new file mode 100644
index 000000000..e86a67400
--- /dev/null
+++ b/spec/factories/stop_area_referential_syncs.rb
@@ -0,0 +1,15 @@
+FactoryGirl.define do
+ factory :stop_area_referential_sync do
+ stop_area_referential nil
+
+ factory :stop_area_referential_sync_with_record do
+ transient do
+ stop_area_sync_operations_count rand(1..30)
+ end
+
+ after(:create) do |stop_area_referential_sync, evaluator|
+ create_list(:stop_area_sync_operation, evaluator.stop_area_sync_operations_count, stop_area_referential_sync: stop_area_referential_sync)
+ end
+ end
+ end
+end
diff --git a/spec/factories/stop_area_sync_operations.rb b/spec/factories/stop_area_sync_operations.rb
new file mode 100644
index 000000000..c62f7f9c6
--- /dev/null
+++ b/spec/factories/stop_area_sync_operations.rb
@@ -0,0 +1,8 @@
+FactoryGirl.define do
+ factory :stop_area_sync_operation do
+ status "MyString"
+stop_area_referential_sync nil
+message "MyString"
+ end
+
+end
diff --git a/spec/models/stop_area_referential_sync_spec.rb b/spec/models/stop_area_referential_sync_spec.rb
new file mode 100644
index 000000000..bc5f92b2d
--- /dev/null
+++ b/spec/models/stop_area_referential_sync_spec.rb
@@ -0,0 +1,24 @@
+require 'rails_helper'
+
+RSpec.describe StopAreaReferentialSync, :type => :model do
+ it 'should have a valid factory' do
+ expect(FactoryGirl.build(:stop_area_referential_sync)).to be_valid
+ end
+
+ it { is_expected.to belong_to(:stop_area_referential) }
+ it { is_expected.to have_many(:stop_area_sync_operations) }
+
+ describe '.record_status'
+ let!(:stop_area_ref_sync) { create(:stop_area_referential_sync) }
+ let!(:stop_area_ref_sync_with_records) { create(:stop_area_referential_sync_with_record, stop_area_sync_operations_count: 30) }
+
+ it 'should add a new record' do
+ stop_area_ref_sync.record_status :ok, "message"
+ expect(stop_area_ref_sync.stop_area_sync_operations.count).to eq(1)
+ end
+
+ it 'should not have more than 30 records' do
+ stop_area_ref_sync_with_records.record_status :ok, "message"
+ expect(stop_area_ref_sync_with_records.stop_area_sync_operations.count).to eq(30)
+ end
+end
diff --git a/spec/models/stop_area_sync_operation_spec.rb b/spec/models/stop_area_sync_operation_spec.rb
new file mode 100644
index 000000000..a98108d71
--- /dev/null
+++ b/spec/models/stop_area_sync_operation_spec.rb
@@ -0,0 +1,9 @@
+require 'rails_helper'
+
+RSpec.describe StopAreaSyncOperation, :type => :model do
+ it 'should have a valid factory' do
+ expect(FactoryGirl.build(:stop_area_sync_operation)).to be_valid
+ end
+
+ it { is_expected.to belong_to(:stop_area_referential_sync) }
+end
diff --git a/spec/tasks/reflex_rake_spec.rb b/spec/tasks/reflex_rake_spec.rb
index bf1a81a82..3a0ce0632 100644
--- a/spec/tasks/reflex_rake_spec.rb
+++ b/spec/tasks/reflex_rake_spec.rb
@@ -7,7 +7,9 @@ describe 'reflex:sync' do
stub_request(:get, "https://reflex.stif.info/ws/reflex/V1/service=getData/?format=xml&idRefa=0&method=#{method}").
to_return(body: File.open("#{fixture_path}/reflex.zip"), status: 200)
end
- create(:stop_area_referential, name: 'Reflex')
+
+ stop_area_ref = create(:stop_area_referential, name: 'Reflex')
+ create(:stop_area_referential_sync, stop_area_referential: stop_area_ref)
Stif::ReflexSynchronization.synchronize
end
@@ -42,6 +44,11 @@ describe 'reflex:sync' do
Stif::ReflexSynchronization.synchronize
end
+ it 'should log sync operations' do
+ expect(StopAreaSyncOperation.count).to eq 2
+ expect(StopAreaSyncOperation.take.status).to eq "ok"
+ end
+
it 'should not create duplicate stop_area' do
expect(Chouette::StopArea.count).to eq 6
expect(Chouette::AccessPoint.count).to eq 2